LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 02-18-2013, 01:51 PM   #1
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Rep: Reputation: Disabled
Is it possible to create a bash script to create multiple new bash scripts


Just wondering if is possible to create a bash script to create multiple new bash scripts.
i.e, run one script which creates a backup, restore, config and setup script (then runs the setup script)?

Just wondering how it is possible to get around issues like the shebang being repeated etc....
 
Old 02-18-2013, 02:00 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Sure, what problems are you facing? The shebang being repeated doesn't matter, because "#" is a comment in a bash script. It only has the special meaning of a "shebang" when it's the first line in the file.

Here's a self-replicating script:
Code:
#!/bin/bash

rm -f newscript.sh

while read line; do
echo "$line" >> newscript.sh
done < $0

chmod +x newscript.sh
exit

Last edited by suicidaleggroll; 02-18-2013 at 02:02 PM.
 
Old 02-18-2013, 02:20 PM   #3
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
Sure, what problems are you facing? The shebang being repeated doesn't matter, because "#" is a comment in a bash script. It only has the special meaning of a "shebang" when it's the first line in the file.

Here's a self-replicating script:
Code:
#!/bin/bash

rm -f newscript.sh

while read line; do
echo "$line" >> newscript.sh
done < $0

chmod +x newscript.sh
exit

Ok this will probably make me crazy but will try to work through it, just want to get my head around the logic and commands - so I want to create a file called "newtest" which when ./newtest runs will create a script called "newtest2" which chmods and runs it to echo "it worked".

Question 1 - how to create a script to create and write (vi) to a new script? (confused)
Question 2 - If ctl-v followed by escape is correct - then i to insert ane ctl-v x: to save and exit, then chmod and run - Is this order even correct yet alone the syntax???
Question 3 - Is it worth it? should i just keep 5/6 separate scripts???
Code:
#!/bin/bash
touch newtest2
vi newtest2

^[i
#!/bin/bash
echo "it workd"
^[:x
 chmod 755 newtest2
./newtest2
 
Old 02-18-2013, 02:28 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Just use echo with I/O redirection like I did in the post you quoted:

Code:
#!/bin/bash

echo "#!/bin/bash" > newtest2
echo 'echo "it worked"' >> newtest2

chmod +x newtest2
./newtest2
Why do you need 5-6 scripts though? You could just use one script with command line arguments to do what you want. For example
Code:
./script setup
./script backup
./script restore
You can grab the first command line argument with "$1", then use a case statement to figure out and act on whatever operation the caller requested.

Last edited by suicidaleggroll; 02-18-2013 at 02:31 PM.
 
Old 02-18-2013, 02:30 PM   #5
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
Just use echo with I/O redirection like I did in the post you quoted:

Code:
#!/bin/bash

echo "#!/bin/bash" > newtest2
echo 'echo "it worked"' >> newtest2

chmod +x newtest2
./newtest2
Genius...thanks a million, there may be hope yet!
 
Old 02-18-2013, 03:45 PM   #6
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
Just use echo with I/O redirection like I did in the post you quoted:

Code:
#!/bin/bash

echo "#!/bin/bash" > newtest2
echo 'echo "it worked"' >> newtest2

chmod +x newtest2
./newtest2
Why do you need 5-6 scripts though? You could just use one script with command line arguments to do what you want. For example
Code:
MINPARAMS=1

if [ -n "$1" ]
then
echo "#!/bin/bash" > $1
chmod 700 "$1"
fi 

if [ $# -lt "$MINPARAMS" ]
then
  echo Usage: `basename $0` /path/to/script.sh
fi  
exit 0
it only makes one file, but feel free to modify away.
 
1 members found this post helpful.
Old 02-19-2013, 11:42 AM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
The topic is shell archives (shar files) reference http://en.wikipedia.org/wiki/Shar
 
  


Reply



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] Run multiple bash and php scripts from a bash script charu Programming 5 07-26-2011 02:40 AM
[SOLVED] BASH: create user and password on multiple machines hobbes80 Programming 7 08-11-2010 10:15 AM
Create BASH script to process multiple inputs on read davisbase Linux - Newbie 5 05-26-2010 11:57 PM
How do I create multiple variables from a list in Bash? Passions Programming 12 05-23-2009 05:23 PM
create multiple directories in bash? Count Zero Programming 19 07-06-2008 08:34 AM

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

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