linux in respect of such learning Lesson

shell scripts into batch and interactive style

shell script has three parts
1, script statement #! / bin / bash
2, script notes # explanation of functions and parameters of
3, script commands

For example: vim /haha.sh Note: .sh script to write the end of the comment information may or may not write, you can write multiple lines
#! / bin / bash
# LS pwd
pwd
LS - L


When executed with /haha.sh may need to file have execute permissions can bash haha.sh

1, the ability to receive parameters
2, to determine the function of
vim haha.sh
#! / Bin / bash
echo "$ 0" $ 0 name of the script
echo "$ #, $ *" $ # $ * represents the total of several variables are expressed What
echo "$ 1, $ 3, $ 5" displays the first, third, fifth parameter

bash haha.sh a b c d e f

haha.sh
6, a b c d e f
a,e,e

Analyzing file determination logic determines integer string determination
file judging
[-f / etc / fstab] fstab determines whether the file is present and whether [] include a space on both sides of
echo $ display showing a result of the establishment of a 0 indicates not satisfied?
[- d / dev] dev determines whether the directory, if there

And logical operators && logic or with logic, if the preceding statement following the successful execution is performed
, for example, [-d / etc] && echo " OK" if etc directory exists and then the output is the OK, otherwise, nothing is output
|| logic or if the execution failed earlier statement, the latter statement is executed
, for example, [$ USER = root] && echo "admin" || echo "user"
If the variable USER is the root admin or output to tell the User
! Non-logic negation [! $ USER = root] USER determines whether or not the variable is the root

the -Z determines whether the content is empty string
[$ HAHA the -Z] HAHA determining whether the variable is null, 0 is the variable does not exist, a variable already exists, the variable assignment before check whether there is a variable
digital comparator symbols
- gt -eq equal to greater than - lt -ge less than less than or equal -le -ne not equal
, for example, to see if the memory is less than 1g
Free -m | grep Mem: | awk '{}. 4 Print $' extraction the remaining memory value
[ `free -m | grep Mem: | awk '{print $ 4}'` - lt 1024] && echo "buzu" || echo "chongzu"
current memory usage is less than 1024 is less than the output or outputs chongzu buzu
free -m displayed in megabytes memory
search Mem: row
extracted from the fourth row of column awk '{print $ 4}

Single branch conditional statement only once chance to determine
if a conditional test -e statement indicating whether, regardless of the file or
#! / bin / bash
IF [! -e / Media / haha]
then
mkdir / Media / haha back then with multiple commands can
fi
two-branch conditional statements can have twice the opportunity to determine the
#! / bin / the bash
of ping -i. 3 -C $. 3. 1 & -w 0.2> / dev / null frequency -C -i -w timeout interval
IF [$? -eq 0]
the then
echo "the HOST. 1 $ IS the On-Line"
the else
echo "$ 1 IS Host Off-Line"
fi
bash haha.sh 192.168.10.10

Multi-branch conditional statement
#! / bin / the bash
Read -p "the Enter:" You can use the GRADE elif multiple
IF [$ -ge the GRADE 85] && [$ -le the GRADE 100]; the then
echo "henhao"
elif [$ -ge the GRADE 70] && [$ -le 84 the GRADE]
echo "Pass"
the else
echo "Failure"
Fi

for loop
vim /user.txt
zhangsan
lisi
wangwu
zhaosi
#! / bin / bash
the Read -p "the Enter" PASSWD
for UNAME `CAT users.txt`
do RHCE examinations FOR statement in
the above mentioned id $ UNAME &> / dev / null
IF [$? -eq 0]
the then
echo" cunzai "
the else
useradd $ & UNAME> / dev / null
echo "$ pASSWD" | passwd --stdin $ UNAME &> / dev / null
fi
DONE
Note: if the script can also add conditional statements, such as if the success of the new user to specify the output information, unsuccessful output specification information

IP address test whether a text line
vim /ip.txt
192.168.10.10
192.168.10.20
192.168.10.30

vim /haha.sh
#! /bin/bash
for IP in `cat ip.txt`
do
ping -c 3 -i 0.2 -W 3 &>/dev/null
if [ $? -eq 0 ]
then
echo "$IP is On-line"
else
echo "$IP is Off-line"
fi
done

 

Guess you like

Origin www.cnblogs.com/Bluejun/p/12348679.html