ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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[@]}"
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).
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.