Linux Shell Programming Guide
Linux Shell Programming Guide
Introduction
■ Shell is an environment in which we can run our commands, programs, and shell
scripts.
■ It gathers input from you and executes programs based on that input. When a
program finishes executing, it displays that program's output.
■ - kernal = core of a computer’s operating system
■ - terminal = text input/output environment
■ - shell = command line interpreter
■ - shell scripts = commands written in a file
[Link]
Shells Available in Unix
- C shell (csh)
- TC shell (tcsh)
- Bourne shell (sh)
- Korn shell (ksh)
- Bourne Again Shell (bash)
Bourne Again Shell (bash)
Bash is the default shell in most Linux [Link] offers functional improvements
over sh for both programming and interactive use.
[Link]
Special Characters
# [Link] beginning with a # (with the exception of #!) will not be executed.
# This line is a comment.
; Command separator. Permits putting two or more commands on the same line.
echo hello ; echo world
;; Terminator in a case option
case "$variable" in
abc) echo "\$variable = abc" ;;
xyz) echo "\$variable = xyz" ;;
esac
` command substitution. The `command` construct makes available the output of
command for assignment to a variable
[Link]
Special Characters
[Link]
Getting help
[Link]
man pages
The man command shows detailed manuals for each command. These are referred to as “man
pages.”
It provides a detailed view of the command which includes NAME, SYNOPSIS, DESCRIPTION,
OPTIONS, EXIT STATUS, RETURN VALUES, ERRORS, FILES, VERSIONS, EXAMPLES, AUTHORS ..etc
man pwd
[Link]
Linux file system structure
[Link]
Linux home page structure
[Link]
Commands for navigating the Linux file system
[Link]
Commands for navigating the Linux file system
★ cat – read and concatenate data from the file and gives their content as output.
cat filename read single filename
cat file1 file2 show contents of file1 and file2
cat -n file show contents of fille with preceding line number
cat -s file suppress repeated empty lines in output
★ cp - copy fiiles or directories from source to destination
cp Src_file Dest_file copy the contents of src_file to the dest_file
cp Src_file1 Src_file2 Src_file3 Dest_directory copies each source file to the destination directory
cp -R Src_directory Dest_directory copies all files of the source directory to the destination directory, creating any files or directories
needed
★ mv - move fiiles or directories from source to destination
mv Src_file Dest_file move the file src_file to the dest_file
mv -i Src_file Dest_directory ask the user for confirmation before moving a file that would overwrite an existing file,
mv -f Src_directory Dest_directory overwrite the destination file forcefully and delete the source file.
[Link]
Commands for navigating the Linux file system
★ mkdir - create multiple directories at once as well as set the permissions for the directories
mkdir foldername
mkdir -m 777 foldername used to set the file modes
★ rmdir - remove empty directories from the filesystem
rmdir -p directory remove directory if it is empty
rmdir -p directory remove all child and parent empty directories
rmdir directorylist remove all directories
★ whereis - locates source/binary and manuals sections for specified files.
whereis perl List the directories where the perl source files, documentation, and binaries are stored.
whereis -b perl List only perl binaries are stored.
whereis -s perl List only perl sources are stored.
whereis -m perl List only perl manuals are stored.
[Link]
Piping and Redirection
Every program we run on the command line automatically has three data streams connected to
it.
STDIN (0) - Standard input (data fed into the program)
STDOUT (1) - Standard output (data printed by the program, defaults to the terminal)
STDERR (2) - Standard error (for error messages, also defaults to the terminal)
Piping and redirection is the means by which we may connect these streams between programs
and files to direct data in interesting and useful ways.
[Link]
Piping
Pipes allow you to funnel the output from one command into another where it will be used as the
input.
Pipes are unidirectional
The | operator feeds the output from the program on the left to the program on the right.
This will sort the given file and print the unique values only.
sort [Link] | uniq
[Link]
Redirection
[Link]
Informal Commands
[Link]
Informal Commands
[Link]
File Permissions
[Link]
File Permissions
using chmod command, we can set permissions (read, write, execute) on a file/directory for the
owner, group and the world.
chmod permission file
There are 2 ways to use the command
★ Absolute mode
★ Symbolic mode
[Link]
File Permissions
Absolute Mode
file permissions are not represented as characters but a three-digit octal number.
chmod 764 filename
‘764' absolute code says the following:
- Owner can read, write and execute Number Permission Type Symbol
- Usergroup can read and write 0 No permission ---
- World can only read 1 Execute --x
This is shown as 2 Write -w-
chmod -rwxrw-r- filename 3 Write + Execute -wx
4 Read r--
5 Read+Execute r-x
6 Read+Write rw-
7 Read+Write+Execute rwx
[Link]
File Permissions
Symbolic Mode
n the symbolic mode, you can modify permissions of a specific owner.
It makes use of mathematical symbols to modify the file permissions..
chmod o=rwx filename
setting others read+write+execute permissions
chmod g+x filename Operator Description
User Description
u user
g group
o other
[Link]
Comments
Lines beginning with a # (with the exception of #!) are comments and will not be
executed.
# This line is a comment
Comments may also occur following the end of a command.
echo “a comment will follow” # this is a comment
Comments may also follow whitespace at the beginning of a line.
# This is also a comment
a quoted or an escaped # in an echo statement does not begin a comment.
echo “hello #This is not a comment”
[Link]
Variables
[Link]
Operators
Assignment Operator
= is the all-purpose assignment operator, which works for both arithmetic and string
assignments.
var=27
category=minerals # No spaces allowed after the "="
Logical Operators
! NOT
&& AND
|| OR
[Link]
Operators
Arithmetic Operators
+ plus
- minus
* multiplication
/ division
** exponentiation
% mod (returns the remainder of an integer division operation)
+= plus-equal (increment variable by a constant) [38]
-= minus-equal (decrement variable by a constant)
*= times-equal (multiply variable by a constant)
/= slash-equal (divide variable by a constant)
%= mod-equal (remainder of dividing variable by a constant)
[Link]
Operators
Relational Operators
-eq equal to
-nt not equal to
-gt greater than
-lt lessthan
-ge greater than or equal to
-le less than or equal to
< is less than (within double parentheses) (("$a" < "$b"))
<= is less than or equal to (within double parentheses) (("$a" <= "$b"))
> is greater than (within double parentheses) (("$a" > "$b"))
>= is greater than or equal to (within double parentheses) (("$a" >= "$b"))
[Link]
Quoting
[Link]
read
Reads the value of a variable from stdin, that is, interactively fetches input from the
keyboard.
echo "Enter the value of variable 'var1': "
read var1
echo
prints (to stdout) an expression or variable
echo Hello
echo $a
An echo requires the -e option to print escaped characters.
Normally, each echo command prints a terminal newline, but the -n option suppresses
this.
[Link]
Conditional Commands
if can test any [Link] the value of a variable from stdin, that is, interactively fetches input from the keyboard.
if [ condition ]
then
command1
else
command2
fi
elif is a contraction for else if. The effect is to nest an inner if/then construct within an outer one.
if [ condition1 ]
then
command1
elif [ condition2 ]
then
command4
else
default-command
fi
[Link]
Conditional Commands
The case construct is the shell scripting analog to switch in C/C++. It permits branching to
one of a number of code blocks, depending on condition tests.
case "$variable" in
"$condition1" )
command...
;;
"$condition2" )
command...
;;
[Link]
iterative commands
A loop is a block of code that iterates [52] a list of commands as long as the loop control
condition is true.
for arg in [list]
do
command(s)...
done
This construct tests for a condition at the top of a loop, and keeps looping as long as that
condition is true (returns a 0 exit status)..
while [ condition ]
do
command(s)...
done
[Link]
break and continue
[Link]
expr
[Link]
bc
Bash can't handle floating point calculations, and it lacks operators for certain important
mathematical functions.
echo "scale=2; 2/3" | bc
.66
[Link]
grep
searches a file for a particular pattern of characters and displays all lines that contain
that pattern.
case insensitive search
grep -i “Word” [Link]
display count of matches
grep -c “Word” [Link]
checking whole word only
grep -w “Word” [Link]
[Link]
arrays
[Link]