[SOLVED] Is it possible to create a bash script to create multiple new bash scripts
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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....
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.
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???
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.