LinuxQuestions.org
Visit Jeremy's Blog.
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 11-30-2009, 01:22 PM   #1
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
bash: two very similar errors, one ends a while loop, the other doesn't


Hello

Can anybody tell me why the script below exits the while loop after the first error?

It is part of a script to calculate checksums of files listed in $input_file. I want to capture stderr in a variable, and if there is any error output, write it to a file. I don't want to simply redirect with
Code:
command 2 >> $error_file
since I want to include further debugging info on each line of $error_file.

The script encounters an error when processing the first line of $input_file (this is by design - I'm testing it atm). It then continues executing until it reaches the end of the while block, then exits without going back to the beginning of the while loop. Since the error occurs in a subshell created by $( ), I would expect the while loop to continue until the end of $input_file is reached.

Here is the script with the problem:
Code:
#!/bin/bash

input_file=$1

output_file=sha256sums.txt

cat $input_file | while read i; do
	err_output=$(sha256sum $i 2>&1 1>>$output_file);
	echo exit status $? i is $i;
done
I run it with an input file consisting of 21 lines of text, each of which is the path to an existing regular file. Its output is one line, corresponding to the first line of the file:
Code:
exit status 1 i is Music/...Off The Bone/01 - Cramps - Human Fly.ra


Here is a very similar script which does not show the same problem:
Code:
#!/bin/bash

input_file=$1

output_file=sha256sums.txt

cat $input_file | while read i; do
#	err_output=$(sha256sum $i 2>&1 1>>$output_file);
	err_output=$(exit 1)
	echo exit status $? i is $i;
done
I run it with the same input file, and it generates output for each line,
Code:
exit status 1 i is [filename]
... [21 times]
Why does the first script stop after the first line? I know it continues to the end of the while block, since the command
Code:
echo exit status $? i is $i;
is executed. According to the bash man page,
Quote:
while list; do list; done
The while command continuously executes the do list as long as
the last command in list returns an exit status of zero.
It would appear that all relevant commands return an exit status of zero, and the only one which returns an exit status of 1 should not cause the loop to end.

Hope that that's clear and someone can help!

Thanks

OS
 
Old 11-30-2009, 02:31 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
The sha256sum command is reading from stdin and thus emptying it so the next read gets nothing.

From the man page
Code:
SYNOPSIS
       sha256sum [OPTION]... [FILE]...

DESCRIPTION
       Print or check SHA256 (256-bit) checksums.  With no FILE, or when FILE is -, read standard input.
The file name in $i is not quoted and includes the - character. Try
Code:
err_output=$(sha256sum "$i" 2>&1 1>>$output_file)
 
Old 12-02-2009, 09:30 AM   #3
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Original Poster
Rep: Reputation: 39
Brilliant, thanks! I knew the $i needed quoting, I'd omitted the quotes to force an error, but it didn't occur to me that the '-' character would be significant. Got it working now!
 
  


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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
a little bash help with the 'for' command (or similar) checkmate3001 Linux - General 5 02-18-2009 12:52 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
Odds and ends errors..... DragonM15 Linux - Software 2 01-15-2006 02:36 AM
Loop ends unexpectedly after one run! trex_dk Programming 2 11-11-2005 03:49 AM

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

All times are GMT -5. The time now is 04:21 AM.

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