LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Is it possible to create a bash script to create multiple new bash scripts (https://www.linuxquestions.org/questions/linux-newbie-8/is-it-possible-to-create-a-bash-script-to-create-multiple-new-bash-scripts-4175450690/)

Batistuta_g_2000 02-18-2013 01:51 PM

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....

suicidaleggroll 02-18-2013 02:00 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


Batistuta_g_2000 02-18-2013 02:20 PM

Quote:

Originally Posted by suicidaleggroll (Post 4894588)
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


suicidaleggroll 02-18-2013 02:28 PM

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.

Batistuta_g_2000 02-18-2013 02:30 PM

Quote:

Originally Posted by suicidaleggroll (Post 4894603)
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!

Habitual 02-18-2013 03:45 PM

Quote:

Originally Posted by suicidaleggroll (Post 4894603)
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. :)

jpollard 02-19-2013 11:42 AM

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


All times are GMT -5. The time now is 07:33 PM.