Search code examples
linuxbashshellglob

Check what kind of parameters shell script got


I need some help in my shell script. I have this:

#!/bin/bash

for i in $*
do
   if [[ $i = *[a-zA-Z] ]]
      then echo $i contains just letters.
   elif [[ $i = *[a-zA-Z0-9] ]]
      then echo $i contains numbers and letters.
   else
      echo Error.
done

I would like the result to be, for example:

$ ./script.sh abCd a9d a-b
abCd contains just letters.  
a9d contains numbers and letters.  
Error.

But I get contains just letters in every case.

I also tried grep command too, but without success.


Solution

  • Your RegEx are wrong. Try the following:

    #!/bin/bash
    
    for i in $*
    do
       if [[ $i =~ ^[a-zA-Z]+$ ]]
          then echo $i contains just letters.
       elif [[ $i =~ ^[a-zA-Z0-9]+$ ]]
          then echo $i contains numbers and letters.
       else
          echo Error.
       fi
    done