LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-10-2012, 05:38 AM   #1
darkego
LQ Newbie
 
Registered: Jul 2012
Posts: 2

Rep: Reputation: Disabled
script output name from a variable


Hello people!!

Whith an script, I want to set the name of an output file employing the name of a variable: e.g.:

while read code
do
out=$code
done < input >> $out.txt

However, the name of the output file I got is '.txt'. But
if i do 'echo $out', i can confirm that the variable has the value i want.

I solved the issue by doing:

done < input >> temp.txt
cp temp.txt $out.txt
rm temp.txt

but this doesn't look so nice

any idea??

Thanks in advance!

Last edited by darkego; 07-10-2012 at 05:40 AM.
 
Old 07-10-2012, 07:14 AM   #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
One solution
Code:
while read code
do
out=$code
( <statements writing to stdout> ) >> $out.txt
done < input
 
Old 07-10-2012, 07:19 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I think that the file designated by the redirection gets opened before the loop starts. thus, the variable is not yet defined.
Some experiments will confirm this, but I think you've already proven it.
 
Old 07-10-2012, 10:54 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,031

Rep: Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202
pixellany has the right information. At the point of the loop starting the variable is blank and this will be when the file is opened for writing.
 
Old 07-10-2012, 11:12 AM   #5
darkego
LQ Newbie
 
Registered: Jul 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks for the replies and explanations!
Yes, the variable is not defined at that stage, so the variable $out must be defined before the loop or I have to adopt the catkin's suggestion
thanks!
 
  


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
[SOLVED] bash script mysql output variable comparison dominic.rsa Programming 2 12-02-2011 01:59 AM
shell script: saving output from command into a variable grcunning Linux - Newbie 7 01-26-2010 12:43 PM
passing awk output to $variable for shell script? NewnanNOC Programming 3 10-23-2008 03:08 PM
Script: put the output of a command into a variable. poincare999 Programming 4 11-04-2007 12:53 PM
bash script: how to assign an output (not the result) to a variable? Singing Banzo Programming 8 10-01-2006 07:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:42 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