bash: "undefined variable" and pipe


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash: "undefined variable" and pipe
# 1  
Old 02-18-2008
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  
Old 02-18-2008
[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  
Old 02-18-2008
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  
Old 02-18-2008
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  
Old 02-18-2008
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  
Old 02-18-2008
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  
Old 02-18-2008
Quote:
Originally Posted by nagaidhlig
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

"Help with bash script" - "License Server and Patch Updates"

Hi All, I'm completely new to bash scripting and still learning my way through albeit vey slowly. I need to know where to insert my server names', my ip address numbers through out the script alas to no avail. I'm also searching on how to save .sh (bash shell) script properly.... (25 Replies)
Discussion started by: profileuser
25 Replies

3. Programming

Compiling C++ code with NetCDF libraries: "undefined reference"

Hi! I am trying to compile a C++ code with cmake and gcc on Ubuntu. The code uses NetCDF4 libraries. I specify the path to these libraries as follows: -I/usr/local/include -L/usr/local/lib -lnetcdf -lnetcdf_c++4 "ccmake" and "cmake" work fine. After typing "make" I receive the error... (0 Replies)
Discussion started by: Alauda
0 Replies

4. Emergency UNIX and Linux Support

Perl error: Can't call method "value" on an undefined value

Hi, I am running a perl script to automate a process and I keep running into a error can't find the "value" Can't call method "value" on an undefined value at process_file.pl line 44. file is CVS cell is ifdfdxrfmp.ksh Here is the script I have also attached it as well: ... (2 Replies)
Discussion started by: vpundit
2 Replies

5. Programming

make fails with "undefined reference to..."

i am compiling a program called vasp on suse and get the following error. there are many more preprocess and ifort commands prior so i just grabbed the tail of the log file: ./preprocess <main.F | /usr/bin/cpp -P -C -traditional >main.f90 -DMPI -DHOST=\"LinuxIFC\" -DIFC -Dkind8 -DNGZhalf... (6 Replies)
Discussion started by: crimso
6 Replies

6. Shell Programming and Scripting

Simplify Bash Script Using "sed" Or "awk"

Input file: 2 aux003.net3.com error12 6 awn0117.net1.com error13 84 aux008 error14 29 aux001.ha.ux.isd.com error12 209 aux002.vm.ux.isd.com error34 21 alx0027.vm.net2.com error12 227 dux001.net5.com error123 22 us008.dot.net2.com error121 13 us009.net2.com error129Expected Output: 2... (4 Replies)
Discussion started by: sQew
4 Replies

7. Shell Programming and Scripting

How can I use a pipe operator in a variable: OPTION="| command"?

I have a string of commands I am piping some data through and I want to allow command line switches to select which commands are used. I want to do something like this: OPTION="| command3" command1 -a -b c.txt | command2 -d -e $OPTION >result.txt I want to do it that way because OPTION may be... (1 Reply)
Discussion started by: KenJackson
1 Replies

8. AIX

Getting error "Undefined symbol: .u_strlen_2_6"

Hi, I am using xlC compiler. The compilation goes fine but at the time of linking it gives the following error ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more information. ld: 0711-317 ERROR: Undefined symbol: .u_strlen_2_6 ld: 0711-317 ERROR: Undefined symbol:... (0 Replies)
Discussion started by: nachiketv
0 Replies

9. Programming

shared object "undefined symbol: fstat" error

Didn't have this problem in AIX, but ported to Linux with GCC compiler and am now getting a runtime error: tssutil: symbol lookup error: /work/agility/devel/bin/libagam.so: undefined symbol: fstat I'm sure most of you know that fstat is an intrinsic function just like printf, memcpy, etc. When I... (5 Replies)
Discussion started by: marcus121
5 Replies

10. Shell Programming and Scripting

Please help formatting bash "time" variable to HH:MM:SS format

Ok, this is going to be hard to describe, but here it goes. I have written a bash script that, while executing starts a timer, and when done stops the timer. The $RUNTIME variable value is in seconds, so the variable usually equals a number like 126 (equals 2 minutes 6 seconds). In my script I... (3 Replies)
Discussion started by: vikingshelmut
3 Replies
Login or Register to Ask a Question