0

I'm running commands in bash like this:

cd path &&
service service1 stop &&
(run some scripts) &&
service service1 start &&
service service2 restart

Is there any tool which would run commands without "&&" at end like it would be there? Now I'm just adding "&&" with replace function in text editor, I know i can write simple script which would do that automatically but if there is tool for that in repo it would be great.

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

0

&& lets you do something based on whether the previous command completed successfully. It may be used to chain commands, so that the next command runs only if the previous succedded. If this is Your goal, there is a simpler way. If You want to run the next command only if the previous one succedded, You may want to use:
set -euo pipefail
If any of the commands fail (return with a non-zero status), than the script will stop.
From http://tldp.org/LDP/abs/html/options.html :
- set -e -> Abort script at first error, when a command exits with non-zero status (except in until or while loops, if-tests, list constructs)
- set -u -> Attempt to use undefined variable outputs error message, and forces an exit
- set -o pipefail -> Causes a pipeline to return the exit status of the last command in the pipe that returned a non-zero return value.

( Every shell script that i write, starts with a set -euo pipefail. It is a good practice. )

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Using `set -e` without a solid understanding of when it might fail is *not* good practice. See http://mywiki.wooledge.org/BashFAQ/105. – chepner Dec 08 '17 at 14:16
0

you can use sed : sed ':a;N;$!ba;s/\n/ \&\& /g' < in.txt > out.txt

how it works is explained in this question.

Setop
  • 2,262
  • 13
  • 28
  • Whilst code only answers may solve the original problem, some explanation of your solution would improve the quality of this answer. Also 'try' is more something for a comment, an answer should provide a working solution. – Nigel Ren Dec 08 '17 at 15:17