CHAPTER 14. Building Scripts
In this chapter you will learn
Writing Shell scripts
Command aliases and substitutions
Pipes
Files, Descriptors and exit status
Structured commands (if, for, while, until, break, continue).
In this chapter, you are going to gain a comprehensive understanding of script building with examples. The key to script building is your familiarity with commands. Scripting is highly advantageous, as it can process more than one command, process the results, and feed one result to the other building a chain.
You already know how to execute multiple commands using ‘;’. For instance, pwd; cd ..; is a combination. What is it? It is a basic shell script!
Writing Shell Scripts
To write a shell script you simply need some understanding of Linux commands and a text editor. On Ubuntu, the default editor can be used (gedit). Let’s examine the components. 
A shell script starts with a comment at the top. 
#!/bin/shell_type
For instance, #!/bin/bash
This is a comment as it starts with the ‘#’. These lines are not interpreted by the shell! To comment, ‘#’ should be used. However, this specific line tells the shell what shell to use to execute this script on. We call #! Shebang.
The next part will be a set of commands with explanations. If you are familiar with programming, shell scripting is similar.
If we create a script with the following example, 
#!/bin/bash
who
#who command displays the account you are currently logged into and at what #time the user logged into the system.
pwd
it should work as expected.
Image: who commands in a script
00219.jpeg
You could also use the absolute or relative path.
The next thing you need to do is the changing of permissions. Why? Let’s create a file and examine the permissions.
00220.jpeg
According to the result, there are insufficient permissions to execute this file. The system treats itself as a script, but it hasn’t modified the permissions. It is up to the user.
To provide execution permissions, chmod command can be used.
chmod u+x Myscript
00221.jpeg
Once this step is complete, the script is ready to be executed.
00222.jpeg
If you want to display text guides, the output of environment variables and such, you can use the echo command. We have learned environment variables in a previous lesson.
Command Aliases
Using a command alias can save a lot of time. To define an alias, you have to do a simple assignment.
Syntax: alias=command(s)
Examples:
-       alias L=”ls -al”
-       alias search=grep
Command Substitution
It is also possible to use an output of a command as an input to a variable. This is a great feature to continue the script and execute it with dynamic data.
To achieve this, you must use $().
Example: user_name=$(USERNAME)
Example Script:
#!/bin/bash
user_name=$(who)
echo “Your username is :” $user_name
00223.jpeg
Creating Files
Another great feature is that you can output the results. You can even name the file according to your needs. This is explained in the next example.
#!/bin/bash
mydate=$(date +%Y%m%d)
echo “Today is :” $mydate
who > log.$mydate
ls -l log.$mydate
00224.jpeg
Here, we create a date and format it. %Y is the year in YYYY format. The month is in numeric format (e.g., 09) and the date is in a numeric format as well. The output is similar to YYYYMMDD. The output is taken into a variable. Then we echo the variable. Finally, we use the who command and redirect it’s input to a new file using the redirect operator >. The file name is created as the “log”.YYYYMMDD. This is how we create logs to keep records. If the script runs on the next day, the filename will be log.20190925.
Redirection is an extremely useful feature. Instead of using commands like touch, you can redirect the output to a file. Yet, you can even create files from scripts by using the touch command. For instance,
#!/bin/bash
mydate=$(date +%Y%m%d)
echo “Today is :” $mydate
touch log.$mydate#Creates a file and name the first part .lo and later using #our variable
who >> log.$mydate#Append the output of ‘who’ command to the file.
00225.jpeg
00226.jpeg
You can also do reverse the process we performed using the ‘>’ operator. This is known as input redirection. For instance, we could get an entry of a file to a new variable or a command directly. The following example explains this in detail.
#!/bin/bash
wc < log.20190924
# wc is a new command to you. It counts the number of lines, words and #characters.
00227.jpeg
Finally, we will look into the ‘<<’ operator. This is known as the inline input operator.
Example: wc << lastLine
00228.jpeg
As you notice, until the lastLine was entered as text input, it captured the user input. 
Other examples:
Redirecting error and standard output to the same file. You could use the following command.
Example: ls Test Test2 Test3> dirlist 2>&1
Here, we are trying to list the properties of the Test directory. Remember, ls command supports multiple file or directory names. It then lists each file’s or directory’s properties. Test and Test2 directories are on my desktop. Test3 directory does not exist. I am redirecting the output of this command as well as the error into a new file called dirlist.
00229.jpeg
Standard File Descriptors
We are revisiting the file descriptors again.
Standard Input - 0: STDIN
Standard Output – 1: STDOUT
Standard Error - 2: STDERR
The first is standard input, the next are standard output and standard error. The numerical values are file descriptors.
In addition to these techniques, pipes are extensively used in scripts.
Pipes
Piping is useful when an output of a command is required as an input of another. This is something we have learned so far. The operator used is ‘|’.
Example:
#!/bin/bash
cat employee.txt | sort -nr -k 2
#Here we redirect the file entry we obtain to the sort function. It sorts by numeric #values, in reverse order and sort by column id (2nd column).
00230.jpeg
Mathematical Operations in Script Building
Any script can be used to perform certain calculations. There are some built-in commands in Linux. We are looking into the command expr. This stands for the expression. These are the operators for expr command.
00231.jpeg
Image: expr operators
00232.jpeg
Examples:
00233.jpeg
00234.jpeg
Rather than using the expr command, it is more convenient to use brackets.
#!/bin/bash
var1=2
var2=6
var3=3
var4=$(expr $var1 \* $var2)
echo $var4
var5=$[ ($var1 * $var2)/$var3]
echo $var5
00235.jpeg
The Bash Calculator
When you want to perform more complex mathematical functions, there is a built-in bash tool to perform this. It is the bash calculator. The command is bc.
00236.jpeg
00237.jpeg
The bash calculator supports the following.
-       Numerical values (integer and floating-point numbers).
-       Variables.
-       Expressions.
-       Functions.
-       Statements (i.e., if-then-else).
-       Comments.
In the last line, you can see a problem. 8.45/5 = 1. This is inaccurate unless it is rounded up. The problem here is the number of decimal places. It is handled by a built-in variable known as scale. To receive the intended output, we need to modify the scale.
00239.jpeg
00240.jpeg
The bc command supports variables. The following example uses variables instead of numerical values.
The next important task is to get bc working in a script. In this case, we need to depend on pipes. 
#!/bin/bash
var1=10
var2=2
var3=$(echo "$var1 ^ $var2" | bc)
echo "The answer is: "$var3
00242.jpeg
Another example using the scale option.
#!/bin/bash
var1=10
var2=3
var3=$(echo " scale=3; $var1 / $var2" | bc)
echo "The answer is: "$var3
00243.jpeg
The next example will be looking into how the inline input operator can be used with the bc command in a script.
#!/bin/bash
var1=8
var2=1.75
var3=3
varF=$(bc << EOF # EOF stands for End of File
scale = 4
v1 = ( $var1 * $var2)
v2 = ($var1 /  $var3)
v1 + v2
EOF
)
echo The final calculation is: $varF
00244.jpeg