Asnau Nauticas
Asnau Nauticas

Reputation: 346

Getting unexpected end of file error while using getopts in Shell script

Here is the following code:

compact()        { echo "compact alarms" }

node() { echo "node alarms" }

severity() { echo "Severity alarms" }

csv() { echo "CSV generator" }

table() { echo "tabular data" }

help() { echo "help" }

exit() { echo "exit" }

while getopts ":c :n :s :x :t :h :e" opt; do
  case $opt in
            c) compact
                ;;
            n) node
                ;;
            s) severity
                ;;
            x) csv
                ;;
            t) table
                ;;
            h) help
                ;;
            e) exit
                ;;
            *)
                echo "Error wrong Syntax,Opening help" && help
                ;;
        esac
done

when I am executing sh test.sh -t newfile I am getting Syntax error test.sh: line 36: syntax error: unexpected end of file Can someone please tell me why it is displaying unexpected end of file.

Upvotes: 1

Views: 246

Answers (1)

Juan Diego Godoy Robles
Juan Diego Godoy Robles

Reputation: 14955

You're using a wrong syntax for the functions, to use oneliners try this instead:

help() { echo "help";}

exit() { echo "exit";}

Explanation

From bash man page:

Shell Function Definitions

A shell function is an object that is called like a simple command and executes a compound command with a new set of positional parameters. Shell functions are declared as follows:

   [ function ] name () compound-command [redirection]

Compound Commands

{ list; } list is simply executed in the current shell environment. list must be terminated with a newline or semicolon. This is known as a group command.

Upvotes: 1

Related Questions