Linux Commands and Shell Script Programming

www.getmyuni.com
Linux Commands and Shell Script Programming
www.getmyuni.com
History of Linux
Linux Kernel
Written in :
C, assembly
Initial release: 0.01 (17 September 1991)
Latest release: 3.18 (07 December 2014)
www.getmyuni.com
The Linux operating system


Runs many applications on a variety of different hardware
A multi-user and multitasking OS
www.getmyuni.com
Versions of Linux OS

Core component: Linux kernel
Kernel version determines version of the OS

New versions add capabilities or fix bugs

www.getmyuni.com
Before starting Linux commands and shell script
programming you must know




Kernel: The core component of the OS
Shell: Runs within the terminal
 BASH shell (Bourne Again Shell): the default shell
Terminal: Screen that allows you to log in
Processes: Process is any kind of program or task carried out by your
PC
www.getmyuni.com
What is Kernel?
Kernel is the heart of the Linux O/S. It manages resource of Linux O/S.
Resources means facilities available in Linux. For eg. Facility to store
data, print data on printer, memory, file management etc . Kernel decides
who will use this resource, for how long and when. It runs your programs
(or set up to execute binary files) It's Memory resident portion of Linux. It
performance following task :
I/O management

Process management

Device management

File management

Memory management
www.getmyuni.com
What is Linux Shell?
Computer understand the language of 0's and 1's called binary
language, In early days of computing, instruction are provided using
binary language, which is difficult for all of us, to read and write. So in
O/s there is special program called Shell. Shell accepts your instruction
or commands in English and translate it into computers native binary
language. This is what Shell Does for US. You type Your command and
shell convert it as. It's environment provided for user interaction. Shell is
an command language interpreter that executes commands read from
the standard input device (keyboard) or from a file. Linux may use one of
the following most popular shells (In MS-DOS, Shell name is
COMMAND.COM which is also used for same purpose, but it's not as
powerful as our Linux Shells are!)
Continued..
www.getmyuni.com
Shell Name
Developed By
Where
Remarks
BASH ( Bourne-Again
SHell )
Brian Fox and Chet
Ramey
Free Software Foundation
Most common shell in
Linux. It's Freeware shell.
CSH (C SHell)
Bill Joy
University of California
(For BSD)
The C shell's syntax and
usage are very similar to
the C programming
language.
KSH (Korn SHell)
David Korn
AT & T Bell Labs
TCSH
Ken Greer, Paul
Placeway, Christos
Zoulas, etc
Carnegie Mellon
University
TCSH is an enhanced but
completely compatible
version of the Berkeley
UNIX C shell (CSH).
Any of the above shell reads command from user (via Keyboard or
Mouse) and tells Linux O/s what users want. If we are giving commands
from keyboard it is called command line interface ( Usually in-front of $
prompt, This prompt is depend upon your shell and Environment that you
set or by your System Administrator, therefore you may get different
prompt).
NOTE: To find your shell type following command $ echo $SHELL
How to use Shell? To use shell (You start to use your shell as soon as
you log into your system) you have to simply type commands.
www.getmyuni.com
What is Processes?
Process is any kind of program or task carried out by your PC. For
example $ ls -lR , is command or a request to list files in a directory and
all subdirectory in your current directory. It is a process. A process is
program (command given by user) to perform some Job. In Linux when
you start process, it gives a number (called PID or process-id), PID starts
from 0 to 65535.
Why Process required?
Linux is multi-user, multitasking o/s. It means you can run more than two
process simultaneously if you wish. For e.g.. To find how many files do
you have on your system you may give command like $ ls / -R | wc -l
This command will take lot of time to search all files on your system. So
you can run such command in Background or simultaneously by giving
command like $ ls / -R | wc -l & The ampersand (&) at the end of
command tells shells start command (ls / -R | wc -l) and run it in
background takes next command immediately. An instance of running
command is called process and the number printed by shell is called
process-id (PID), this PID can be use to refer specific running process.
www.getmyuni.com
Linux Command Related with Process
For this purpose Use this Command Example To see currently running
process ps $ ps
To stop any process i.e. to kill process kill {PID} $ kill 1012
To get information about all running process ps -ag $ ps -ag
To stop all process except your shell kill 0 $ kill 0
For background processing (With &, use to put particular command and
program in background) linux-command &
$ ls / -R | wc -l &
NOTE that you can only kill process which are created by yourself. A
Administrator can almost kill 95-98% process. But some process can not
be killed, such as VDU Process.
www.getmyuni.com
Operating System Information
$ uname -a
Gives you operating system version
$ cat /proc/meminfo
Gives you memory information
www.getmyuni.com
Login
Users can login to Linux machines either locally or remotely. Linux
provides various ways to connect and following are few of them.
Console
Local: login console
Remote: SSH, Telnet, rsh, etc
www.getmyuni.com
Changing directories
pwd (print working directory)
Used to identify the current directory path
cd (change directory)
Used to move from one directory to another
www.getmyuni.com
ls command options
ls (list the files in the current working directory)
www.getmyuni.com
To create text file
cat > { file name }
$ cat > myfile
type your text
when done press
^D
NOTE: Press and hold CTRL key and press D to stop or to end file
(CTRL+D)
www.getmyuni.com
Displaying content of text files
cat command
Display the entire contents of a text file to the screen
tail command
By default, displays the last 10 lines (including blank lines) of a text file to
the terminal screen
Can also take a numeric option specifying a different number of lines to
display
-f, –output appended data as the file grows; -f, –follow, and –
follow=descriptor are equivalent
-n, –lines=N output the last N lines, instead of the last 10
Full syntax: tail -f -n 100 [PATH TO LOG FILE]
Example: tail -f -n 100 /app/AdminServer.log
www.getmyuni.com
The grep command
grep
Displays lines in a text file that match a common regular expression
www.getmyuni.com
Managing files and directories
mkdir command
Takes arguments specifying the absolute or relative pathnames of the
directories to create
The mv command requires two arguments at a minimum:
Source file/directory
Target file/directory
cp command
Command used to create copies of files and directories
rm command
Takes a list of arguments specifying the absolute or relative pathname of
files to remove
Remove all files in given directory/subdirectory. Use it very carefully.
rm -rf *
rmdir command
Command used to empty directories
www.getmyuni.com
User Logged in details
who
List who is currently logged on to the system
whoami
Report what user you are logged on as
www.getmyuni.com
Finding files
find command
Used to find files using various criteria
Searches the directory tree recursively, starting from a certain directory,
for files that meet criteria
Example:
Search for files to delete:
find /oradata -type f -name "*.arc" -mtime +90 -exec ls -l {} \;
Execute delete:
find /oradata -type f -name "*.arc" -mtime +90 -exec rm {} \;
To Change date and time:
find . -exec touch -t 201304261001 {} \;
www.getmyuni.com
Linking files
ln (link) command
Command used to create hard and symbolic links
To create hard link use the ln command with two arguments:
The existing file to hard-link
The target file that will be created as a hard link to the existing file
ln -s /path/to/file /path/to/symlink
www.getmyuni.com
File and directory permissions
chown (change owner) command
Used to change the owner and group owner of a file or directory
chgrp (change group) command
Change the group owner of a file or directory
Takes two arguments at a minimum:
The new group owner
The files or directories to change
Use the chmod command to change file permissions
The permissions are encoded as an octal number
www.getmyuni.com
To change file access permissions
u - User who owns the file
g - Group file owner
o - User classified as other
a - All other system user
+ Set permission
- Remove permission
r - Read permission
w - Write permission
x - Execute permission
chmod {u|g|o|a} {+|-} {r|w|x} {filename}
$ chmod u+x,g+wx,o+x myscript
NOTE: This command set permission for file called 'myscript' as User
(Person who creates that file or directory) has execute permission (u+x)
Group of file owner can write to this file as well as execute this file (g+wx)
Others can only execute file but can not modify it, Since we have not
given w (write permission) to them. (o+x).
www.getmyuni.com
Disk usage
df (disk free) command
The easiest method for monitoring free space
Example:
df -g
www.getmyuni.com
Pipes
Sends Standard Output of one command as Standard Input to another
command
Pipe
A string of commands connected by the “|” metacharacter
www.getmyuni.com
Common filter commands
www.getmyuni.com
The ps command
Most versatile and common utility that can view processes
Without arguments, displays processes running in the current shell
Options of the ps command
www.getmyuni.com
Sending signals to processes
kill signal
Type of signal sent to a process by the kill command
Different kill signals affect processes in different ways
kill command
Command that kills all instances of a process by command name
www.getmyuni.com
Scheduling commands
The at daemon (atd)
System daemon that executes tasks at a future time
Configured with the at command
The cron daemon (crond)
System daemon that executes tasks repetitively in the future
Configured using cron tables
www.getmyuni.com
Common at commands
www.getmyuni.com
crond scheduling commands
cron tables
A file specifying tasks to be run by the cron daemon
User cron tables and system cron tables
Six fields separated by space or tab characters
www.getmyuni.com
User cron table format
www.getmyuni.com
User cron table entry
www.getmyuni.com
Location of cron tables
/var/spool/cron
Stores user cron tables
/etc/cron.d
Contains additional system cron tables
/etc/crontab
Default system cron table
www.getmyuni.com
User cron tables
/etc/cron.allow
File listing all users who can use the cron command
/etc/cron.deny
File listing all users who cannot access the cron command
crontab command
Command used to view and edit user cron tables
www.getmyuni.com
The system cron table
Systems are scheduled to run commands during non-business hours
Commands may perform system maintenance, back up data, or run
CPU-intensive programs
Most commands are scheduled by the cron daemon from entries in the
system cron table /etc/crontable
www.getmyuni.com
Switch User Accounts
su <accountname>
switch user accounts. You will be prompted for a password. When this
command completes, you will be logged into the new account. Type exit
to return to the previous account
su
Switch to the root user account. Do not do this lightly
Note: The root user does not need to enter a password when switching
users. It may become any user desired. This is part of the power of the
root account.
sudo su -l (thats a lower “L”) makes you root
pbrun su
or
/usr/local/bin/pbrun -u [USER ID TO EMULATE] ksh
example: /usr/local/bin/pbrun -u mdmadmin ksh
www.getmyuni.com
What is pbrun command?
pbrun
Usage: pbrun [-v][-n][-p][-d option] [-h hostname]
[-u username] command [args ...]
The pbrun command is used to request that an application or command
be run in a controlled account. The user simply adds pbrun to the
beginning of the command line, for example:
pbrun backup /usr /dev/dat
pbrun su
or
/usr/local/bin/pbrun -u [USER ID TO EMULATE] ksh
example: /usr/local/bin/pbrun -u mdmadmin ksh
www.getmyuni.com
Creating user accounts
useradd command
Adds a user account to the system
www.getmyuni.com
Deleting user accounts
userdel command
Remove a user account
www.getmyuni.com
Managing groups
groupadd
Adds a group
groupmod
Modifies the name of a GID
groupdel
Deletes a group
www.getmyuni.com
Common compression utility
gzip
www.getmyuni.com
ftp command
ftp - Internet file transfer program
Syntax:
ftp [-pinegvd ] [host ]
The uses of the command ftp for remotely copying, renaming, and
deleting files.
ftp abc.xyz.edu
This command will attempt to connect to the ftp server at abc.xyz.edu. If
it succeeds, it will ask you to log in using a username and password.
Public ftp servers often allow you to log in using the username
"anonymous" and your email address as password. Once you are logged
in you can get a list of the available ftp commands using the help
function.
www.getmyuni.com
Introduction to the vi Editor
vi (pronounced vee-EYE, short for “visual”) provides basic text editing
capabilities. Three aspects of vi make it appealing. First, vi is supplied
with all UNIX systems. You can use vi at other universities or any
businesses with UNIX systems. Second, vi uses a small amount of
memory, which allows efficient operation when the network is busy. Third,
because vi uses standard alphanumeric keys for commands, you can
use it on virtually any terminal or workstation in existence without having
to worry about unusual keyboard mappings. As a point of interest, vi is
actually a special mode of another UNIX text editor called ex. Normally
you do not need to use ex except in vi mode.
www.getmyuni.com
A Brief vi Session
Starting vi
To start vi, enter:
vi filename RETURN
where filename is the name of the file you want to edit. If the file does not
exist, vi will create it for you.
You can also start vi without giving any filename. In this case, vi will ask
for one when you quit or save your work.
How to exit from vi (command mode)
:q <enter> is to exit, if you have not made any changes to the file
:q! <enter> is the forced quit, it will discard the changes and quit
:wq <enter> is for save and Exit
:x <enter> is same as above command
ZZ is for save and Exit (Note this command is uppercase)
The ! Character forces over writes, etc. :wq!
www.getmyuni.com
Introduction to Shell Programming
Shell program is series of Linux commands. Shell script is just like batch
file is MS-DOS but have more power than the MS-DOS batch file. Shell
script can take input from user, file and output them on screen. Useful to
create our own commands that can save our lots of time and to automate
some task of day today life.
Variables in Linux
Sometimes to process our data/information, it must be kept in computers
RAM memory. RAM memory is divided into small locations, and each
location had unique number called memory location/address, which is
used to hold our data. Programmer can give a unique name to this
memory location/address called memory variable or variable (Its a
named storage location that may take different values, but only one at a
time).
www.getmyuni.com
In Linux, there are two types of variable
1) System variables - Created and maintained by Linux itself. This type of
variable defined in CAPITAL LETTERS. You can see system variables by
giving command like $ set
2) User defined variables (UDV) - Created and maintained by user. This
type of variable defined in lower LETTERS.
www.getmyuni.com
System (BASH environment) variables
www.getmyuni.com
How to define User defined variables (UDV)
To define UDV use following syntax
Syntax: variablename=value
NOTE: Here 'value' is assigned to given 'variablename' and Value must
be on right side = sign For
e.g.
$ no=10 # this is ok
$ 10=no # Error, NOT Ok, Value must be on right side of = sign.
www.getmyuni.com
Rules for Naming variable name (Both UDV and
System Variable)
(1) Variable name must begin with Alphanumeric character or underscore
character (_), followed by one or more Alphanumeric character.
(2) Don't put spaces on either side of the equal sign when assigning
value to variable. For e.g.. In following variable declaration there will be
no error
(3) Variables are case-sensitive, just like filename in Linux.
(4) You can define NULL variable as follows (NULL variable is variable
which has no value at the time of definition)
(5) Do not use ?,* etc, to name your variable names.
How to print or access value of UDV (User defined variables)
To print or access UDV use following syntax
Syntax: $variablename
For eg. To print contains of variable 'unitname'
$ echo $unitname
www.getmyuni.com
How to write shell script
Now we write our first script that will print "Hello World" on screen. To
write shell script you can use in of the Linux's text editor such as vi or
mcedit or even you can use cat command. Here we are using cat
command you can use any of the above text editor. First type following
cat command and rest of text as its
$ cat > first
clear
echo "Hello World"
Press Ctrl + D to save. Now our script is ready. To execute it type
command
$ ./first
This will give error since we have not set Execute permission for our
script first; to do this type command
$ chmod +x first
$ ./first
First screen will be clear, then Hello World is printed on screen. To print
message of variables contains we use echo command.
www.getmyuni.com
How to Run Shell Scripts
Because of security of files, in Linux, the creator of Shell Script does not get
execution permission by default. So if we wish to run shell script we have
to do two things as follows
(1) Use chmod command as follows to give execution permission to our
script
Syntax: chmod +x shell-script-name OR chmod 777 shell-script-name
(2) Run our script as
Syntax: ./shell-script-name
For e.g.
$ ./first
Here '.'(dot) is command, and used in conjunction with shell script. The dot(.)
indicates to current shell that the command following the dot(.) has to be
executed in the same shell i.e. without the loading of another shell in
memory. Or you can also try following syntax to run Shell Script
$ bash first
OR
$ /bin/sh first
www.getmyuni.com
Commands Related with Shell Programming
echo [options] [string, variables...]
Displays text or variables value on screen.
Options
-n Do not output the trailing new line.
-e Enable interpretation of the following backslash escaped characters in
the strings:
\a alert (bell)
\b backspace
\c suppress trailing new line
\n new line
\r carriage return
\t horizontal tab
\\ backslash
www.getmyuni.com
The read Statement
Use to get input from keyboard and store them to variable.
Syntax: read variable1, variable 2,...variable N
www.getmyuni.com
if-then-fi for decision making in shell script
if-then-else-fi
Syntax:
if condition
then
condition is (true)
execute all commands up to elif statement
elif condition1
condition1 is (true)
execute all commands up to elif statement
else
None of the above condition,condtion1 are true
execute all commands up to fi
fi
www.getmyuni.com
for loop Syntax:
for { variable name } in { list }
do
execute one for each item in the list until the list is
not finished (And repeat all statement between do and done)
done
www.getmyuni.com
while loop Syntax:
while [ condition ]
do
command1
command2
....
done
Loop is executed as long as given condition is true.
www.getmyuni.com
The case Statement
The case statement is good alternative to Multilevel if-then-else-fi
statement. It enable you to match several values against one variable. Its
easier to read and write. Below is the syntax:
case $variable-name in
pattern1) command
..
command;;
pattern2) command
..
command;;
patternN) command
..
command;;
*) command
..
command;;
esac
www.getmyuni.com
Conditional execution i.e. && and ||
The control operators are && (read as AND) and || (read as OR).
An AND list has the
Syntax: command1 && command2
Here command2 is executed if, and only if, command1 returns an exit
status of zero.
An OR list has the
Syntax: command1 || command2
Here command2 is executed if and only if command1 returns a non-zero
exit status.
www.getmyuni.com
Common test statements
www.getmyuni.com
Special operators
www.getmyuni.com
Functions
Function is series of instruction/commands. Function performs particular
activity in shell. To define function use following syntax:
function-name ( )
{
command1
...
commandN
return
}
Where function-name is name of you function, that executes these
commands. A return statement will terminate the function.
# Set up Date, User
DATE=`date
'+%Y/%m/%d %H:%M:%S'`
www.getmyuni.com
USER=`/usr/bin/whoami`
export DATE USER
Sample Shell Script
Dd=false
clear
while [ "$Dd" != true ]
do
echo "
User: $USER
Host: $HOSTNAME Date: $DATE "
echo "
**************************************************************"
echo "
*
Welcome To Build and Release Management *"
echo "
*
Please Choose Function
*"
echo "
**************************************************************"
echo "
"
echo "
1. Frontend Deploy on Server
"
echo "
2. Backend Deploy on Server
"
echo "
0. EXIT
"
echo "
"
echo "
**************************************************************"
echo
echo
echo " Choose[1,2,0]: \c"
read OPTm
echo "Option .....:" $OPTm
case $OPTm in
1)
echo -e '\E[1;33;44m'"Task Completed, press enter to continue (Goto Menu)"; tput sgr0
echo ""
read
Dd=false
;;
2)
echo -e '\E[1;33;44m'"Task Completed, press enter to continue (Goto Menu)"; tput sgr0
echo ""
read
Dd=false
;;
0)
echo "
"
echo "
Operation Completed. Thank you. "
Dd=true
;;
*)
echo "
"
echo "
INVALID OPTION. PLEASE CHOOSE AGAIN."
echo "$Press ENTER to continue ...."
read OPTm
;;
esac
done
www.getmyuni.com
A list of commands and a quick description
alias ........ this allows the user view the current aliases
awk ........ this allows the user to search for a pattern within a file
bdiff ......... compares two large files
bfs .......... scans a large file
cal ......... shows a calendar
cat ......... concatenates and prints a file
cc ......... c compiler
cd ........ changes directories
chgrp ......... changes a file groups ownership
chmod ....... changes the permission on a file
chown .......... changes the individual ownership of a file
cmp ......... compairs two files
comm .......... compares two files so as to determine which lines are common to both
cp .......... copies file to another location
cu ......... calls another unix sysytem
www.getmyuni.com
Continued..
date .......... returns the date and time
df ......... shows all mounted drives on your machine
diff ......... displays the diference between two files
du ......... shows the disk usage in blocks for a directory
echo ........ echoes the data to the screen or file
ed ........ text editor
env ......... lists the current environment variables
ex ........ another text editor
expr ........ evaluates a mathmatical formula
find ........ finds a file
f77 .......... fortran complier
format ........ initializes a floppy disk
grep ......... searches for a pattern within a file
help ......... gives help
kill ........ stops a running process
www.getmyuni.com
Continued..
ln ........ creates a link between two files
lpr ......... copies the file to the line printer
ls ......... lists the files in a directory
mail ........ allows the user to send/receive mail
mkdir .......... makes directory
more ......... displays a data file to the screen
mv ........... used to move or rename files
nohup ........ allows a command to continue running even when you log out
nroff ......... used to format text
passwd ........ changes your password
pkgadd ......... installs a new program onto your machine
ps ........... Lists the current processes running
pwd ........ displays the name of the working directory
rm ........ removes files
rmdir ........ removes directories
www.getmyuni.com
Continued..
set ......... lists all the variables in the current shell
setenv ......... sets the environment variables
sleep ......... causes a process to become inactive
source ......... allows the user to execute a file and update any changed values in that file
sort .......... sorts files
spell ......... checks for spelling errors in a file
split ........ divides a file
stty ......... sets the terminal options
tail ......... displays the end of a file
tar ......... copies all specified files into one
touch ........ creates an empty file or updates the time/date stamp on a file
troff ......... outputs formatted output
tset ........ sets the terminal type
umask ......... specify a new creation mask
uniq ......... compairs two files
www.getmyuni.com
Continued..
uucp ........ unix to unix execute
vi ........ full screen editor
vipw ......... opens the vi editor as well as password file for editing
volcheck ......... checks to see if there is a floppy disk mounted to your machine
wc ......... displays detail in the full size
who ........ inf. on other people online
write ......... send a message to another user
! ....... repeats commands
www.getmyuni.com
Thanks