What's up with spacing and syntax errors in Shell Scripts? -


i'm pretty familiar , comfortable batch scripts on windows. i've started out on unix , i'm having hard time getting hang of spacing things properly.

the script below works fine. let's call script1.sh

!#/bin/sh now=$(date +"%s") echo "this file created on $now" 

this script pasted below throws error ,also i've pasted further below

!#/bin/sh now=$(date + "%s") echo "this file created on $now" 
[root@localhost /]# sh postbackup.sh postbackup.sh: line 1: !#/bin/sh: no such file or directory date: operand `%s' try `date --help' more information. file created on 

why space between + , "%s" matter ? additional information on how might affect in other cases extremely helpful.

you have 2 errors in script:


postbackup.sh: line 1: !#/bin/sh: no such file or directory

this because should use !# (wrong), , not #! (correct), , tries run !#/bin/sh command, doesn't exist.
error otherwise harmless , "works" because call sh explicitly sh postbackup.sh, it's still error , should fix it.


date: operand `%s'

this doesn't work on commandline, date expects single argument, starts +, , provides format (%s in case), adding space there makes two arguments, , date doesn't know %s belongs +.

as said, fail if enter on command line:

$ date + %s   date: operand ‘%s’ 

so need do, make single argument, can done either adding quotes, or escaping space \:

$ date '+ %s'  1427897164 $ date +\ %s  1427897164 

note output starts space! expected, you've given space date.

as far know, there no way prevent leading space, except removing later ... suggest don't use space here ;-)


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -