NSurette
NSurette

Reputation: 21

Variable Syntax Error with Bash Script (Homework)

I'm currently working on homework for a Unix course and I've got everything else done but I just can't figure out what I've done wrong here. I've done this exact same script in csh (but we need to do it in bash as well.) I've already fixed a few errors but now the one I'm getting is "Variable Syntax" when I try to run it.

I've tried using double brackets for the while and ifs I've also tried declaring the variables for the inputs before I try to read to them I've also tried bugging several people in my class but most of them are pretty clueless and just copy off others.

#/!bin/bash
#
#Menunix - Bash
#
#Usage: menunixb
input = "$(1)" 
while [ $input != 5 ]
do
    echo Please choose an option
    echo 1: List Files 
    echo 2: Display today's date and time
    echo 3: Check whether a file is a directory or not
    echo 4: Create a file backup
    echo 5: Quit
    read input
    case $input in
        1) 
            ls
            ;;
        2)
            echo -n Time:
            date +"%T"
            echo Date:
            date +"%D"
            ;;
        3)
            echo What file do you wish to check
            read finput
            if [ -d $finput ] ; then
                echo $finput is a Directory
            elif [ -f $finput ] ; then
            echo $finput is a File
            else
                echo $finput does not exist
                ;;
            4)
                echo Please enter filename to backup
                read binput
                cp $binput{,.bak}
                ;;
            5)
                exit 1
            *)
                echo Please choose a valid input
                exit 1
            esac
        done
#EOF

Upvotes: 2

Views: 3193

Answers (5)

NSurette
NSurette

Reputation: 21

The error is actually on the interpreter line:

#/!bin/bash

is not the same as

#!/bin/bash

Once I actually had the script running in bash it was just a few minor syntax errors and I was done.

Upvotes: 0

mklement0
mklement0

Reputation: 439193

Update: As the OP discovered, another issue was a malformed shebang line: #/!bin/bash instead of #!/bin/bash (consider using #!/usr/bin/env bash, though).

A tip beforehand: http://shellcheck.net is a very handy tool for checking shell code for errors.


@Ube and @kojiro's answers both contain important pointers:

  • ' is used for quoting, so it must either be used inside double quotes, or it must be escaped as \'.

  • No spaces are allowed around the = in variable assignments in bash.

But there's more:

  • Use $1 to refer to the first argument; $(1) does something fundamentally different: it uses command substitution ($(...)) to execute the enclosed command and return its stdout output; thus, $(1) tries to execute a - most likely non-existent - command 1 and return its output.

  • Always use [[ ... ]] rather than [ ...] in bash; it's more robust and provides more features; case in point: [ $input != 5 ] will break if $input is undefined or empty - you'd have to double-quote it to prevent that; by contrast, it's fine to use [[ $input != 5 ]].

  • All case branches must be terminated with ;; (even the branches that just contain exit commands;).Fine print: the very last branch also works without ;;

  • The if [ -d $finput ] ; then ... statement is missing its closing fi.

Upvotes: 1

David W.
David W.

Reputation: 107060

I indented the code for you. You have a few issues:

  • Always use quotation marks around echo statements. You can't use words like today's or else you're starting to quote everything to the end of your file.
  • Use indents. I indented your code as it appeared on my editor. Notice something? You're missing a fi to end an if statement.

What type of system are you on? You should always use a real live program editor. I dope slap developers who use Notepad at my work.

  • You can always use VIM on any platform. It comes on Linux and Mac and is downloadable on Windows. Does automatic indenting and syntax highlighting which would have caught your incomplete if statement and your today's error.
  • VIM can be a bit daunting. It's powerful, but hard to learn. Notepad++ is a easy to use program editor with syntax highlighting and auto indenting.
  • On Mac, try TextWrangler which is free and includes syntax highlighting and auto indenting. You can also use Xcode which comes for free with Mac OS X.
  • On Linux, you're on your own. Most Linux people use either VIM, EMACs, or Eclipse. Kate is popular, but is a KDE application. I've seen a few people use BlueFish. Most are free, easy to install, and are open source.
  • A not so secret trick. Put set -xv in your code. It'll print out the lines being executed and the result once the shell interpolates that line. You can even set the PS4 prompt to \$LINENO: which will print out the line number of your shell script too. This is a great debugging aid. To turn off the verbose debugging, use set +xv.

Upvotes: 0

Saucier
Saucier

Reputation: 4360

Have a look at:

input = "$(1)"

You notice the spaces around the equal sign (=)? This is not allowed in bash. It has to be

input="$(1)"

instead.

Upvotes: 3

kojiro
kojiro

Reputation: 77137

You must fix the quotes around the echo statements.

echo 2: Display today's date and time

is particularly wrong because it opens a quotation, but never closes it. Try quoting all those echo lines:

echo "2: Display today's date and time"

Spoiler: I was bored and refactored this. Feel free to use it to research a different way to do things in the shell. It may involve features you haven't been introduced to in your class, but you may find it helpful anyway.

Upvotes: 3

Related Questions