Bash 09 – Conclusion

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
340
Reaction score
367
Credits
11,754
Over the last eight articles, we have covered all the information for making Bash scripts. The rest is up to you.

In this article, I hope to go over some concepts again and help you set up a helpful script.

Previous articles: https://www.linux.org/forums/shell-command-line.181/

Overview

In the first article, I covered the fact of creating a file to use for making a script. If you want to do it right, you will create a file with all the basic information.

We can automate this easily. Creating the initial file, filling it with the basic initial information and making it executable for the current user.

Let’s get to work.

Option 1

The first way we can do to set up a basic script file is to create one. I will create a file named ‘base.sh’ and place it in my Home Folder. The ‘base.sh’ file will be something like:

#!/bin/bash
# Author: Jarret Buse
# Date Created:
# Date Modified:
# Description:
# Usage:


This is extremely basic and has nothing filled in but my name.

We just create another script that will copy this one to the proper place with the new name. Once copied, we can set it to be executable. The script can be:

#!/bin/bash
# Author: Jarret Buse
# Date Created: 07/07/2022
# Date Modified: 07/07/2022
# Description: Script to create new empty script files
# Usage: Script name followed by location and script name.sh

cp $HOME/scripts/base.sh $1


NOTE: Be aware that if we create a base file and make it executable, a new file will be executable when it is copied.

A line could be added to the end to make the file executable if needed.

chmod +x $1

NOTE: Keep in mind, if the script file is to be copied to a folder in the $PATH, then you will most likely need to use ‘sudo’.

This is a very basic option for creating a new script file.

Option 2

This script maker will create a file and fill in most of the information. The ‘Author’ will be taken from the current user that is logged in. The ‘Date Created’ and ‘Date Modified’ are taken from the ‘Date’ command with the format set to ‘MM-DD-YYYY’. Since the file is being created from scratch, it is necessary to add the executable permission to the file when done.

The script requires you to include a path and filename for the new script.

#!/bin/bash
# Author: Jarret Buse
# Date Created: 07/07/2022
# Date Modified: 07/07/2022
# Description: Script to create new empty script files. Will fill in Date from system and name from current user.
# Usage: Script name followed by location and script name.sh

touch $1
date1=`date +%m-%d-%Y`
echo "#!/bin/bash" >> $1
echo "#Author: $USER" >> $1
echo "#Date Created: $date1" >> $1
echo "#Date Modified: $date1" >> $1
echo "#Description: " >> $1
echo "#Usage: " >> $1
chmod +x $1


Once the file is created, you can edit it to fill in the rest of the information.

The double greater-than symbols cause the data echoed to the file to be appended to the information already in the file.

If you wanted, the ‘chmod’ line can be made to ‘chmod 744 $1’, instead of just adding executable permissions.

Option 3

In this example, we can accept all items from the command-line as a parameter.

The script follows and is described in the ‘Description’ section.

#!/bin/bash
# Author: Jarret Buse
# Date Created: 07/07/2022
# Date Modified: 07/07/2022
# Description: Script to create new empty script files. Uses parameters:
# -f = path and filename
# -a = author name
# -t = date(time)
# -d = description
# -u = usage
#
# Usage: Script name followed by location and script name.sh, then the parameters (-a, -t, -d, -u)

date1=`date +%m-%d-%Y`
while getopts ":f:a:t:d:u:" opt; do
case $opt in
f) file1=${OPTARG};;
a) author=${OPTARG};;
t) date1=${OPTARG};;
d) description=${OPTARG};;
u) usage=${OPTARG};;
esac
done

touch $file1
echo "#!/bin/bash" >> $file1
echo "# " >> $file1
echo "#Author: $author" >> $file1
echo "#Date Created: $date1" >> $file1
echo "#Date Modified: $date1" >> $file1
echo "#Description: $description" >> $file1
echo "#Usage: $usage" >> $file1
chmod +x $file1


I did not specify that ‘getopts’ would handle errors by omitting the leading colon. You could add this if you want it.

I specified to get the system date at the first line that is executed. In the case that a date is not specified, the system date is used.

When getting the system date, you can set the ‘Author’ from the current User as we did previously.

If no ‘File Name’ is specified, we can generate an error, since it is mandatory.

The ‘Description’ and ‘Usage’ parameters are unnecessary.

Conclusion

Try one of these scripts when creating a shell script file. Place the script in your ‘PATH’ so it is executable from anywhere on the system.

These kinds of scripts can be handy. Over time, you can tweak the script to suit your needs better than these that are generally in use.
 
Last edited by a moderator:

Members online


Latest posts

Top