Write a bash script that will compare a variable entered by

Write a bash script that will compare a variable entered by user input or passed parameter. You will hard code or set two variables in your script, one integer and one string. Determine if the variable being entered or passed is an integer or a string. Compare it to the variable of the same type you\'ve hard coded or set. If they match, display a successful message. If they do not match, display an error message.

Solution

echo \"Enter input\" #enter input
read -r VAL
STR=\"beauty\"
NUM=5
RE=\'^[0-9]+$\'
if ! [[ $VAL =~ $RE ]]; then #compare if it is not number
if [ \"$VAL\" == \"$STR\" ]; then #compare with string
echo \"Matched\"
exit 1
fi
else
if [ \"$VAL\" = \"$NUM\" ]; then #compare with number
echo \"Matched\"
exit 1
fi
fi
echo \"Error not matched\"   

-----------------------------------------

Example output:-

Enter input

5

matched

Write a bash script that will compare a variable entered by user input or passed parameter. You will hard code or set two variables in your script, one integer

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site