I'm trying to make a script to check if an argument has a single uppercase or lowecase letter, or if its anything else (a digit or a word for example.)
So far got this done:
if echo $1 | egrep -q '[A-Z]';
then echo "Uppercase";
elif echo $1 | egrep -q '[a-z]';
then echo "Lowercase";
else
echo "FAIL";
fi
Need to make it to fail me not only if it isnt a letter, but if I insert a word or 2 letters.
You need to use the grep command. The grep command or egrep command searches the given input FILEs for lines containing a match or a text string.
$0 Stores the first word of the entered command (the name of the shell program). $* Stores all the arguments that were entered on the command line ($1 $2 ...). "$@" Stores all the arguments that were entered on the command line, individually quoted ("$1" "$2" ...).
$@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.
When comparing strings in Bash you can use the following operators: string1 = string2 and string1 == string2 - The equality operator returns true if the operands are equal. Use the = operator with the test [ command. Use the == operator with the [[ command for pattern matching.
You was very close !
if echo $1 | egrep -q '^[A-Z]$';
then echo "Uppercase";
elif echo $1 | egrep -q '^[a-z]$';
then echo "Lowercase";
else
echo "FAIL";
fi
^
& $
, means respectively start of line & end of line
egrep
there, grep
is sufficient If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With