Home
Add Document
Sign In
Register
shell scripting
Home
shell scripting
abou t shell scripting...
Author:
Pruthvi Prakasha
38 downloads
483 Views
366KB Size
Report
DOWNLOAD .PDF
Recommend Documents
shell-scripting
linux-shell-scripting-tutorial.pdf
linux-shell-scripting-tutorial.pdf
Unix Shell Scripting 1
Beginning Shell Scripting
Unix Oracle Shell Scripting
Full description
Linux Shell Scripting Tutorial
Linux Shell Scripting Tutorial
Beginning Shell Scripting
Full description
Beginning Shell Scripting
Descrição completa
Beginning Shell Scripting
shell scriipting-A Shell Script to do shell scripting faster.docx
Full description
Unix Shell Scripting With Ksh/Bash
Unix Shell Scripting With Sh&Ksh
Scripting Mikrotik
Scripting MikrotikDescripción completa
Mikrotik Scripting
Shell
Full description
Shell
shell
Shell
Shell
Abaqus Scripting
Abaqus-scripting
Waterline Real Flow Scripting
Real Flow Scripting guide
Scripting - Law of Attraction
Practice telling the new story of how you would like your life to beFull description
Belajar Bash Scripting Linux
asfafaFull description
Grasshopper VB Scripting
Basic Scripting ANSYS MAXWELL
TutorialFull description
10/2/2007
Net212
SHELL PROGRAMMING WITH BASH
Intro
The difference between tcsh and bash is not so noticeable
What is a shell script?
It is apparent when writing shell scripts Set of commands that are executed from a file Flow of execution can be controlled
Shell script anatomy
Beginning
Body
#!/bin/bash #this is a remark or comment grep $1 /etc/passwd
File attributes
‐rwxrwxr‐x 1 student student 31 Sept 21 18:15 printinfo.sh
1
10/2/2007
Making a script 1. Open new file
vi printinfo.sh
2. Put in beginning (specify shell)
#!/bin/bash
3. Put in body (commands and structures)
grep $1 /etc/passwd
4. Save file
Esc , :wq
5. Make it executable
chmoda+x a+xprintinfo.sh
Executing it
If it’s not in your path
If it’s in your path
If it’s not in your path and in your current folder
/path/to/script/printinfo.sh student printinfo.shstudent
./printinfo.shstudent
Script arguments
grep $1 /etc/passwd
Expand last example
$1 stands for first argument $2 would be the second argument and so on… $0 is the script name grep $1 /etc/passwd grep $1 /etc/passwd| cut –f $2 –d “:” echo thank you for using script $0
Use it
./printinfo.shstudent3 student:x:500:500:student:/home/student:/bin/bash
500 Thank you for using script ./printinfo.sh
2
10/2/2007
Another example
Last example printed username from passwd file and its uid How about an account creation script
#!/bin/sh useradd $1 passwd $1
Save it as mkuser.sh and run it
./mkuserlinuxguy ./mkuser linuxguy Changing password for linuxguy
Another example
Save process list
#!/bin/bash
ps–U $USER > processes.txt chmod a+x a+xlistprocess.sh listprocess. sh ./listprocess.sh
Control the execution flow
Use if statements
If (condition is true) Statements
Use while loops
While(conditionis true) Statements
3
10/2/2007
If statements
Does a file exist?
if [ ‐f /etc/inittab ] #if [condition]
then
fi #end if
ec o yes statements
Run it
chmod chm od a+x a+xcheckinittab.sh checki nittab.sh ./checkinittab.sh yes
Variables
Argument variables
Internal variables
$0, $1, $2,… myvar=“/bin/bash” avalue=`grep student /etc/passwd| cut –f 7 –d “:”`
Back quotes are for command results!!!
cnt=`ls| wc –l` same key as tilde! cnt=‘ls | wc –l’ <‐ Same key as double quote
Variables with if
Check if anything in current folder
cnt=`ls| wc –l` if [ $cnt–gt 0] #is count greater than zero? then #yes it is echo there are files here else #no it isn’t echo no files here
fi
4
10/2/2007
Conditional operators
Operators for numbers
‐gt Greater than ‐eq Equal to ‐lt less than ‐ne Not equal to
Operators for words and sentences (strings)
= equal != not equal
Another if example
Check if .bash_profile exists
bashfile=“.bash_profile”#a string variable
if [ `ls –a .bash_profile` = “$bashfile” ] then echo it exists
else echo it’s not there
fi
Sum up variable usage
Numbers do not use double quotes
Mynum=10 if [ $Mynum–lt 11 ] Echo M numis less than 11 Fi
Strings do use double quotes
Mystr=“david” if [ “$Mystr” != “david” ] echo This is not david fi
5
10/2/2007
Sum up if statements
For comparing values
if [ value1 operator value2 ] then statements fi
For checking the exit code
if some command here then #do this if exit code is 0 statements fi
Exit codes
Also known as an exit status code
Status code 0 means success
type echo $? To see the last exit code
echo $? 0
Status code 1 means failure
ls /etc/issues echo $? 1
While statements
Do something until condition is false
Ex: Create 10 files
Uses same conditional expressions as if = i=1 while [ $i ‐lt $Stoppoint ] do touch myfile$i i=$[$i+1] done
6
10/2/2007
Another while example
Read input from a pipe
ls | while read file
do echo $USER has file $file done
Results
student has file myfile1 student has file myfile2
….
7
×
Report "shell scripting"
Your name
Email
Reason
-Select Reason-
Pornographic
Defamatory
Illegal/Unlawful
Spam
Other Terms Of Service Violation
File a copyright complaint
Description
×
Sign In
Email
Password
Remember me
Forgot password?
Sign In
Our partners will collect data and use cookies for ad personalization and measurement.
Learn how we and our ad partner Google, collect and use data
.
Agree & close