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
-
2Is the question about how to write basic bash syntax? There are tons of tutorials out there ...pLumo– pLumo2018-10-04 06:48:45 +00:00Commented Oct 4, 2018 at 6:48
-
1what 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– sj172018-10-04 06:51:51 +00:00Commented Oct 4, 2018 at 6:51
-
1I don't believe you looked. Here's one serverfault.com/a/216431/267016Chris Davies– Chris Davies2018-10-04 06:55:08 +00:00Commented Oct 4, 2018 at 6:55
-
Possible duplicate of Correct syntax for `if...elif` statementsKiwy– Kiwy2018-10-04 11:28:04 +00:00Commented Oct 4, 2018 at 11:28
Add a comment
|
1 Answer
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.
-
Okay thnx, that was just a dummy commands, it worked for me.sj17– sj172018-10-04 06:59:24 +00:00Commented Oct 4, 2018 at 6:59
-
Unfortunately it does not work in
GNU Make 4.2.1(usingSHELL=/bin/bash)artu-hnrq– artu-hnrq2021-06-27 03:55:35 +00:00Commented Jun 27, 2021 at 3:55 -
@artu-hnrq A Makefile is not a shell script. See e.g. How to write bash script in Makefile or How to write exactly bash scripts into Makefiles? or Iterate bash associative array in Makefile2021-06-27 06:15:53 +00:00Commented Jun 27, 2021 at 6:15