Basics of Shell Scripting
Basics of Shell Scripting
These permissions are not granted by default on a newly created file but you can
do it with the following command
chmod +x script.sh
You can also run the script without setting the permissions using sh or bash
Always give script files a .sh extension so you know that it is a shell script (Not
required)
Start with a script that simply outputs the mailbox file to the terminal
#!/bin/bash
#basic_script1.sh
cat /var/spool/mail/geeko
The first line tells Linux kernel to pass this file to the bash shell to be executed
The last line uses the cat command to open the mailbox file and print it to the
terminal
#!/bin/bash
#basic_script3.sh
echo -e “Welcome to the Request tracker. \nThe following are open requests:\n”
The echo command is used to output text, which is enclosed in double quotes, to
the terminal
\\ - Outputs a backslash
\b - Outputs a backspace
\n - Outputs a new-line
Variables:
The string “geeko” is assigned to the variable a which is then used in the echo
command
When you assign a variable just use the name of the variable
When you access the data of a variable use the $ before the variable
name
When you assign data to a variable there can be no spaces between the
variable name, the = and the data
geeko@linux-rwke:~/Shell_Scripting> ./variables_1.sh
By default a variable can hold any kind of data but it is possible to limit a variable
to a specific type using the declare command
Command Substitution:
Means that the output of a command is used in a shell command line or a shell
script
Example that uses the output of the command date to generate the output of
the current date
#!/bin/bash
#command_subs_1.sh
Instead of printing the output of a command to the screen with echo, it can also
be assigned to a variable
#!/bin/bash
#command_subs_2.sh
TODAY=`date +%m/%d/%y`
The output of date is assigned to the variable TODAY which is then printed to the
screen with echo.
Introduction:
This objective gives an overview of useful commands that can be used in shell
scripts and is intended as a reference
When combined with the here operator (<<) a good choice to output several
lines of text from a script
Use this command to cut out sections of lines from a file so only the specified
section is printed on standard output
Can specify single or several sections
root
bin
daemon
..
Example to take the output of ls and cut out everything from the thirty-fifth
character, pipe it to sort and have is sorted by file size
date
Same as +%Y-%m-%d
The grep and egrep Commands
Print the line along with leading and trailing context lines
To avoid having special characters in the seach patterns interpreted by the shell
enclose the pattern in quotation marks
file1:blurb
file2:Blurb
The sed program is a stream editor used from the command line, not
interactively
d: Delete
s: Substitute (replace)
p: Output line
a: Append after
The output normally goes to standard output and can also be redirected to a file
-f filename
Can specify a script file from which to read its editing commands
Sometimes need to specify exact line or lines to be processed and there are
special characters you can use
Used to compare values and to check for file and their properties, such as if it
exists, is executable, etc.
If the tested condition is true then test returns an exit status of 0 and if not true
returns an exit status of 1
Compare 2 files
Compare 2 integers
-eq Equal
Test strings
Combine tests