Basics of Shell Scripting

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Basics of Shell Scripting

Basic Rules of shell scripting:

A shell programming language is a powerful and complete programming


language

A shell script is an ASCII text file containing commands to be executed in


sequence

To allow this the following permissions need to be set on the file

 “r” (readable) and “x” (executable) for the user running it

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

 /bin/sh is a symbolic link to /bin/bash

 Example: sh script.sh or bash script.sh

The script directory must be in the user's search path

 Otherwise must be started using full path names

 A good way to do this is to create a /bin directory in the users home


directory and add it to the users PATH

Do this in the ~.bashrc

Example: export PATH=$PATH:~/bin

Always give script files a .sh extension so you know that it is a shell script (Not
required)

Using of Basic script elements:

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 second line is a comment since it starts with #

The last line uses the cat command to open the mailbox file and print it to the
terminal

Another improvement would be to add a start message to the script

#!/bin/bash

#basic_script3.sh

echo -e “Welcome to the Request tracker. \nThe following are open requests:\n”

cat /var/spool/mail/geeko | grep '#'

The echo command is used to output text, which is enclosed in double quotes, to
the terminal

 The option -e lets echo interpret backslash sequences

 The sequence \n is used to creates a line break (new-line) in the output

Backslash sequences that can be used with echo

\\ - Outputs a backslash

\a - Outputs an alert (beep tone)

\b - Outputs a backspace

\n - Outputs a new-line

\t - Outputs a horizontal tab

\v - Outputs a vertical tab

Variables and Command Substitution:

Variables:
The string “geeko” is assigned to the variable a which is then used in the echo
command

The important things are

 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

The output would look like this

geeko@linux-rwke:~/Shell_Scripting> ./variables_1.sh

Hello, my name is Geeko

Variables can contain strings and numbers as well

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

It is also possible to assign the output of a command to a variable or to use a


command directly where the output is needed

 Called a command substitution

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

echo “Today is `date +%m/%d/%y`”

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`

echo “Today is $TODAY”

The output of date is assigned to the variable TODAY which is then printed to the
screen with echo.

Useful commands in shell scripting:

Introduction:

This objective gives an overview of useful commands that can be used in shell
scripts and is intended as a reference

 Use the cat command

 Use the cut command

 Use the date command

 Use the grep and egrep command

 Use the sed command

 Use the test command

 The cat Command

When combined with the here operator (<<) a good choice to output several
lines of text from a script

Interactively, it is mostly run with a filename as an argument and prints to


standard output

 The cut Command

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

The command is applied to each line of text as available in a file or on standard


output

Use cut -f to cut out text fields

Use cut -c to work with specified characters

Use cut -d to specify a field separator

 A tab is the default

 The cut Command

Example to cut and print the first field to standard output

cut -d : -f1 /etc/passwd

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

ls -l somedir/ | cut -c 35- | sort -n

 The date Command

Use whenever have a need to obtain a date or time string

Output with no options

date

Fri Sep 03 14:18:12 CEST 2014

The output can be formatted using options

The -I option prints the date and time in ISO format

 Same as +%Y-%m-%d
The grep and egrep Commands

Used to search one or multiple files for certain patterns

The syntax is grep searchpattern filename ...

The output is all lines that contain the pattern

Many options are available to do things like

 Only print the line numbers

 Print the line along with leading and trailing context lines

Search patterns can be supplied as regular expressions

The regular grep has limited capabilities

Can use egrep or grep -E for more complex patterns

The regular expressions are in the standard regex syntax

To avoid having special characters in the seach patterns interpreted by the shell
enclose the pattern in quotation marks

tux@DA1:~> egrep (b|B)lurb file*

bash: syntax error near unexpected token |

tux@DA1:~> egrep “(b|B)lurb” file*

file1:blurb

file2:Blurb

 The sed Command

The sed program is a stream editor used from the command line, not
interactively

Does text transformations on a line-by-line basis

Can specify sed commands directly on the command line or in a special


command script loaded by the program

The syntax is sed editing-command filename


The available editing commands are single-character arguments such as

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

Each sed command must be preceded by an exact address or address range


specifying the lines to which the editing command applies

Options can be specified to influence the overall behavior of the program

-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

 $ stands for the last line

Command to print only lines 1 through 9

sed -n '1,9p' somefile

 The test Command

Exists as both a built-in command and as an external command

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

In shell scripts mainly used to declare conditions to influence the operation of


loops, branches and other statements

The test syntax is test condition

Test whether a file exists


-e File exists

-f File exists and is a regular file

-d File exists and is a directory

-x File exists and is an executable file

Compare 2 files

-nt Newer than

-ot Older than

-ef Refers to same inode (hard link)

Compare 2 integers

-eq Equal

-ne Not equal

-gt Greater than

-lt Less than

-ge Greater than or equal

-le Less than or equal

Test strings

test -z string True if string has 0 length

test string True if string has nonzero length

test string1 = string2 True if strings are equal

test string1 != string2 True if strings are not equal

Combine tests

test ! condition - True if condition is not true

test condition1 -a condition2 - True if both are true

test condition1 -o condition2 - True if either is true

You might also like