![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Encoding Problem while using "|" (PIPE) as delimiter from Mainframe to Unix | seshendra | UNIX for Dummies Questions & Answers | 1 | 02-20-2008 05:36 AM |
| Getting error "Undefined symbol: .u_strlen_2_6" | nachiketv | AIX | 0 | 06-05-2006 09:16 AM |
| shared object "undefined symbol: fstat" error | marcus121 | High Level Programming | 5 | 04-24-2006 07:11 PM |
| By angle-brackets/"pipe" button doesn't work? | riwa | Linux | 1 | 04-02-2006 06:43 PM |
| Please help formatting bash "time" variable to HH:MM:SS format | vikingshelmut | Shell Programming and Scripting | 3 | 09-15-2005 08:54 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
bash: "undefined variable" and pipe
Hi,
haven't found anything about this through searching, so may be a new topic: when doing this: set -o nounset set -o errexit find . -name "*.lib" | while read library; do echo ${libary} done echo "after while" I expect the script to exit within the while loop (because of nounset and the typo libary!=library). Actually, the script continues, probably because the pipe is executed in a subshell. But shouldn't the undefined variable error propagate up to the calling shell and trigger a non-zero exit? Is there an option or way to force this? Thanks in advance. |
|
||||
|
Thanks for your answer, radoulov, but I'm not sure that's the solution, because the trap doesn't catch within the pipe, only when the script ends:
my script is called "b": trap "echo I\'m out..." exit set -o nounset -o errexit find ./t -name "*.lib"|while read library; do echo $libary; done echo the end $ sh b b: line 3: libary: unbound variable the end I'm out... Anyway this would trap *all* exits, the good ones as well as the bad ones, which is not what I'm after. I want the script "b" to exit with a non-zero return value as soon as it's encountering the unbound variable, even befor the while loop is completed. |
|
|||||
|
The trap was only for the visual effect
the point was the process subsitution, consider this: Code:
% cat s #!/bin/bash -ue find ./t -name "*.lib"|while read library;do echo $libary done echo after the first loop # you'll see it while read library;do echo $libary done< <(find ./t -name "*.lib") echo after the second one # you won't see it ubuntux% ./s ./s: line 4: libary: unbound variable after the first loop ./s: line 10: libary: unbound variable |
|
||||
|
Hi,
Try this script. This may help you. Code:
set -o nounset
#set -o errexit
find . -name "*.txt" | while read library; do
echo ${libary}
done
RC=$? #return status of the previous command
if [ $RC -ne 0 ]
then
exit
fi
echo "after while"
Chella |
|
||||
|
Thanks chella, but your example only warns when you expect an error. My point was that an unsuspected "unbound variable" could get unnoticed.
radoulov, that works. I got a bit confused by the different behaviours of "./s" and "sh s" (the latter gives a syntax error). Although, as I said, I was looking more for a setting that would raise an error with the existing code, rather than using a different construct, because when I know where to look, I can check unbound variables easily by hand. Anyway, it probably is a feature in bash (not a bug) so I just have to be watchful. Thank you very much again! |
|
|||||
|
Quote:
./command (in the first case the shebang is only a comment): Try changing the script like this: Code:
% cat s #!/bin/bash -ue find ./t -name "*.lib"|while read correct;do echo $wrong echo $correct done||exit echo after the loop % ./s ./s: line 3: wrong: unbound variable % sh s ./t/1.lib after the loop % sh -ue s s: 5: wrong: parameter not set Code:
% cat s #!/bin/bash -ue find ./t -name "*.lib"|while read correct;do echo $wrong echo $correct done echo after the loop % sh s ./t/1.lib after the loop % sh -uevx s #!/bin/bash -ue find ./t -name "*.lib"|while read correct;do echo $wrong echo $correct done + find ./t -name *.lib + read correct s: 1: wrong: parameter not set |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|