The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
what is wrong with this tr -d? cleansing_flame UNIX for Dummies Questions & Answers 3 02-06-2008 09:34 AM
What’s wrong with the following? vrn UNIX for Dummies Questions & Answers 8 03-19-2006 06:09 PM
where have i gone wrong? Blip Shell Programming and Scripting 3 01-28-2004 01:43 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 02-02-2007
Registered User
 

Join Date: Dec 2006
Posts: 31
what is wrong here

Hello,
I have a simple script such as

-----------------------------
#! /bin/sh


YEAR=`date -u +%Y`;
MONTH=`date -u +%m`;
DAY=`date -u +%d`;
DATE=$MONTH$DAY$YEAR
LOGFILES=auditTrail-$DATE
LOGMATCH=$LOGFILES\*
ARGUM=''

# find all files and write them to a file
find . -name "$LOGMATCH" > temp


cat ./temp | while read file_line
do
ARGUM=$file_line' '$ARGUM
echo $ARGUM
done

echo "final string contains"
echo $ARGUM
echo "nothing"

----------------------------

I was hoping that it will read a line one at the time and add to the variable ARGUM. It does exactly that within the loop. Yet, when it gets out the loop, last line
echo $ARGUM
return nothing

output looks like this
./log/auditTrail-02022007.10.out
./log/auditTrail-02022007.14.out ./log/auditTrail-02022007.10.out
./log/auditTrail-02022007.7.out ./log/auditTrail-02022007.14.out ./log/auditTrail-02022007.10.out
./log/auditTrail-02022007.1.out ./log/auditTrail-02022007.7.out ./log/auditTrail-02022007.14.out ./log/auditTrail-02022007.10.out
final string contains

nothing


-----------------------
I thought that variable ARGUM is global everywhere in the script.
What am I missing?

Thank you in advance
Reply With Quote
Forum Sponsor
  #2  
Old 02-02-2007
Registered User
 

Join Date: Jan 2007
Posts: 366
The while runs in a subshell.

So all changes made to ARGUM are gone when the loop is done
When you change it into"

while read file_line
do
ARGUM=$file_line' '$ARGUM
echo $ARGUM
done < cat ./temp

it will work fine

Typical behavior for Linux systems or the bash shell.
Reply With Quote
  #3  
Old 02-02-2007
Registered User
 

Join Date: Dec 2006
Posts: 31
Thank you for the reply.
I tried, but now am getting

syntax error: `./temp' unexpected

Last edited by arushunter; 02-02-2007 at 04:20 PM.
Reply With Quote
  #4  
Old 02-02-2007
reborg's Avatar
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 3,590
remove the cat.
Reply With Quote
  #5  
Old 02-02-2007
Registered User
 

Join Date: Jan 2007
Posts: 366
Indeed, the "cat" shouldn't be there, I copied/pasted a little too fast
Reply With Quote
  #6  
Old 02-02-2007
Registered User
 

Join Date: Dec 2006
Posts: 31
thank you again. I did remove "cat"
and my while loop look as

while read file_line
do
ARGUM=$file_line' '$ARGUM
done < ./temp

echo $ARGUM

Yet, I am still getting emty output.
echo @ARGUM produces empty string.
Any other suggestings.
Thank you in advance
Reply With Quote
  #7  
Old 02-02-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,618
Switch ksh or bash. They won't do that to while loops.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:21 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0