LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   script output name from a variable (https://www.linuxquestions.org/questions/linux-newbie-8/script-output-name-from-a-variable-4175415861/)

darkego 07-10-2012 04:38 AM

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!

catkin 07-10-2012 06:14 AM

One solution
Code:

while read code
do
out=$code
( <statements writing to stdout> ) >> $out.txt
done < input


pixellany 07-10-2012 06:19 AM

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.

grail 07-10-2012 09:54 AM

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.

darkego 07-10-2012 10:12 AM

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!


All times are GMT -5. The time now is 02:40 PM.