Little knowledge
- The expression conditions of shell script if can be viewed by man test, for example, - a is equivalent to &&. 2.
Example 1: Simulated landing
#!/bin/bash
echo -n "login:"
read name
echo -n "password:"
read passwd
if [ $name = "wilcohuang" -a $passwd = "31415926yhwy." ];
then echo "the host and password is right"
else echo "input is error!"
fi
Example 2: Compare the size of two numbers
#!/bin/bash
echo "please enter two number"
read a
read b
if test $a -eq $b
then echo "no.1 = no.2"
elif test $a -gt $b
then echo "no.1 > no.2"
else echo "no.1 < no.2"
fi
Example 3: Search for the existence of files in a directory
#!/bin/bash
echo "enter a file name:"
read a
if test -e /data/home/wilcohuang/shell/$a
then echo "the file is exist!"
else echo "the file is not exist!"
fi
Example 4: Use of for loops
#!/bin/bash
clear
for num in 1 2 3 4 5 6 7 8 9 10
do
echo $num
done
Example 5: Command line input
#!/bin/bash
echo "Please enter a user:"
read a
b=$(whoami)
if test $a = $b
then echo "the user is running"
else echo "the user is not running"
fi
Example 6: Delete files of size 0 in the current directory
#!/bin/bash
for filename in `ls`
do
if test -d $filename
then continue
else a=$(ls -l $filename | awk '{print $5}')
if test $a -eq 0
then rm -rf $filename
fi
fi
done
Example 7: If there are files in a directory, change its file system size to 3G
#!/bin/bash
while line=`ls /data/home/wilcohuang/shell/test1`
do
if test $line = ""
then echo "NULL"
sleep 1
else echo $line
chfs -a size=3G /data/home/wilcohuang/shell/test1
exit 0
fi
done
Example 8: Test IP address
#!/bin/bash
for i in {1..9}
do
echo "the number of $i computer is"
ping -c 1 192.168.0.$i
done
ping: -c count times
Example 9: Read and print from 0.sh
#!/bin/bash
while read line
do
echo $line
done < 0.sh
Example 10: Read from 0.sh and + 1
#!/bin/bash
test -e 0.sh
while read line
do
a=$line+1
echo $a
done < 0.sh
Example 11: General parametric-free functions
#!/bin/bash
p()
{
echo "hello"
}
p
Example 12: Transfer parameters to functions
#!/bin/bash
p_num()
{
num=$1
echo $num
}
for n in $@
do
p_num $n
done
Command: bash para.sh 10
Example 13: Get the local IP address
ifconfig | grep 'inet addr:' | awk '{print $2}' | sed 's/addr://g'
`
[root@www ~] sed [-nefr] [action]
Options and parameters:
- n: Use silent mode. In the general use of sed, all data from STDIN is typically listed on the terminal. But if the - n parameter is added, only the line (or action) that has been specially processed by sed will be listed.
- e: edit SEDS directly in command-line mode;
- f: Write the sed action in a file directly, while - f filename can run the sed action in filename.
- r: sed's actions support the grammar of extended formal representations. (The default is basic formal representation French)
- i: Modify the contents of the read files directly instead of exporting them to the terminal.
Action description: [n1[, n2]]
n1, n2: Not necessarily exists. Generally speaking, it means "the number of rows that choose to perform actions". For example, if my actions need to be performed between 10 and 20 rows, then "10, 20 [action behavior]"
function:
A: Added, a can be followed by strings, and these strings will appear in a new line (the next line at present)~
c: Instead, strings can be used at the back of c. These strings can replace lines between N1 and n2.
d: Delete, because it is deleted, so there is usually no drum after d.
i: Insert, i can be followed by strings, and these strings will appear on a new line (the current previous line);
p: Print, or print out some selected data. Usually p runs with parameter sed-n
S: Substitution. Substitution can be done directly. Usually the action of this s can be matched with the normal representation! For example, 1,20s/old/new/g is it!
`
Example 14: Find the largest file
#!/bin/bash
a=0
for name in *.*
do
b=$(ls -l $name | awk '{print $5}')
if test $b -ge $a
then a=$b
namemax=$name
fi
done
echo "the max file is $namemax"
Example 15: Find all IPS in this network segment
a=1
while :
do
a=$(($a+1))
if test $a -gt 255
then break
else
echo $(ping -c 1 10.204.172.$a | grep "ttl" | awk '{print $4}' | sed 's/://g')
ip=$(ping -c 1 10.204.172.$a | grep "ttl" | awk '{print $4}' | sed 's/://g')
if [ -n "$ip" ];
then
echo $ip >> ip.txt
fi
fi
done
Note: Comparing strings in shell must be double quotation marks, otherwise the result is incorrect, such as:
a=""
if [ -n "$a" ];
then
echo 'yes'
else
echo 'no'
fi
Example 16: case exercise
#!/bin/bash
clear
echo "enter a numbe from 1 to 5:"
read num
case $num in
1)echo "you enter 1"
;;
2)echo "you enter 2"
;;
3)echo "you enter 3"
;;
4)echo "you enter 4"
;;
*)echo 'default'
;;
esac
Example 20: yes/no returns different structures
#!/bin/bash
clear
echo "enter [y/n]:"
read a
case $a in
y|Y|Yes|YES)echo "you enter $a"
;;
n|N|NO|no)echo "you enter $a"
;;
*)echo 'error'
;;
esac
Example 22: Using built-in commands
#!/bin/bash
clear
echo "hello,$USER"
echo "Today's date id `date`"
Example 23: Print all users without passwords
cat /etc/shadow | grep '!!' | awk -F ":" '{print $1}'
Example 23: Check whether the port number is started
#!/bin/bash
n=1
echo "inspect nginx service..."
while true
do
if test $n -gt 20
then
echo "nginx Service Startup Failure"
break
fi
sleep 5
n=$(($n+1))
port=`netstat -natp | grep "0.0.0.0:80"`
if [ ${#port} -gt 3 ];
then
echo "nginx The service has been started"
break
fi
done