LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-06-2022, 04:26 AM   #1
Debian6to11
Member
 
Registered: Jan 2022
Location: Limassol, Cyprus
Distribution: Debian
Posts: 382
Blog Entries: 1

Rep: Reputation: 71
Script command explanation


Suppose I have a script with four lines as follows:
command1
command2
command3
command4

If command2 gives an error does the script proceed to command3, or does it stop?

If so, is it essentially the same as:
command1 && command2 && command3 && command4
 
Old 03-06-2022, 06:56 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,764

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
No, the && means command2 will only run if command1 completed successfully and so on.

command1
command2
...

command2 will run regardless if command1 ran successfully.
 
1 members found this post helpful.
Old 03-06-2022, 08:05 AM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,146
Blog Entries: 6

Rep: Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834
Why don't you try/experiment/test it for yourself.

Code:
#!/usr/bin/bash

a=({1..20})
b=(cow horse dog cat bird fish frog elephant lion tiger)
c=(one two three four five six seven eight nine ten)

echo "${a[@]}"
ech "${b[@]}"
echo "${c[@]}"

echo -e ''$_{1..50}'\b─'

echo "${a[@]}" &&
ech "${b[@]}" &&
echo "${c[@]}"
 
1 members found this post helpful.
Old 03-06-2022, 08:57 AM   #4
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,617

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555

If you want a script to stop when it encounters an error, you can use "set -e"

 
1 members found this post helpful.
Old 03-06-2022, 10:24 AM   #5
Debian6to11
Member
 
Registered: Jan 2022
Location: Limassol, Cyprus
Distribution: Debian
Posts: 382

Original Poster
Blog Entries: 1

Rep: Reputation: 71
It seems I have to use (set -e).

Is there anything I can do to find out which line gave the error?
 
Old 03-06-2022, 10:44 AM   #6
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,617

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555

"set -x" will output certain commands, after expansions, but before executing.

It's quite common to apply these to the shell/bash command - e.g. "#!/usr/bin/bash -ex" - try that with teckk's script to see how it works.

The benefit of "set" is you can turn the options on/off for relevant parts of a script (Though confusingly, set uses "+" to turn things off.)

Also, if you haven't already, ShellCheck can be used to highlight common errors.


Last edited by boughtonp; 03-06-2022 at 10:52 AM.
 
Old 03-06-2022, 11:12 AM   #7
Debian6to11
Member
 
Registered: Jan 2022
Location: Limassol, Cyprus
Distribution: Debian
Posts: 382

Original Poster
Blog Entries: 1

Rep: Reputation: 71
I tried teckk's script and also a second time I used (-ex) at the end of the hashbang line, I have seen no difference.
Trying that with the set -e as a second line closed the terminal window after pressing enter, no error output. Never mind about that though.

With the set -e option, I can place that line anywhere, let's say at the beginning of a script, and to unset it at line 50 for instance, I have to insert a line as (set +). Did I get that right?

And there is nothing that can show me which line or command gave me an error? I am planning to automate LFS as much as I can and I do expect a few errors at least (on a good day). I am planning to make it in 3-4 sections and then combine it (the file will be long at the end).
 
Old 03-06-2022, 11:19 AM   #8
Debian6to11
Member
 
Registered: Jan 2022
Location: Limassol, Cyprus
Distribution: Debian
Posts: 382

Original Poster
Blog Entries: 1

Rep: Reputation: 71
Or can I direct the output to a file to find out where the error was?
 
Old 03-06-2022, 11:19 AM   #9
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,146
Blog Entries: 6

Rep: Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834
Quote:
And there is nothing that can show me which line or command gave me an error?
test1.sh
Code:
#!/usr/bin/bash

set -x

a=({1..20})
b=(cow horse dog cat bird fish frog elephant lion tiger)
c=(one two three four five six seven eight nine ten)

echo "${a[@]}"
ech "${b[@]}"
echo "${c[@]}"

echo -e ''$_{1..50}'\b─'

echo "${a[@]}" &&
ech "${b[@]}" &&
echo "${c[@]}"
Code:
bash ./test1.sh
+ a=({1..20})
+ b=(cow horse dog cat bird fish frog elephant lion tiger)
+ c=(one two three four five six seven eight nine ten)
+ echo 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
+ ech cow horse dog cat bird fish frog elephant lion tiger
./test1.sh: line 10: ech: command not found
+ echo one two three four five six seven eight nine ten
one two three four five six seven eight nine ten
+ echo -e '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─' '\b─'
──────────────────────────────────────────────────
+ echo 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
+ ech cow horse dog cat bird fish frog elephant lion tiger
./test1.sh: line 16: ech: command not found
Tells exactly what line failed.

Last edited by teckk; 03-06-2022 at 11:20 AM.
 
1 members found this post helpful.
Old 03-06-2022, 11:49 AM   #10
Debian6to11
Member
 
Registered: Jan 2022
Location: Limassol, Cyprus
Distribution: Debian
Posts: 382

Original Poster
Blog Entries: 1

Rep: Reputation: 71
Good one, what I need. And that was a nice example teckk.

Thanks to all. Have a nice day/night.
 
Old 03-06-2022, 01:02 PM   #11
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,617

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555
Quote:
Originally Posted by Debian6to11 View Post
I tried teckk's script and also a second time I used (-ex) at the end of the hashbang line, I have seen no difference.
You should do! Although you haven't stated which distro/shell you're using, but I'm fairly sure this is POSIX level and should work everywhere

Anyway, here's a quick example - it has the same effect whether you are running Bash yourself, or whether execve is calling it as "#!/path/to/bash" (although one important distinction is the latter passes only a single argument to the interpreter).
Code:
$ bash -c 'echo one;ech two;echo three'
one
bash: ech: command not found
three

$ bash -e -c 'echo one;ech two;echo three'
one
bash: ech: command not found

$ bash -x -c 'echo one;ech two;echo three'
+ echo one
one
+ ech two
bash: ech: command not found
+ echo three
three

$ bash -ex -c 'echo one;ech two;echo three'
+ echo one
one
+ ech two
bash: ech: command not found

Quote:
With the set -e option, I can place that line anywhere, let's say at the beginning of a script, and to unset it at line 50 for instance, I have to insert a line as (set +). Did I get that right?
Enable with "set -e", (optionally) disable with "set +e", or "set -x" and "set +x", or "set -ex" and "set +ex", etc.

 
Old 03-07-2022, 01:10 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,989

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
what was missing:
I would suggest to use:
Code:
set -o errexit # set -e
set -o nounset
errexit will force to stop the script in case of an error, so back to the original post, if command2 fails command3 and anything after will not be executed.
nounset is used to stop if someone tries to use an undefined variable.
set -o xtrace (or set -x) is used to print a lot of debug messages, this was described before.
 
Old 03-07-2022, 06:41 AM   #13
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,617

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555

And for consistency/completeness, "set -o nounset" is the same as "set -u"

The benefit of the single-letter options over option names is the ability to combine them in a single argument/command.

"help set" provides a convenient reminder of what's what.

 
Old 03-07-2022, 12:02 PM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,875
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Also there is set -o pipefail
Example:
Code:
set -e
make install 2>&1 | tee log.make.all
echo 'tee' succeed
vs
Code:
set -e
set -o pipefail
make install 2>&1 | tee log.make.all
echo both 'make install' and 'tee' succeed
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Basic dig Command Usage and Detailed Explanation of its Output. LXer Syndicated Linux News 1 02-23-2019 03:46 AM
command explanation shlomo.hovir Linux - Newbie 11 10-17-2018 02:35 PM
[SOLVED] Need an explanation of here scripts as well as the "cat" command UNGR Linux - Newbie 11 08-13-2011 10:08 AM
Need explanation for TOP command bkarthick Linux - Newbie 3 06-02-2011 08:37 AM
Netstat command output explanation. nobitavn Linux - Newbie 8 10-13-2010 11:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration