LinuxQuestions.org
Visit Jeremy's Blog.
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 01-09-2009, 02:46 PM   #1
geech
LQ Newbie
 
Registered: Jan 2009
Posts: 1

Rep: Reputation: 0
Case statements and vars for bash script


Can someone please tell me why when this runs that ZIP and FILE are echo'd as nulls at the end? The file test.txt has tons of stuff in it.

FILENAME=test.txt
count=0
cat $FILENAME | while read LINE
do
count=`expr "$count" + 1`
case $count in
"1") ZIP=$LINE;;
"2") FILE=$LINE;;
*)
esac
done
echo $ZIP
echo $FILE
 
Old 01-09-2009, 04:49 PM   #2
antegallya
Member
 
Registered: Jun 2008
Location: Belgium
Distribution: Debian
Posts: 109

Rep: Reputation: 42
Hello,
it is because when you use a pipe bash start a subshell to execute the while read ...
and so you are not under the scope of your ZIP and FILE variables when you try to echo them and then you get null.
To avoid this you can use a 'for' instead :
Code:
FILENAME=test.txt
count=0
for LINE in "$(cat $FILENAME)"
 do
   count=`expr "$count" + 1`
   case $count in
    "1") ZIP=$LINE;;
    "2") FILE=$LINE;;
    *)
   esac
 done
echo $ZIP
echo $FILE

Last edited by antegallya; 01-09-2009 at 04:52 PM. Reason: Added quotes around $(cat $FILENAME)...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Bash script: Move files, ignore case Fredde87 Linux - Software 3 09-03-2008 03:34 AM
BASH script with case ( mandriva ) hellreaper Programming 3 02-19-2008 07:36 AM
BASH IF statements Part II kinetik Programming 4 05-07-2006 01:18 AM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 02:41 AM
Newbie troubles with Bash if/then statements jimieee Programming 4 12-04-2003 06:33 PM


All times are GMT -5. The time now is 08:24 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration