Here is my code:
#!/bin/bash
if [ "$#" -ne 2 ] ; then
echo "$0: exactly 2 arguments expected"
exit 3
fi
if [$1 != "file" -a $1 != 'dir'] ; then
echo "$0: first argument must be string "file" or "dir""
exit 1
elif [-e $2 -a -r $2]; then
if ["$1" = "file" -a -f $2] ; then
echo YES
elif ["$1" = "dir" -a -d $2] ; then
echo YES
else
echo NO
fi
exit 0
else
echo "$0: $2 is not a readable entry"
exit 2
fi
If I run ./lab4 file filename1
it will check if the first parameter is the string "file" or "dir" then if the first parameter is "file" and filename1 is a file, it will print yes. Same thing for dir.
It doesn't recognize $1
and $2
. The code will output:
./lab04Q2: line 7: [file: command not found
./lab04Q2: line 10: [-e: command not found
even though I did put 2 parameters when running the program.
code
in your posts here.