The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-18-2008
nagaidhlig nagaidhlig is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 3
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.
  #2 (permalink)  
Old 02-18-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
[bash]

Code:
$ trap "echo I\'m out..." exit
$ set -o nounset -o errexit
$ find ./t -name "*.lib"|while read library; do echo $libary; done
bash: libary: unbound variable
$ while read library; do echo $libary; done< <(find ./t -name "*.lib")
bash: libary: unbound variable
I'm out...
  #3 (permalink)  
Old 02-18-2008
nagaidhlig nagaidhlig is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 3
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.
  #4 (permalink)  
Old 02-18-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
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
  #5 (permalink)  
Old 02-18-2008
chella chella is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 75
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"
Regards,
Chella
  #6 (permalink)  
Old 02-18-2008
nagaidhlig nagaidhlig is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 3
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!
  #7 (permalink)  
Old 02-18-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Quote:
Originally Posted by nagaidhlig View Post
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.
[...]
I think you should understand the difference between sh command and
./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
Consider this:

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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0