LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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
 
LinkBack Search this Thread
Old 12-23-2006, 02:16 AM   #1
ZT13
LQ Newbie
 
Registered: Dec 2006
Distribution: Ubuntu Edgy
Posts: 5

Rep: Reputation: 0
Angry BASH visibility problem


I have this weird problem: inside the while loop, ERR_COUNT and WAR_COUNT variables are modified by the script, but outside the loop, they still return the initial value, 0. Don't know what to do...
PLEASE HELP!!!!!


function compile
{
declare -i ERR_COUNT=0
declare -i WAR_COUNT=0
declare LINE
declare -i LEN
declare MESSAGE

gcc "$DEST_DIR/$IDEST_DIR/tema.c" -o "$DEST_DIR/$IDEST_DIR/tema" -O2 -Wall -lm -pedantic 2> "$DEST_DIR/$IDEST_DIR/rez.txt"
cat "$DEST_DIR/$IDEST_DIR/rez.txt" | while read LINE;
do
LEN=${#LINE};
LINE="${LINE#*warning}";
if [ ${#LINE} -ne $LEN ]; then
WAR_COUNT+=1;
else
LINE="${LINE#*error}";
if [ ${#LINE} -ne $LEN ]; then
ERR_COUNT+=1;
fi
fi
echo $ERR_COUNT $WAR_COUNT
done

echo $ERR_COUNT $WAR_COUNT
}

OUTPUT EXAMPLE:
1 0
2 0
2 1
2 2
2 3
0 0 #!!!!!!!!!!
 
Old 12-23-2006, 07:12 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OEL
Posts: 9,090

Rep: Reputation: 227Reputation: 227Reputation: 227
Use the Korn shell, bash is picking the arguably wrong side of a pipe as a background process.

Alternatively, you can just redirect the rez.txt file to the while command, instead of using an unnecessary cat.
 
Old 12-23-2006, 10:36 PM   #3
wmakowski
Member
 
Registered: Oct 2003
Location: Ohio
Distribution: Fedora 14, 15, RHL 5.2, Gentoo
Posts: 519

Rep: Reputation: 36
You can make this work with file descriptors or as jlliagre suggests redirecting rez.txt as input to the while. Here is a portion of your script with the file descriptor statements added.
Code:
...
gcc "$DEST_DIR/$IDEST_DIR/tema.c" -o "$DEST_DIR/$IDEST_DIR/tema" -O2 -Wall -lm -pedantic 2> "$DEST_DIR/$IDEST_DIR/rez.txt"

exec 3<$DEST_DIR/$IDEST_DIR/rez.txt

while read -u3 LINE;
do
LEN=${#LINE};
...
echo $ERR_COUNT $WAR_COUNT
done
exec 3<&-

echo $ERR_COUNT $WAR_COUNT
}
The statement exec 3<$DEST_DIR/$IDEST_DIR/rez.txt opens file descriptor 3 for reading from the rez.txt file.

The statement while read -u3 LINE; uses the file pointed to by file descriptor 3 as input to read on the while statement.

The statement exec 3<&- closes file descriptor 3.

Bill

Last edited by wmakowski; 12-23-2006 at 10:49 PM.
 
Old 12-23-2006, 11:00 PM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OEL
Posts: 9,090

Rep: Reputation: 227Reputation: 227Reputation: 227
A custom file descriptor is not needed in ZT13 case, so the redirection can be very much simplified this way:
Code:
...
gcc "$DEST_DIR/$IDEST_DIR/tema.c" -o "$DEST_DIR/$IDEST_DIR/tema" -O2 -Wall -lm -pedantic 2> "$DEST_DIR/$IDEST_DIR/rez.txt"

while read LINE
do
  LEN=${#LINE};
  ...
  echo $ERR_COUNT $WAR_COUNT
done < $DEST_DIR/$IDEST_DIR/rez.txt

echo $ERR_COUNT $WAR_COUNT
 
Old 12-24-2006, 01:17 AM   #5
ZT13
LQ Newbie
 
Registered: Dec 2006
Distribution: Ubuntu Edgy
Posts: 5

Original Poster
Rep: Reputation: 0
BASH visibility problem

Thank you folks
 
  


Reply

Tags
bash


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check application visibility 3saul Programming 9 02-13-2006 09:43 PM
File visibility problem in Linux / Windows shared FAT32 partition dineshshah Linux - General 2 01-03-2006 09:13 PM
Problem with GCC 4.0 Visibility eelshcn Programming 0 09-22-2005 02:52 PM
Partition Visibility to Windows philipogletree Linux - Newbie 2 07-25-2005 07:51 PM
directories visibility problem in redhat 9 lomaree Linux - Software 3 04-13-2004 01:59 AM


All times are GMT -5. The time now is 10:14 PM.

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
Open Source Consulting | Domain Registration