Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Shell script that generates another shell script

Shell Programming and Scripting


Tags
emitting shell commands, variable evaluation

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 10-15-2013
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 10 November 2016, 10:40 AM EST
Location: France
Posts: 2,948
Thanks: 82
Thanked 630 Times in 601 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 Unix and Linux 10-15-2013
pravin27 pravin27 is offline Forum Advisor  
Advisor
 
Join Date: Sep 2009
Last Activity: 7 September 2016, 8:47 AM EDT
Location: ./India/Bangalore
Posts: 1,271
Thanks: 70
Thanked 299 Times in 290 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 Unix and Linux 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 Unix and Linux 10-15-2013
sea sea is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 14 October 2016, 2:49 PM EDT
Location: Swissh
Posts: 1,285
Thanks: 256
Thanked 225 Times in 208 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 Unix and Linux 10-15-2013
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 5 December 2016, 4:09 AM EST
Location: San Jose, CA, USA
Posts: 9,725
Thanks: 459
Thanked 3,320 Times in 2,840 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 Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 10:36 AM.