Reputation: 93
Here is my script:
#!/bin/bash
if [ $# -lt 2 || $# -gt 3 ]; then # invalid number of arguments given
echo "usage: minor3 name [euid]"
else # valid number of arguments given
echo Good day, $1! Nice to meet you!
CHOICE=1
while [ "$CHOICE" -gt 0 && "$CHOICE" -lt 4 ] # print out the menu, obtain user choice, execute appropriate system call using if-else statements, loop if desired
do
echo "+*******************************************************************+
Enter one of the following options: |
1) List and count all non-hidden files in the current directory. |
2) Check if given user (default = current user) is logged in, then |
... list all active processes for that user. |
3) List the sizes and names of the 10 largest files and directories |
... in the current directory. |
4) Exit this shell program. |
+*******************************************************************+
> "
read CHOICE
if [ "$CHOICE" = 1 ]; then
# list and count all files
echo test
fi
if [ "$CHOICE" = 2 ]; then
if [ $# = 3 ]; then # euid was given
# use given user ($2)
echo given user
else
# use current user
echo current user
fi
fi
if [ "$CHOICE" = 3 ]; then
# list sizes and names of 10 largest files/directories
echo test
fi
done
echo Thanks, $1! Have a great day!
fi
This is the error I'm getting:
cwd0042@cse04:~/3600/min3$ chmod +x minor3.sh
cwd0042@cse04:~/3600/min3$ ./minor3.sh
./minor3.sh: line 51: syntax error: unexpected end of file
Line 51 is the line after the last fi
, i.e., the line after the last line of my program. I found an earlier stack overflow post that said to use dos2unix, but the Linux server I have to test on is owned by my school, so I don't have the ability to install it, making it not an option. The server uses Linux Ubuntu 12.04.5 LTS, if that makes a difference.
Upvotes: 1
Views: 641
Reputation: 3405
You cannot use &&
and ||
inside [ ... ]
which just calls
the test
command.
Use -a
and -o
instead or use two [...]
expressions:
if [ "$#" -lt 2 ] || [ "$#" -gt 3 ] ; then
Another way is to use the builtin [[ ... ]]
bash operator.
It allows to use &&
and ||
:
if [[ "$#" -lt 2 || "$#" -gt 3 ]]; then
The program also crashes if you do not enter
a number and just type Enter. Check at least
if $CHOICE
is empty.
It seems that you have CRLF line ending (dos-like), use e.g.
perl -pi -e 's/\r\n/\n/g' -- YOURSCRIPTNAME.sh
do fix this. This emulates dos2unix
.
Upvotes: 3