How to execute the multiple commands with && operator in new lines(not all in single line) :shell script

Tirumala

I am Installing multiple packages by using && operator for stopping execution of succeeding one if preceding one fails. && function is working when all written in single line.

But When they written in line by line && function is not working and I am getting error. My code is like this:

yum -y install XXZ \
&& yum -y install rsync \
&& yum -y install libxml2-devel \
&& yum -y install ntp

The Error is:

install5.sh: line 4: syntax error near unexpected token `&&'
'nstall5.sh: line 4: `&& yum -y install rsync \

How to stop the execution of successive one if preceding one fails by using && operator when the commands are written in line by line(not in a single line).?

Please Help!

Neel_123

$? stores exit code of last command. You can do the following in a script:

yum -y install XXZ
if [[ $? != "0" ]]   ## exit code other than zero 
then
    ## your code for failure
    exit
fi
yum -y install rsync
if [[ $? != "0" ]]     
then
    ## your code for failure
    exit
fi

Do the same for all packages. I recommend you to make an array and use for loop. Exit code zero means success. Hope this helped.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Run multiple commands inside a buildah container separated by the boolean AND (&&) operator from the command line

From Dev

How to apply the ! (not) operator in simple terminal commands

From Dev

Union commands after the || (OR) operator

From Dev

TCL conditional commands using ternary operator

From Dev

Redirecting the input of two commands via pipe operator in Bash

From Dev

Invalid arithmetic operator while loop using output from aws cli commands

From Dev

Merging Users in Kinvey

From Dev

PostgreSQL: Efficiently split JSON array into rows

From Dev

boost::asio how to read full buffer in right way?

From Dev

GIT SVN: fetching a recreated SVN branch without the wrong merge parent

From Dev

How to find out value of a particular attribute index in core data model iOS

From Dev

iOS - iPad preview in Xcode is missing

From Dev

Java: Method hooking & Finding object instances

From Dev

Using git diff, how can I show the patch from the index to a given commit?

From Dev

Cast for upcasting only

From Dev

How can I multiply or divide value under the cursor by a repeat count?

From Dev

Can`t compile boost spirit word_count_lexer example

From Java

How to make a for loop variable const with the exception of the increment statement?

From Java

Count letters in a text in the Welsh language

From Java

Why does using the ternary operator to return a string generate considerably different code from returning in an equivalent if/else block?

From Java

No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp() in Flutter and Firebase

From Java

Changes using mutable reference of a field are not reflected after move of the original instance

From Java

Elastic Search indexes gets deleted frequently

From Java

Forgot do in do... while loop

From Java

How to fix "An unexpected error occurred. Please try again later. (7100000)" error in Google Play Console?

From Java

Does C# perform short circuit evaluation of if statements with await?

From Java

Why doesn't Python give any error when quotes around a string do not match?

From Java

Why is this regular expression so slow in Java?

From Java

Why are my two tuples containing strings, created the same way, not equal?

Related Related

  1. 1

    Run multiple commands inside a buildah container separated by the boolean AND (&&) operator from the command line

  2. 2

    How to apply the ! (not) operator in simple terminal commands

  3. 3

    Union commands after the || (OR) operator

  4. 4

    TCL conditional commands using ternary operator

  5. 5

    Redirecting the input of two commands via pipe operator in Bash

  6. 6

    Invalid arithmetic operator while loop using output from aws cli commands

  7. 7

    Merging Users in Kinvey

  8. 8

    PostgreSQL: Efficiently split JSON array into rows

  9. 9

    boost::asio how to read full buffer in right way?

  10. 10

    GIT SVN: fetching a recreated SVN branch without the wrong merge parent

  11. 11

    How to find out value of a particular attribute index in core data model iOS

  12. 12

    iOS - iPad preview in Xcode is missing

  13. 13

    Java: Method hooking & Finding object instances

  14. 14

    Using git diff, how can I show the patch from the index to a given commit?

  15. 15

    Cast for upcasting only

  16. 16

    How can I multiply or divide value under the cursor by a repeat count?

  17. 17

    Can`t compile boost spirit word_count_lexer example

  18. 18

    How to make a for loop variable const with the exception of the increment statement?

  19. 19

    Count letters in a text in the Welsh language

  20. 20

    Why does using the ternary operator to return a string generate considerably different code from returning in an equivalent if/else block?

  21. 21

    No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp() in Flutter and Firebase

  22. 22

    Changes using mutable reference of a field are not reflected after move of the original instance

  23. 23

    Elastic Search indexes gets deleted frequently

  24. 24

    Forgot do in do... while loop

  25. 25

    How to fix "An unexpected error occurred. Please try again later. (7100000)" error in Google Play Console?

  26. 26

    Does C# perform short circuit evaluation of if statements with await?

  27. 27

    Why doesn't Python give any error when quotes around a string do not match?

  28. 28

    Why is this regular expression so slow in Java?

  29. 29

    Why are my two tuples containing strings, created the same way, not equal?

HotTag

Archive