As I think that is easier delete than write, I did a script to start scripts with the while loop for getopts already written. Then, if I don’t need to use arguments I just have to delete the block.
I called it mkscript.sh and I saved it in my /bin folder.
#!/bin/bash
nom=$1
echo "#!/bin/bash" > $nom
echo "" >> $nom
echo "# Default values
#variable_a=default_a
#variable_b=default_b"
echo "# uncomment to argument
#while getopts a:b: option
#do
#case \${option}
#in
#a) variable_a=\${OPTARG};;
#b) variable_b=\${OPTARG};;
#esac
#done" >> $nom
chmod 751 $nom
vim $nom
The way to use it is:
mkscript.sh name_of_the_new_script
Actually,
nom=$1
is another example of how to read arguments in bash. In this way you have to pass the arguments in order after the name of the script. Then $1 correspond to the first argument, $2 the second argument, ${10} the tenth argument, etc.
The last two commands make the script executable (with chmod; we can change privileges later) and open it in a text editor (vim).
chmod 751 $nom
vim $nom
When I run the script the result is another script already open to edit, like that:
#!/bin/bash
# Default values
#variable_a=default_a
#variable_b=default_b
# Uncomment to argument
#while getopts a:b: option
#do
#case ${option}
#in
#a) variable_a=${OPTARG};;
#b) variable_b=${OPTARG};;
#esac
#done
I only have to change or to add variables and, of course, write the rest of the script.
More