4

Here is the script

bash --version | head -n1
if [ "$1" == "now"  ]
then
    echo if now
    execut job
else
    echo else "_"$1"_"  # make sure we are not picking any spaces
    if [ condition  ]
    then
    execut something else
    fi
fi

./script now
if now

works as expected if run from interactive shell. However if invoked from cron as, the if goes to the else block instead

* * * * *   root    /home/user/./script now >> /tmp/log
cat /tmp/log
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
else _now_

Same with "-eq" as well.

Am I missing something dead simple because I did not have breakfast yet ?

running Ubunt 14.04LTS.

Nabil Sham
  • 267
  • 1
  • 2
  • 9

2 Answers2

6

because the syntax is only with one equal: [ $a = $b ]

 if [ ! $a = $b ]; then     #with space in condition and need instruction between then and fi, else doesn't works
 ...Script to execute...
 elif [ ! $a = $c ]; then
 ...Script to execute...
 else
 ...Script to execute...
 fi     

conditional sh list

 [ $a = $b ] && return 1 || return 0        conditionnal alternative format
 if [ $a = $b -o $b = $c ]          -o mean OR
 if [ $a = $b -a $b = $c ]          -a mean AND
 if [ $a = $b -o $b = $c ] || [ $a = $b -a $a = $c ] multiple conditionnal or
 if [ $a = $b -o $b = $c ] && [ $a = $b -a $a = $c ] multiple conditionnal and
 if [ -z $a ]                       is empty or null
 if [ ! -z $a ]                     not equal to ""
 if [ -n $a ]                       is not empty or null
 if [ $a -eq $b ]                   equal (numeric)
 if [ $a -ne $b ]                   not equal (numeric)
 if [ $a -gt $b ]                   greater than (numeric)
 if [ $a -ge $b ]                   greater or equal than (numeric)
 if [ $a -lt $b ]                   lower than (numeric)
 if [ $a -le $b ]                   lower or equal than (numeric)
 if [ -d $a ]                       is a directory  
 if [ -e $a ]                       file exist  
 if [ -f $a ]                       is a file
 if [ -g $a ]                       is group allowed for the file
 if [ -r $a ]                       is readable 
 if [ -s $a ]                       file is not 0kb
 if [ -u $a ]                       is user allowed for file
 if [ -w $a ]                       is writable
 if [ -x $a ]                       is executable

more with bash

 if [[ "$a" =~ "$b" ]]              match (reg exp)
 if [[ $a == *$b* ]]                match (glob)
 if [ "${a/$b}" = $a ]              match (string)
 if [ -z "${a##*$b*}" ]             match (string, work with dropbox !)
 if [ ${a/$b} = $a ]                match (string)
Froggiz
  • 3,013
  • 1
  • 18
  • 30
  • Using double brackets, can use double equal `[[ "$1" == now ]]`, but I don't see a `#!` in the script, so your suggestion is better. – glenn jackman Nov 05 '15 at 17:03
  • 1
    So why "==" works in interactive shell ? – Nabil Sham Nov 05 '15 at 18:46
  • 3
    To expand on this: `==` is allowed by bash, but not by all other shells. Since this script does not start with a shebang line (e.g. `#!/bin/bash`), which shell it runs under is up to the whim of whatever launches it. When you run it from an interactive bash shell it uses bash. When cron runs it, it defaults to /bin/sh, which on your system is actually `dash` -- a much more basic shell that doesn't support `==` or `[[ ]]` or any other bash extensions. BTW, don't use `-eq` For this -- it does numeric comparison, not string comparison. – Gordon Davisson Nov 05 '15 at 18:54
0

Putting in @gordon-davisson 's comment as an answer since that's what resolved it for me:

Add the "shebang" line at the top of the shell script:

 #!/bin/bash

That will tell the OS that it's a bash script and needs to be interpreted that way. Then it will work with if [ "$a" == "$b" ] and the like.

Nikhil VJ
  • 141
  • 1
  • 6