Variable Syntax Error with Bash Script (Homework)

后端 未结 5 528
轻奢々
轻奢々 2020-12-11 11:07

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 s

相关标签:
5条回答
  • 2020-12-11 11:27

    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.

    0 讨论(0)
  • 2020-12-11 11:35

    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.

    0 讨论(0)
  • 2020-12-11 11:35

    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.
    0 讨论(0)
  • 2020-12-11 11:37

    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.

    0 讨论(0)
  • 2020-12-11 11:41

    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.

    0 讨论(0)
提交回复
热议问题