1) How to Build a Bash Script
Last updated
Was this helpful?
Last updated
Was this helpful?
$ cat /etc/shells
$ which bash
/usr/bin/bash
$ echo $SHELL
/bin/bash
IF bash is not the default shell then: $ chsh -s /bin/bash add password if required and then restart terminal
A "shell' is a program that interprets the commands you type in the terminal and passes it onto the Operating System. Its a way to issue commands to the computer.
Bash Shell = Bourne Again SHell (BASH) - 1979 Stephen Bourne developed the Bourne Shell which BASH is based upon is an IMPROVED version and people thought it was nice to call it the Bourne Again SHell :)
Most commonly used shell in Linux with broad set of features and faster than other shells
A script is a file containing commands for the shell. They automate tasks, save tine and increases reliability
Shebang
#! /usr/bin/bash or /bin/bash
To check what type a file is, lets create a file in ~/bash_course/first_script and add the shebang at the top, save and close
$ file first_script
-> it tells that its a Bourne-Again script.
Change the shebang to read #! /usr/bin/python3
$ file first_script
-> it tells that its a Python script
Actual Bash Commands itself:
Formatting rule - leave a line between the shebang and your 1st line of code
echo "This is my first script"
The Exit Statement - Tells the shell that the script has ended as the shell reads the script line by line By putting different codes for the exit statement can tell the shell if it has completed correctly or not etc Has codes from 0 - 255
exit 0
Notes [1] Out of range exit values can result in unexpected exit codes. An exit value greater than 255 returns an exit code modulo 256. For example, exit 3809 gives an exit code of 225 (3809 % 256 = 225).
Notes [2] An update of /usr/include/sysexits.h allocates previously unused exit codes from 64 - 78. It may be anticipated that the range of unallotted exit codes will be further restricted in the future. The author of this document will not do fixups on the scripting examples to conform to the changing standard. This should not cause any problems, since there is no overlap or conflict in usage of exit codes between compiled C/C++ binaries and shell scripts.
Change the permissions to the file:
$ chmod 755 my_1st_script -----> or you could do chmod +x my_1st_script --- All the same :)
And run the script:
$ ./my_first_script -----> the ./ means "in this directory"
This is the where we add professional touches to our scripts that include the following:
# /bin/bash
# Author: Wallis Short
# Date Created: 22 May 2021
#Last Modified: 22 May 2021
# Description:
# Prints "This is my first bash script"
# Usage ---->Tells the user how to actually use the script
# my_1st_script
echo "This is my first bash script"
exit 0
We need to change the permissions to 744 for security and only enable the owner to execute the rest can just read. The rwx - w means write or is able to 'edit' the file
Remember: If you want to give others 'execute' permissions, you also have to give them 'read' permissions otherwise the shell wont be able to read the file and thereby wont be able to execute it.
To see what's in our systems PATH run this command: echo "$PATH"
$ echo "$PATH"
ubuntu@ansible:~/bash_course$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Whenever we run an executable, the shell will look through all the files in each of the above folders looking for the command we have just run. If its not in there - we get an error. So if, for example, I'm in /home/ubuntu/.ssh folder and I run ./backup_script - I'll get an error as this script is not in any folders within my $PATH variable.
$ mkdir /home/ubuntu/bash_course/scripts Then move existing bash scripts into the new scripts folder
Edit the .profile
file in the users home directory and add the following line at the bottom:
export PATH="$PATH:$HOME/bash_course/scripts"
The dot profile (.profile) file is usually read when the system boots up so we need to reload the file by 2 ways:
We log off and then log back in again OR
We run the "source" command
source ~/.profilecheck
Lest check if it has loaded?
$ echo "$PATH"
We can see it has been added in - so now we can run the script from anywhere within the Linux file structure