5
a=Y
b=Y
c=Y


if condition like $a=='y' then execute all this statements******  
cat *.tar.gz | tar -xzvf - -i  
echo "5"  
tar -xvf *.tar.gz   
echo "9"  
rm -rf *.tar.gz  

elif($b=='y') condition ***   
cp $source $destination  
cp $source/conf/* $destination/conf

else (**** )
some commands
pLumo
  • 22,231
  • 2
  • 41
  • 66
sj17
  • 65
  • 1
  • 1
  • 4
  • 2
    Is the question about how to write basic bash syntax? There are tons of tutorials out there ... – pLumo Oct 04 '18 at 06:48
  • 1
    what i actually want is to run multiple commands if condition is true otherwise skip all those commands, in all the tutorials i am getting that u can olny run one command after if holds true. – sj17 Oct 04 '18 at 06:51
  • 1
    I don't believe you looked. Here's one https://serverfault.com/a/216431/267016 – roaima Oct 04 '18 at 06:55
  • Possible duplicate of [Correct syntax for \`if...elif\` statements](https://unix.stackexchange.com/questions/266359/correct-syntax-for-if-elif-statements) – Kiwy Oct 04 '18 at 11:28

1 Answers1

11

The standard form of an if-statement is

if condition; then
    action
    action
    ...
elif condition; then
    action
    action
    ...
else
    action
    action
    ...
fi

where the elif and else branches are optional and where there may be multiple elif branches.

In your case:

if [ "$a" = "y" ]; then
    cat *.tar.gz | tar -xzvf - -i  
    echo "5"  
    tar -xvf *.tar.gz   
    echo "9"  
    rm -rf *.tar.gz
elif [ "$b" = "y" ]; then
    cp "$source" "$destination"  
    cp "$source"/conf/* "$destination"/conf   
else
    some commands
fi

I have not looked at the actual commands that you want to execute here, and whether they make sense.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • Okay thnx, that was just a dummy commands, it worked for me. – sj17 Oct 04 '18 at 06:59
  • Unfortunately it does not work in `GNU Make 4.2.1` (using `SHELL=/bin/bash`) – artu-hnrq Jun 27 '21 at 03:55
  • @artu-hnrq A Makefile is not a shell script. See e.g. [How to write bash script in Makefile](https://unix.stackexchange.com/q/255743) or [How to write exactly bash scripts into Makefiles?](https://unix.stackexchange.com/q/270778) or [Iterate bash associative array in Makefile](https://unix.stackexchange.com/q/232436) – Kusalananda Jun 27 '21 at 06:15