In: Computer Science
write a bash shell program named L1 as follows . L1 expects exactly 2 command line arguments. expects $1 to contain only digits. expects $2 to have exactly 4 characters (can be any character).
if NOT exactly 2 command line arguments, echo "need 2 args " and exit.
otherwise, if $1 and/or $2 have incorrect value(s) (as above), echo "bad argX", where X is the first incorrect argument, and exit.
otherwise, echo "good ". you can use echo,grep, pipe to check if the argument have the required characters.
example:
>L1 849474 0-=~
good
>L1 47347 8x
bad arg2
>L1 123abc 8d
bad arg1
#!/bin/bash
#Creating a regular expression, which will accept any types of number
re='^[+-]?[0-9]+([.][0-9]+)?$'
#Finding the length of the string using expr
len=`expr length "$2"`
if [[ $# -ne 2 ]]; then
echo "need 2 args"
elif ! [[ $1 =~ $re ]] ; then
echo "bad arg1";
elif [[ $len -ne 4 ]]; then
echo "bad arg2";
else
echo "good"
fi
Figure 1: Output
Figure 2: Code Screenshot