Shell script that generates another shell script | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Shell script that generates another shell script

Shell Programming and Scripting


Tags
emitting shell commands, variable evaluation

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-15-2013
Yongfeng Yongfeng is offline
Registered User
 
Join Date: Oct 2013
Last Activity: 3 February 2014, 2:28 AM EST
Posts: 8
Thanks: 7
Thanked 0 Times in 0 Posts
Bug Shell script that generates another shell script

If there's a file called example.txt with contents:
Foo
Bar
Baz
Goo

then I need to generate a shell script that has commands to reconstruct example.txt on another machine:


Code:
echo "Foo" >> example.txt
          echo "Bar" >> example.txt
          echo "Baz" >> example.txt
          echo "Goo" >> example.txt

I use for i in * to search a directory and conduct the above operation on every file that's not binary. Suppose i points to example.txt now, as the above example shows. I used sed to extract the contents of the files line by line. The question is how to use the result extracted by sed as the argument of echo?

I tried:

Code:
number=`wc -l < $i`
          count=1
          while [ $count -le $number ]
          do
          tmp=`sed -n "$count,1p" $i`
          echo ' echo "$tmp" >> $i ' >> bundle.sh
          done

Here tmp is the content of each line, but $tmp didn't evaluate in echo.

Actually the purpose of doing so is to transfer a directory containing various files and subdirectories which also contain files to another machine through simple email. Only bundle.sh needs to be transferred. So when the shell script bundle.sh is run on the other machine, the whole directory will be constructed there. I'm writing a script that can generate the bundle.sh script. And of course this needs to be done recursively since there're subdirectories, but ignore it for now. Thanks.

Here is a url to the full description of the task:
http://stackoverflow.com/questions/1...g-shell-script

Last edited by Yongfeng; 10-15-2013 at 05:18 AM..
Sponsored Links
    #2  
Old 10-15-2013
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 28 October 2014, 9:25 AM EDT
Location: France
Posts: 2,930
Thanks: 81
Thanked 624 Times in 596 Posts
Instead of generating a file that regenerate the file, why don't you just generate a copy of the file ?

If you want to copy a directory with subtree, see
tar command as well as cp or scp command (see -R and -r options)
The Following User Says Thank You to ctsgnb For This Useful Post:
Yongfeng (10-15-2013)
Sponsored Links
    #3  
Old 10-15-2013
pravin27 pravin27 is offline Forum Advisor  
Advisor
 
Join Date: Sep 2009
Last Activity: 10 October 2014, 7:04 AM EDT
Location: ./India/Banglore
Posts: 1,207
Thanks: 53
Thanked 270 Times in 263 Posts
How about this ?

Code:
$ cat test123
foo
bar
baz
goo
foo baar


Code:
$ cat p33.sh
#!/bin/sh

i=test123
number=`wc -l < $i`
count=1
while [ $count -le $number ]
do
     tmp=`sed -n "$count,1p" $i`
     echo "echo $tmp >> $i" >> bundle.sh
     count=$(($count + 1))
done


Code:
$ cat bundle.sh
echo foo >> test123
echo bar >> test123
echo baz >> test123
echo goo >> test123
echo foo baar >> test123

The Following User Says Thank You to pravin27 For This Useful Post:
Yongfeng (10-15-2013)
    #4  
Old 10-15-2013
Yongfeng Yongfeng is offline
Registered User
 
Join Date: Oct 2013
Last Activity: 3 February 2014, 2:28 AM EST
Posts: 8
Thanks: 7
Thanked 0 Times in 0 Posts
actually it's a homework, so I'm supposed to do it using my own code. Thanks.

---------- Post updated at 10:00 AM ---------- Previous update was at 10:00 AM ----------

thanks a lot.

---------- Post updated at 02:35 PM ---------- Previous update was at 10:00 AM ----------

hi, could you pls explain why there shouldn't be a double quote in $tmp and why count=$(($count + 1)) should be used to increase count? many thanks.
Sponsored Links
    #5  
Old 10-15-2013
sea sea is online now
Registered User
 
Join Date: Sep 2013
Last Activity: 21 November 2014, 10:57 AM EST
Location: Swissh
Posts: 428
Thanks: 69
Thanked 62 Times in 61 Posts
I'd rather use either: ((count++)) OR let count++
hth

EDIT:
The double brackets sends count++ to subshell, while let does it arithmeticly (AFAIK)
Sponsored Links
    #6  
Old 10-15-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 November 2014, 3:24 AM EST
Location: San Jose, CA, USA
Posts: 5,074
Thanks: 195
Thanked 1,698 Times in 1,441 Posts
Homework items can only be posted in the Homework and Classwork Forum and must include the completely filled out template required when posting homework.

This thread is closed.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Correct shell script to Call One shell script from another shell script Vineeta Nigam Shell Programming and Scripting 5 12-07-2012 03:37 PM
call another shell script and pass parameters to that shell script sunrexstar Shell Programming and Scripting 2 11-29-2010 03:53 PM
How to use ssh execute other shell script on other host (shell script include nohup)? orablue Shell Programming and Scripting 1 11-18-2010 09:46 PM
need help creating a shell that generates another shell adshocker Shell Programming and Scripting 6 06-04-2009 02:57 AM
invoking a shell script inside cgi shell script smriti_shridhar Shell Programming and Scripting 2 07-09-2008 02:50 AM



All times are GMT -4. The time now is 12:18 PM.