SH 1
SH 1
|
|
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.,)
9. sed - script
10. awk - script
+------------------------+
|User Layer: Appln
|
+------------------------+
| OS Layer
|
+------------------------+
| H/W
+------------------------+
----------------------------------------------------------
systemcall()
|
FileSystem <--> PCB |IPC;SCHED;MM;Net|
|
Device Driver
|
Device Controllers
|
---------------------------------------------------------------
PCB - Process Ctrl Block
write(1,"Hello")
|
sys_write(1,"Hello")
|
my_write_(.."Hello")
|
printk(FLAG,"Hello")
|
asm push
call
..
|
|
101010101
systemcall()=FD
FD=0 <STDIN>
FD=1 STDOUT
FD=2 STDERR
FD=0x3451 (Memory) - Valid 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
cat /etc/shells
ps - command
(or)
-----------------------------------------------------------
subshell
--------
shell inside another shell(new shell -child)
root@host~]# echo $0
bash <== current working shell is bash
---------------------------------------------------------------------------------
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
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
|-> file must be executable (chmod a+x filename / chmod 777 filename)
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
---------------------------------------------------------------------------------
Tasks
------
|
test -> ps {Enter}
|
see what is your working shell?
|
echo $0
root@host~]# echo $0
bash
step 2: using command with command style display following system info.
-----------------------------------------------------------------------------------
Shell script Comment lines
--------------------------
1. Single line comment #
2. Multiline Comments
<<Flag <== user defined string
....
....
Flag
----------------------------------------------------------------------------------
shell script
-------------
new to shellscript (or) new to program
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`<--|
count=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`
-------------------
modify p2.sh file
|
initialize variable=value style
|
initialize each command result to variable
using variable - display system details.
----
write a shell script
|
initialize a filename
|
using ls -l <filename> - display file details
fname="p1.sh"
ls -l ${fname}
(or)
|
Write a shell script
-----------------------------------
Emp name is:Mr.Arun
Mr.Arun Emp ID:E123
Mr.Arun Working dept is:sales
----------------------------------
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
read
Keyboard(<STDIN>) ----<-------Shellscript--------->---------Monitor(STDOUT)
echo
Syntax:-
----------
read UDV
Syntax:-
---------
read -p "prompt message"<space>UDV
-----------------------------------------------------------------------------------
-
Q1. Write a shell script
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
===================================================================================
======
root@host~]# myvar=100
root@host~]# echo ${myvar}
100
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
-----------------------------------------------------------------------------
--------------------------------------------------------------------------------
unset <variable>
-----------------------------------------------------------------------------------
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
va=100
vb=50
Va=10
Vb=20
n=5
echo $((++n)) ->6
n=6
((n++))
echo $n -> 6
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
-------------
========================================================================
Relational operators
--------------------
|
-------------------
| |
| |
string number(0-9)
|
("A-Za-z0-9space specialchars)
(or)
[<space>Expression<space>]
|->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
(or)
-----------------------------
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
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
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
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"
*******************************************************************************