LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 04-13-2010, 09:13 AM   #1
idaham
LQ Newbie
 
Registered: Aug 2009
Posts: 26

Rep: Reputation: 0
Variables in text file, how to get their values when printing out the text?


Hi!
I'm doing some bash-scripting and want to be able to print some text (just plain text) files into the new bash-scripts, created within a loop. Here's a short example of what I do:

Code:
############### main bash script ##################
#!/bin/bash 

##Filename
variable1=10

for ((j=0;j<=40;j+=1))
do
  ## Create another bash-script
  echo "#!/bin/bash" >> bash_script_$j
  ... some stuff...
  cat file1.txt >> bash_script_$j
  ... some more stuff...
done
where the text file (file1.txt) I want to print in the the new bash script looks something like:
Code:
############### file1.txt ##################
...some stuff...
logsave log some_program($variable1)
mv output_$j folder_$j/
... some more stuff...
I.e, the text file contains variables such as "$j", "$variable1" etc that are undefined. Doing the above works for creating new bash scripts (bash_script_1 - bash_script_40) but the variables are not determined. I would like, if possible, to somehow print the text in file1.txt into the new bash-scripts with the variables determined, i.e:
Code:
############### bash_script_1 ##################
...some stuff...
logsave log some_program(10)
mv output_1 folder_1/
... some more stuff...
The text files I read are quite extensive so I would really prefer not having to paste them into the FOR-loop directly. Does anyone have any suggestions? Thanks!
Ida
 
Old 04-13-2010, 09:36 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Presuming you call the scripts created on the fly, could you pass the variable values on the command line?
Code:
#!/bin/bash 

##Filename
variable1=10

for ((j=0;j<=40;j+=1))
do
  ## Create another bash-script
  echo "#!/bin/bash" >> bash_script_$j
  echo 'j=$1' >> bash_script_$j 
  ... some stuff...
  cat file1.txt >> bash_script_$j
  ... some more stuff...
  bash_script_$j $j
done
Alternatively, they could get the value of $j from their own name
Code:
j=${0##*_}
 
Old 04-14-2010, 04:28 AM   #3
idaham
LQ Newbie
 
Registered: Aug 2009
Posts: 26

Original Poster
Rep: Reputation: 0
Hi!

I was able so solve the problem using the "sed" command:

Code:
sed -e s#'$variable1'#$variable1#g -e s#'$j'#$j#g file1.txt >> bash_script_$j
/Ida
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading variables from a text file in C++ Mork2k4 Programming 7 09-09-2009 10:54 AM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 03:36 PM
Reading a CSV text file and storing the values in Oracle Database table shafi2all Linux - Newbie 3 04-17-2008 01:19 PM
Read variables from a text file in Bash jakev383 Linux - General 5 12-20-2006 08:29 AM


All times are GMT -5. The time now is 07:38 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration