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
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.
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.
I indented the code for you. You have a few issues:
today's
or else you're starting to quote everything to the end of your file.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.
if
statement and your today's
error.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
.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.
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
.