[go: up one dir, main page]

0% found this document useful (0 votes)
47 views19 pages

SH 1

This document provides an overview of shell scripting in Unix/Linux systems. It discusses shell script basics like variables, operators, conditional statements, looping statements and file handling. It also covers more advanced topics like functions, external commands, regular expressions, here documents and signal handling. The document explains the relationship between the user layer, OS layer and hardware layer in a Linux system and how commands are executed via system calls. It includes examples of a simple shell script to display system information and commands to make the script executable.

Uploaded by

pythonregxclass
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views19 pages

SH 1

This document provides an overview of shell scripting in Unix/Linux systems. It discusses shell script basics like variables, operators, conditional statements, looping statements and file handling. It also covers more advanced topics like functions, external commands, regular expressions, here documents and signal handling. The document explains the relationship between the user layer, OS layer and hardware layer in a Linux system and how commands are executed via system calls. It includes examples of a simple shell script to display system information and commands to make the script executable.

Uploaded by

pythonregxclass
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 19

About Unix/Linux - arch

|
|
About Shell
Types of shell
|
unix/linux command line structure
|
Script(Programming)
----------------------
1. Variables
a. User Defined Variable(UDV)
b. Shell (or) env (or) Builtin variable
c. Commandline args
d. Array
2. Operators
a. Arithemtic
b. Relational
c. Logical
d. File Test
e. Regx (or) match =~
3. Conditional statements - Test/Validation
a. if statement
i) if only
ii)if..else
iii)if..elif
b. case statement
4. Looping statements
a. Conditional style
b. Collection style

5. FileHandling

6. Function
a. functioncall
b. nested call
c. call with arguments
d. scope
e. loadable script
7. External filters (grep;find;sed;awk;comm;cut etc.,)

8. Regular Expression (grep;sed;awk)

9. sed - script
10. awk - script

11. Heredocument (or) Line Oriented style - Database,web,ssh,etc.,

12. Signal Trap


---------------------------------------------------

OS - System software(program) - interface between user & h/w

+------------------------+
|User Layer: Appln
|
+------------------------+
| OS Layer
|
+------------------------+
| H/W
+------------------------+

Kernel - Core part of an os


------
|->programm code written in C

----------------------------------------------------------
systemcall()
|
FileSystem <--> PCB |IPC;SCHED;MM;Net|
|
Device Driver
|
Device Controllers
|
---------------------------------------------------------------
PCB - Process Ctrl Block

root@host~]# command line


-------------
|
primary prompt

root@host~]# echo "Hello"


Hello
root@host~]#

echo "Hello" ->shell ->|systemcall() ->FileSystem ->DD ->DC |->H/W

write(1,"Hello")
|
sys_write(1,"Hello")
|
my_write_(.."Hello")
|
printk(FLAG,"Hello")
|
asm push
call
..
|
|
101010101

root@host~]# strace command

systemcall()=FD

FD=0 <STDIN>
FD=1 STDOUT
FD=2 STDERR
FD=0x3451 (Memory) - Valid call

FD=3 to 2 power 32/64 - Valid Call

FD= -1<== Failed Call

---------------------------------------------------------
User Layer
----------------//shell-----
systemcall()
|
FileSystem <--> PCB |IPC;SCHED;MM;Net|
OS Layer |
Device Driver
|
Device Controllers
|
----------------------------------------------------
H/W Layer

About Shell
------------
->shell is interface between User layer and Kernel
->interpret - command(or) instruction to kernel understanding format.

Types of shell
----------------
1.Bourne Shell(sh)
|
2.Korn shell(ksh)
--------------------//AT&T

3. CShell (csh) ->tcsh ->expect

4. BourneAgainSHell (BASH) - default Linux shell - GNU

How to find out list of available shells?

cat /etc/shells

Test current working shell name

ps - command

(or)

echo $0 (special variable)

-----------------------------------------------------------
subshell
--------
shell inside another shell(new shell -child)

root@host~]# echo $0
bash <== current working shell is bash

root@host~]# sh {Enter} <== creating subshell


sh-4.2#
sh-4.2#
sh-4.2# echo $0 {enter}
sh <== Current working shell
|
sh-4.2# csh {Enter} <== creating new subshell
#
# echo $0
csh
# ps -f

PID PPID(Parent Process PID) .. CMD


101 34 bash
102 101 sh
103 102 csh

---------------------------------------------------------------------------------
Unix/Linux - Kernel abstractions are File,Process
-----------
File - Data - Under storage
|
Process - Data - Underthe CPU

Process
|
----------------------------------------------------
| |
User process System process
- by user - by systemD (or) init

Linux Boot process stage


--------------------------
OFF
|
Switch ON PC
|
PowerOnSelfTest(POST)
|
MasterBootRecord(MBR)
|
Bootloader
|
Kernel
|
Process(OL7/OL8/RHL7/.... - systemD ; OL5/OL6;RHL5;RHL6;DEB10,Ubuntu10 .. - init)
|
start list of all other process(system process)
|
getty (?) - login:_______
| password:________
| |
| |
shell(CLI) Desktop(GUI)
| |
user I/O (user process)

Process
|
----------------------------------------------------
| |
User process System process
- by user - by systemD (or) init
- TTY:pts/PortNumber - TTY: ?
|
- Parent is:working shell - Parent is:systemd (or) init

-----------------------------------------------------------------------------------
---
Linux commands

1. Command only style


2. Command arguments style
3. Command anotherCommand

1. Command only style


------------------------
root@host~]# date
root@host~]# ls
root@host~]# whoami
root@host~]# ps
root@host~]# echo
root@host~]# ...

2. Command with argument style


--------------------------------
root@host~]# date +%D
root@host~]# ls <== commandonly
root@host~]# ls -l <= command with one argument
root@host~]# ls -l /etc/passwd /var/log/boot.log <== command with arguments
-- ----------- ------------------
root@host~]# echo Welcome to unix shell script
Welcome to unix shell script

root@host~]# echo "Welcome to unix shell script"


Welcome to unix shell script

3. Command with anotherCommand style - command `command` <= backquote ``


-------------------------------------- | |
root@host~]# uname <= command only this is NOT single quote
Linux
root@host~]# echo "Working kernel name:uname" <== command with arguments
Working kernel name:uname
Vs
root@host~]# echo "Working kernel name:`uname`" <== command with anothercommand
Working kernel name:Linux

root@host~]# echo `uname` root@host~]# echo `Uname`


Linux Vs Uname command not found

command `command` (or) command $(command)


--------------------------------------------------------------------------------
about shell script file
-----------------
|-> Text file - ordinary file
|-> filename extension is .sh (.sh is optional) (ex: p1.sh p2)

|-> file must be executable (chmod a+x filename / chmod 777 filename)

File (F) - by 3 users


-------
->user (or) author - u
->groups - g
->others - o

each user can perform read/write/execute operation


-------

chmod user details + filename


-
chmod u+r filename
|||
|||->read permission
||
||->To assign permission
|
user

chmod u+x filename


|||
|||->execute permission
||
||->To assign permission
|
user

chmod u-x filename


|||
|||->execute permission
||
||->To Remove permission
|
user

chmod ugo+x filename (or) chmod a+x filename (or) chmod +x filename

r w x
1 1 1 -> 7
1 1 0 -> 6
1 0 1 -> 5
1 0 0 -> 4
0 0 0 -> 0

chmod 777 filename


|||
||others
|group
|
user

chmod 765 filename


rwxrw-r-x .. filename

---------------------------------------------------------------------------------
Tasks
------
|
test -> ps {Enter}
|
see what is your working shell?
|
echo $0

sh-4.2# bash {Enter} <== type word bash press {Enter}

root@host~]# echo $0
bash

step 1: create a file (vi p1.sh)

step 2: using command with command style display following system info.

Working kernel name (ex: echo "Working kernel name is:`uname`


Kernel version echo "`uname` Version is:`uname -r` )
Login name
Total no.of running process count => ps -e|wc -l
display today date/time => date

step 3: save this file (:wq )


|
step 4: Give Execute permission to this file
(chmod a+x p1.sh (or) chmod 777 p1.sh)
|
step 5: Run a file (/root/myproject/d1/p1.sh) (or) ./p1.sh
---------------------------------------------------------------------------------
root@krosumlabs:~/ShellScript_Training# cat -n p1.sh
1 echo "Working Kernel is:`uname`"
2 echo "`uname` Version is:`uname -r`"
3 echo "My Login name is:`whoami`"
4 echo "Total no.of process count is:`ps -e|wc -l`"
5 echo "Today:`date +%D` (MM/DD/YYYY)"
6 echo
7 echo "Today:$(date +%D) Working kernel name is:$(uname)"
root@krosumlabs:~/ShellScript_Training#
root@krosumlabs:~/ShellScript_Training# ./p1.sh
Working Kernel is:Linux
Linux Version is:3.0.0-12-generic
My Login name is:root
Total no.of process count is:133
Today:08/08/22 (MM/DD/YYYY)

Today:08/08/22 Working kernel name is:Linux


root@krosumlabs:~/ShellScript_Training# echo

-----------------------------------------------------------------------------------
Shell script Comment lines
--------------------------
1. Single line comment #

2. Multiline Comments
<<Flag <== user defined string
....
....
Flag

<<Abc <== open


...
... shell won't interpret this section code
...
Abc <== close

1 echo "Working Kernel is:`uname`"


2 echo "`uname` Version is:`uname -r`"
3 # uname is a command - display working kernel name
4 # -r is an option - display kernel version
5 # ------------------------------------
6 echo "My Login name is:`whoami`"
7 echo "Total no.of process count is:`ps -e|wc -l`" # wc -l is word count
- count no.of lines
8 echo "Today:`date +%D` (MM/DD/YYYY)"
9 echo # empty line
10 echo "Today:$(date +%D) Working kernel name is:$(uname)"
11 <<abc
12 in shell script -we can combine one command with another
13 command using backquote `` or compound style $(command)
14 asfdasfsa
15 asfsadfsda
16 asdfdsdafdsaf
17 asdfasdfdsa
18 sadfsafsafsafsa affsfdsafasfads
19 abc
20 echo "End of the script"

----------------------------------------------------------------------------------
shell script
-------------
new to shellscript (or) new to program

step 1: Understand the topic definition - (what is __<topic>_?)


|
step 2: Understand the topic Syntax
|
step 3: Refer Existing Examples
|
step 4: Exercise

variable
----------
variable - placeholder - namespace - it's holding a value
-----
any string
any digits
any command
command result
space,specialchars etc.,

Syntax:-
-----------
variablename=value
Rule 1) Variable name starts with A-Za-z_ (not starts with digits)
Rule 2) Variable name not allows space,specialchars
Rule 3) There is no space in = LHS,RHS

count=150
Fname="/var/log/auth.log"
IP="192.168.1.245"
Lb=1.23
cmd="date" <-------|
|
cmd_result=`date`<--|

Here: count,Fname,IP,Lb,cmd,cmd_result - variables

To replace variable value -> $ <== scalar symbol (singleValue)

$variable (or) ${variable}


|<------>|

count=150
echo "count value is:$count" ->count value is:150

echo "count value is:${count}" ->count value is:150

echo "count value is:$Count" ->count value is: <= empty string
------
|->undefined variable

va="uname"

vb=`uname`

echo "${va}" -> uname <- echo "$va"


|
echo "${vb}" -> Linux <- echo "$vb"

-------------------
modify p2.sh file
|
initialize variable=value style
|
initialize each command result to variable
using variable - display system details.

echo "Login name is:`whoami`" -> name=`whoami`


|
echo "Login name is:$name"
(or)
echo "Login name is:${name}"

----
write a shell script
|
initialize a filename
|
using ls -l <filename> - display file details
fname="p1.sh"

ls -l ${fname}

(or)

echo "about $fname file details:-"


echo "----------------------------"
ls -l $fname
echo "----------------------------"

|
Write a shell script

step 1: initialize employee details (emp ID,emp Name,emp Dept)


|
step 2: using echo command - display employee details line by line

-----------------------------------
Emp name is:Mr.Arun
Mr.Arun Emp ID:E123
Mr.Arun Working dept is:sales
----------------------------------
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

read
Keyboard(<STDIN>) ----<-------Shellscript--------->---------Monitor(STDOUT)
echo

read - read a input value dynamically from Keyboard.

Syntax:-
----------
read UDV

fname="p1.c" echo "Enter a filename:"


echo "About $fname file:-" Vs read fname # interface to Keyboard
ls -l $fname echo "About $fname file:-"
ls -l $fname

echo "Enter a filename:"


read fname

Syntax:-
---------
read -p "prompt message"<space>UDV

read -p "Enter a filename:" fname


|------ STDOUT----| <STDIN>

-----------------------------------------------------------------------------------
-
Q1. Write a shell script

read a disk partition name


read a partition size
read a file system type
read a mount point
|
|
display input disk details to monitor - use single echo command

Example:
---------
Enter a partition name: /dev/xvdb1
|
Enter /dev/xvdb1 size: 100GB
|
Enter /dev/xvdb1 file system type: xfs
|
Enter /dev/xvdb1 mount point: /mnt

Partition details:- <---------


... |
Partition: /dev/xvdb1 |
Type: xfs display input details
Size: 100GB |
Mount point: /mnt |
--- <---------
-------------------------------------------------------------------------
read -p "Enter a disk partition name:" p1
read -p "Enter ${p1} size:" ds1
read -p "Enter ${p1} file system type:" fs1
read -p "Enter ${p1} mount point:" mp1

echo "About ${p1} details :-


--------------------------------
Partition: ${p1}
--------------------------------
File System Type: ${fs1}
--------------------------------
Disk Size: ${ds1}
--------------------------------
Mount Point: ${mp1}
--------------------------------"

===================================================================================
======

Shell variable (or) buitin variable


------------------------------------
- pre-defined
|
env {enter}
---

root@host~]# echo ${SHELL}


/bin/bash

$SHELL - login shell name

$PATH - command env path

$HOME - login directory


$PWD - same as pwd command
echo "Current working directory:`pwd`"

echo "Current working directory:$PWD"


----
|<-- variable

User defined variable Vs Shell variable


--------------------- --------------
local global

root@host~]# myvar=100
root@host~]# echo ${myvar}
100

root@host~]# sh {Enter} <== creating subshell root@host D1]# echo ${SHELL}


sh-4.2# echo $0 /bin/bash
sh root@host D1]# sh{enter}
sh-4.2# echo ${myvar} sh-4.2#
sh-4.2# sh-4.2# echo ${SHELL}
sh-4.2# exit <====== exit from subshell(sh) /bin/bash
root@host~]# echo ${myvar}
100
-----------------------------------------------------------------------------
Convert UDV ->Shell variable
-----------------------------
export UDV=Value
(or)

UDV=Value
export UDV
-----------------------------------------------------------------------------
Write a shell script

- display Login shell name & Current working directory (don't use system command)
|
- display list of files under login directory (ls <logdir>)
----------
Test in commandline
====================
->initialize a DB name (/usr/bin/oracle) to UDV
|
->export a variable
|
-> create new subshell(sh)
|
->display DB name
-----------------------------------------------------------------------------

echo -e "Login shell:${SHELL}\t Current directory:${PWD}"


echo "List of files ${HOME} directory:-"
ls -l ${HOME}

--------------------------------------------------------------------------------

unset <variable>

var=100 -> var | 100 | 0x345


echo $var ->100 | ^
| |
unset var -------------------
-->
delete

echo $var -->empty string

-----------------------------------------------------------------------------------
commands used in variable
--------------------------
read <UDV>
----
export var=value
------
unset <UDV>
-----
echo ${UDV}
----

-----------------------------------------------------------------------------------
----
Operators
------------
Arithmetic operators
--------------------
+ - * / %
.......... input type is number(int) ->output is number(int)

1. expr command
----

Syntax:-
---------
expr<space>Expression
__________

|
Operand<space>OPERATOR<space>Operand

expr 10 + 20 ->30 Vs expr 10+20 ->10+20

va=100
vb=50

expr $va + $vb ->150

echo "Total value is:`expr $va + $vb`" ->Total value is:150


|<------------>|

-> expr won't perform floating point operation

total=`expr 10 + 20` # variable=`command`


-----
|->echo ${total} ->30

Write a shell script


- read a disk name from <STDIN>
- read a disk size from <STDIN> (ex: 150)

- read a another disk name from <STDIN>


- read a another disk size from <STDIN>(ex: 50)
|
|
- calculate sum of disk size <== use expr command
|
|
- display each partition name - size
sum of partition disk size

Partition name: /dev/xvdb1 Size: 150GB


Partition name: /dev/xvdb2 Size: 50GB
--------------------------------------------
Total Size: 200GB
--------------------------------------------
================================================================================
2. Compound style
------------------
$((expression))

echo $((10+20)) -> 30

echo $((10*3)) -> 30

Va=10
Vb=20

echo $((Va+Vb)) ->30

-> supports ++/-- expression

n=5
echo $((++n)) ->6

n=6
((n++))
echo $n -> 6

->like expr, compound style - won't support floating point expression


---------------------------------------------------------------------------

Write a shell script


--------------------
read any 3 files from <STDIN>
|
read each file size from <STDIN>
|
calculate sum of file size
|
display - filename - individual size
total file size
--------------------------------------------------------------------------

read -p "Enter a file name:" f1


read -p "Enter a $f1 file size:" fs1
read -p "Enter a file name:" f2
read -p "Enter a $f2 file size:" fs2
read -p "Enter a file name:" f3
read -p "Enter a $f3 file size:" fs3

total=`expr $fs1 + $fs2 + $fs3` # total=$(fs1+fs2+fs3)

echo "
File name File size
________________________________
${f1} ${fs1}
________________________________
${f2} ${fs2}
________________________________
${f3} ${fs3}
________________________________
Sum of the file Size: ${total}
_________________________________"

==============================================================================

$? - special variable
---
|-> return code -> last command operation exit code

commandA
commandB
echo $? <=== commandB - exit status
commandC
commandD
-------------

$? -> 0 (Last command operation is success)

$? -> non-zero (1 2 3 4 ,... N) - Last command operation is failed

========================================================================
Relational operators
--------------------
|
-------------------
| |
| |
string number(0-9)
|
("A-Za-z0-9space specialchars)

== != -eq -ne -gt -ge -lt -le

"root" == "root" 10 -eq 10


10 -ne 10
10 -gt 5
10 -lt 5
10 -ge 5
test operator
----
(or)
[ ]
Syntax:-
---------
test<space>Expression

(or)
[<space>Expression<space>]

root@host~]# test 10 -eq 10


root@host~]#
root@host~]# echo $?
0
root@host~]# test 10 -gt 50 10 >50
root@host~]# echo $?
1
root@host~]# [ 10 -eq 10 ]
root@host~]# echo $?
0
root@host~]#
----------------------------------------------------------------------
Conditional Statement
---------------------
|->Code block
|->execute only one time

|->if statement
--------------

i) if only
=============
Syntax:-
--------
if<space>[ condition ]
then
True block
fi

# if then fi -keywords

i) if else style
=================
Syntax:-
--------
if<space>[ condition ]
then
True block
else
False block
fi

# if then else fi -keywords


------------------------------------------------------------------

command useradd userA


| |
if [ $? -eq 0 ] if [ $? -eq 0 ]
then then
command-execution is success echo "User creation is done"
else else
command-execution is Failed echo "User creation is failed"
fi fi

(or)

command useradd userA


| |
if [ $? -ne 0 ] if [ $? -ne 0 ]
then then
command - execution is failed echo "User creation is failed"
fi fi

-----------------------------
test shell zsh is installed or not
| ====
test daemon crond is active or not
| ====
compress one gzip (gzip filename) file
test gzip execution is success or not
===

rpm -q zsh
if [ $? -ne 0 ]
then
echo "Package zsh is not installed"
fi

systemctl is-active crond


if [ $? -ne 0 ]
then
echo "crond daemon is not active"
if

gzip filename
if [ $? -ne 0 ]
then
echo "file compression is failed"
fi

commandA
if [ $? -ne 0 ]
then
echo "CommandA operation is failed"
fi
-------------------------------------------
Write a shell script

step 1: read a port number from <STDIN>


|
step 2: test input port number is above 500 (501 and above)
|
step 3: -> initialize app name is: testApp
|
-> not above 500 - initialize app name is demo App
|
step 4: display App name and port number.

read -p "Enter a port number:" port


if [ $port -gt 500 ]
then
app="testApp"
else
app="demoApp"
fi

echo -e "App name is:${app}\t Running port number is:${port}"


------------------------------------------------------------------------

->Test user is root or not


==== |->sorry your not root user - exit
|
|
load a kernel module(modprobe <module>) ex: modprobe nfs
------------- modprobe bluetooth
|
using $? - test module is loaded or not

if [ `whoami` == "root" ]
then
modprobe nfs
if [ $? -eq 0 ]
then
echo "module nfs is loaded successfully"
else
echo "module nfs is not loaded successfully"
fi
else
echo "Sorry your not root user"
exit
fi

---------------------------------------------------------------------
if..elif style
---------------
|->multiconditional style

Syntax:-
--------
if<space>[ Condition1 ]
then
True block-1
elif<space>[ Condition2 ]
then
True block-2

elif<space>[ Condition3 ]
then
True block-3

elif<space>[ Condition4 ]
then
True block-4

elif<space>[ Condition5 ]
then
True block-5
elif<space>[ Condition6 ]
then
True block-6
..

elif<space>[ ConditionN ]
then
True block-N
else
False block
fi

Write a shell script

read a shell name from <STDIN>


|
test input shell name is bash shell - initialize profile file name is bashrc
---- -------
test input shell name is ksh shell - initialize profile file name is kshrc
--- -----
test input shell name is csh shell - initialize profile file name is cshrc
--- ----
|
|
default shell name is: /bin/nologin - initialize profile file name is:
/etc/profile

-> display your shell name profile filename

read -p "Enter a shell name:" sh

if [ $sh == "bash" ]
then
pfname="bashrc"
elif [ $sh == "ksh" ]
then
pfname="kshrc"
elif [ $sh == "csh" ]
then
pfname="csh.rc"
else
echo "Sorry your input shell name is not matched"
echo "default shell details"
pfname="/etc/profile"
sh="/bin/nologin"
fi
echo -e "shell name is:$sh\tprofile file name:$pfname"
*******************************************************************************

You might also like