Phased summary of shell programming + several shell script examples

Posted by fangfang on Sat, 11 Sep 2021 06:31:41 +0200

        I've been learning shell for some time. I've sorted out some basic shell syntax before. Today I'll make a phased summary. In addition, there are several script examples for your reference.

Summary brain map

Script instance

1. Judge whether the two hosts are ping ed

#!/bin/env bash
#This script is used to determine whether the current host communicates with the remote host
#Define variables interactively, and the user decides which host to ping
read -p "Please enter the information you want ping Host IP: " ip
#Use the ping program to determine whether the host is interconnected
ping -cl $ip $>/dev/null
if [ $? -eq 0 ];then
echo "Current host and remote host $ip It's interworking"
else "Current host and remote host $ip Impassability"
fi

2. Judge whether a process exists

Idea:  

        Check the related commands of the process (ps top pgrep tes) -- judge whether the process exists according to the returned status value of the command -- implement it in scripting language according to the logic

ps -ef|grep httpd

echo $?
ps -ef|grep httpd|grep -v 'grep'
 service apache start
 echo $?
 service apache start
 pgrep --help
 pgrep httpd
 echo $?
 service apache start
 pgrep httpd
 echo $?

pgrep command: finds a process from the running process queue based on its name and displays the found process id

Options:

  • -o: Displays only the smallest start process number found
  • -n: Only the maximum end process number found is displayed
  • -l: Display process name
  • -p: Specifies the parent process number
  • -g: Specify process group
#!/bin/env bash
pgrep httpd &>/dev/null
if [ $? -ne 0 ];then
echo "current httpd Process does not exist"
else
echo "current httpd Process exists"
fi


test $? -eq o && echo "current httpd Process exists" || echo "current httpd Process does not exist"

3. Judge whether a service is normal

Idea:

      You can judge whether the process exists. The best way to use / etc/init.d/httpd status to judge the status is to access it directly and judge it by accessing the return values of success and failure

Use the instructions wget, curl, elinks -dump

#!/bin/env bash
#Judge whether the portal website can provide services normally

web_server=www.itcast.cn
wget $web-server $>/dev/null
[ $? -eq 0] && echo "Current website ok" || echo "The current site does not exist ok,Please deal with it immediately"

  4. Calculate the sum of all odd numbers within 1-100

#!/bin/env bash
#Calculate the sum of odd numbers between 1-100
#Define variables to hold odd and


sum=0
for i in {1..100..2}
  do
        let sum=$sum+i
  done
echo "1-100 The sum of all odd numbers within is:" $sum

5. Check the host network communication in the LAN

#!/bin/env bash
#Define variables
ip=10.1.1
#Circular ping host
for ((i=1;i<=10;i++))
do
ping -c1 $ip.$i &>/dev/null
if [ $? -eq 0 ];then
echo "$ip.$i is ok" >> /tmp/ip_up.txt
else
echo "$ip.$i is down" >> /tmp/ip_down.txt
fi
done

      Add one {}& between do and done, and the completion time will be much faster

root@lijingtest-VirtualBox:~# time ./ping.sh
 In LAN IP Network detection completed

real	1m20.077s
user	0m0.011s
sys	0m0.007s
root@lijingtest-VirtualBox:~# vim ping.sh
root@lijingtest-VirtualBox:~# chmod +x ping.sh
root@lijingtest-VirtualBox:~# 
root@lijingtest-VirtualBox:~# 
root@lijingtest-VirtualBox:~# time ./ping.sh
 In LAN IP Network detection completed

real	0m10.020s
user	0m0.014s
sys	0m0.007s
root@lijingtest-VirtualBox:~# 

 

6. Randomly generate telephone numbers starting with 139

Specific requirements:

      Write a script to generate a phonenum.txt file, and randomly generate 1000 mobile phone numbers starting with 139, one line each.

Idea:

  • Generate 1000 phone numbers, and the script needs to cycle 1000 times for while until
  • 139 + 8 bits, and the last 8 bits are generated randomly. echo $[$RANDOM%10] can be generated randomly for each digit
  • Save the randomly generated number to the variable, and then add 139 to save it to the file

realization:

#!/bin/bash
#Generate 1000 numbers starting with 139 randomly and save them to a file


file=/root/phonenum.txt
for ((i=1;i<=1000;i++))
do
 n1=$[#RANDOM%10]
 n2=$[#RANDOM%10]
 n3=$[#RANDOM%10]
 n4=$[#RANDOM%10]
 n5=$[#RANDOM%10]
 n6=$[#RANDOM%10]
 n7=$[#RANDOM%10]
 n8=$[#RANDOM%10]
 echo "139$n1$n2$n3$n4$n5$n6$n7$n8"  >>$file
done

7. Randomly select five lucky visitors

      Randomly select 5 lucky visitors from the above 1000 phone numbers and show them. The middle 4 digits are replaced by *

#!/bin/bash
#Five lucky visitors were randomly selected

phone=/root/phonenum.txt
for ((i=1;i<=5;i++))
do
line=`wc -l $phone |cut -d' ' -f1`
luck_link=$[RABDOM%$line+1]
luck_num=`head -$luck_line $phone|tail -1`
echo "139****${luck_num:7:4}"
echo $luck_num >> luck.txt
sed -i "/$luck_num/d" $phone
done

Topics: Linux shell software testing