1

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.

5
  • 1
    If I copy your snippet and paste it into my Vim, he last fi is on line 39, not on line 50... Commented Feb 15, 2016 at 11:24
  • Probably just a formatting difference. The whole program is there. Commented Feb 15, 2016 at 11:37
  • 1
    @eckes The wrong line number is from the dos vs. unix line ending issue. Commented Feb 15, 2016 at 11:41
  • @jofel: yep. Thought about that when I read your dos2unix emulation script. Commented Feb 15, 2016 at 11:41
  • 1
    Run your code through shellcheck.net Commented Feb 15, 2016 at 13:57

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

@ColeDapprich I extended my answer.
Don't use -a and -o; they are all but deprecated.
@chepner It would be nice, if you could sketch the "modern" way and its advantages, such I can optimize the answer.
@chepner I included the [[ ... ]] way into the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.