vendredi 27 octobre 2017

bash scripting: if condition

I am passing command line arguments to a script, say a.sh.

From a.sh, I use "$@" to pass all those arguments to another bash script (user_pref.sh). In this bash file, I am trying to assign either 0 or 1 based on the parameters received from a.sh using basic if condition. But for some reason, I am not able to reason the correct value and all my variables are set to 0. I am new to bash scripting and would really appreciate any help here. I have referred different post on the site but they have not helped me resolve my issue so far.

a.sh code

#!/bin/sh

/home/postgres/viraltest_scripts/user_pref.sh "$@" &> /home/postgres/viraltest_scripts/logs/refresh_dev_sql.log

user_pref.sh Code:

#!/bin/sh
## assigning default values
a=1
b=1
c=1
if [[ $1 -eq 0 ]]
then
    a=0
fi

if [[ $2 -eq 0 ]]
then
    b=0
fi

if [[ $3 -eq 0 ]]
then
    c=0
fi

Irrespective of what I pass from a.sh, All the variables in user_pref.sh are assigned 0. Can anyone point out what am I doing wrong with the If statement? PS: I am aware of assigning a=$1 but for the purpose of my application, I have to explicitly assign 0 or 1 instead of doing a=$1.

Aucun commentaire:

Enregistrer un commentaire