Common Shell scripts

1. Creating linux System Account and Password through Location Variables
$1 is the first parameter to execute the script, and $2 is the second parameter to execute the script.

1 #!/bin/bash
2 # Author: Peter zh
3 # Blog: https://home.cnblogs.com/zhangwduoduoj/
4 # Time: 2019-08-17 19:19:45
5 # Name: userad.sh
6 # Version: v1.0
7 # Description: This is a Script.
8 
9 `useradd $1`
10 echo "$2" |passwd --stdin "$1"

2. Use tar command to backup all log files under / var/log every 5 days to prevent file overwriting

vim backup.sh
tar zcfP /tmp/varlog_$(date +%F).tar.gz /var/log
crontab -e
* * * * 5 bin/bash /server/scripts/backup.sh


3. Real-time monitoring of local memory and hard disk remaining space, the remaining memory is less than 500 M, root partition remaining space is less than 1000M when sending alarm mail

1 #!/bin/bash
2 # Author: Peter zh
3 # Blog: https://home.cnblogs.com/zhangwduoduoj/
4 # Time: 2019-08-17 20:29:30
5 # Name: jiankong.sh
6 # Version: v1.0
7 # Description: This is a Script.
8 
9 free=`free -m|awk 'NR==3 {print$4 }'`
10 df=`df |awk 'NR==2{print $4}'`
11 
12 if [[ $free -lt 500 && $df -lt 1000000 ]];then
13 mail -s "Warning~" 1227566276@qq.com
14 echo 'send success!'
15 else
16 echo 'zhengchang!'
17 exit 0
18 fi

4. The script generates a random number of less than 100, prompting the user to guess the number, more user input, prompting the user to guess right, guess or guess small, until the end of the user guess the script.

 

5. Check whether the current user of this machine is a super administrator. If it is an administrator, install vsftpd with yum. If not, prompt you to use a string comparison version instead of an administrator.

1 #!/bin/bash
2 # Author: Peter zh
3 # Blog: https://home.cnblogs.com/zhangwduoduoj/
4 # Time: 2019-08-17 20:43:03
5 # Name: root.sh
6 # Version: v1.0
7 # Description: This is a Script.
8 
9 root=`id -u`
10 if [ $root -eq 0 ];then
11 yum install -y vsftpd
12 else
13 echo 'no super user~'
14 fi

6. The user is prompted to input three integers in turn, and the script outputs three digits in turn according to the size of the digits.

1 #!/bin/bash
2 # Author: Peter zh
3 # Blog: https://home.cnblogs.com/zhangwduoduoj/
4 # Time: 2019-08-17 20:45:50
5 # Name: 3geshu.sh
6 # Version: v1.0
7 # Description: This is a Script.
8 read -p "first:" a
9 read -p "second:" b
10 read -p "third:" c
11 
12 if [[ -z $a || -z $b || -z $c ]];then
13 echo "please input num---"
14 exit 1
15 fi
16 
17 if [[ -n "$(echo $a| sed -n "/^[0-9]\+$/p")" && -n "$(echo $b| sed -n "/^[0-9]\+$/p")" && -n "$(echo $c| sed -n "/^[0-9]\+$/p")" ]];then
18 
19 if [ $a -lt $b ];then
20 t=$a;a=$b;b=$t;
21 fi
22 if [ $a -lt $c ];then
23 t=$a;a=$c;c=$t;
24 fi
25 if [ $b -lt $c ];then
26 t=$b;b=$c;c=$t;
27 fi
28 echo " big --- small:$a,$b,$c"
29 
30 else
31 echo "dont abcd...."
32 fi

7. Write scripts to realize the man-machine Stone, Scissors, Cloth game

 

8. Write a script to test 192.168.4.0/24 which hosts are on and those hosts are off (for version)

1 #!/bin/bash
2 # Author: Peter zh
3 # Blog: https://home.cnblogs.com/zhangwduoduoj/
4 # Time: 2019-08-17 21:13:05
5 # Name: ping1.sh
6 # Version: v1.0
7 # Description: This is a Script.
8 for i in {1..254}
9 do
10 ping 192.168.4.$i -c 2 -w 1 2&>1 >/dev/null
11 if [ $? -eq 0 ];then
12 echo "192.168.4.$i active!!!"
13 else
14 echo "192.168.4.$i down..."
15 fi
16 done

9. Write a script to test 192.168.4.0/24 network segment which hosts are in the boot state, those hosts are in the shutdown state (multi-process version), define a function to achieve, ping a host, and detect the host's inventory status.

1 #!/bin/bash
2 # Author: Peter zh
3 # Blog: https://home.cnblogs.com/zhangwduoduoj/
4 # Time: 2019-08-17 21:18:43
5 # Name: ping2.sh
6 # Version: v1.0
7 # Description: This is a Script.
8 fun_ping(){
9 
10 ping 192.168.4.$i -c 1 -w 1 2&>1 >/dev/null
11 if [ $? -eq 0 ]
12 then 
13 echo "192.168.4.$i active!"
14 else 
15 echo "192.168.4.$i down."
16 fi 
17 }
18 for i in {1..254}
19 do 
20 fun_ping
21 done

10. Show progress bar when writing scripts and copying files

 

11, 9*9 multiplication tables (write shell scripts, print 9*9 multiplication tables)


12. Real-time display of packet traffic sent by eth0 network card using dead-cycle

1 #!/bin/bash
2 # Author: Peter zh
3 # Blog: https://home.cnblogs.com/zhangwduoduoj/
4 # Time: 2019-08-17 21:50:32
5 # Name: eth4.sh
6 # Version: v1.0
7 # Description: This is a Script.
8 while :
9 do
10 ifconfig eth4 |sed -n '5,6p'
11 done

13. Use the list of people in the user.txt file to automatically create the corresponding account in the computer and configure the initial password.
To execute the script, you need to prepare a user.txt file in advance, which contains a number of user information.

1 #!/bin/bash
2 # Author: Peter zh
3 # Blog: https://home.cnblogs.com/zhangwduoduoj/
4 # Time: 2019-08-17 21:27:13
5 # Name: zidonguser.sh
6 # Version: v1.0
7 # Description: This is a Script.
8 user=`cat /server/scripts/user.txt`
9 for i in $user
10 do
11 useradd $i
12 echo "123456" |passwd --stdin "$i"
13 done

14. Write batch modification extension scripts, such as batch modification of txt file to doc file
When executing a script, you need to add location parameters to the script
Script name txt doc (you can change the extension of txt to doc)
Script name doc jpg (you can change the extension of doc to jpg)

1 #!/bin/bash
2 # Author: Peter zh
3 # Blog: https://home.cnblogs.com/zhangwduoduoj/
4 # Time: 2019-08-17 18:58:14
5 # Name: rename.sh
6 # Version: v1.0
7 # Description: This is a Script.
8 read -p "input weizhi" $wz
9 for i in `ls $wz`
10 do
11 mv $i `echo $i|sed s#txt#doc#g`
12 
13 done

15. See how many remote IP is connected to the local machine (whether via ssh or web or ftp)
Using netstat-atn, you can view all the connection states of the local machine, -a to view all,
- t only displays tcp connection information, - n digital format
local address (the fourth column is local IP and port information)
Foreign address (the fifth is IP and port information for remote hosts)
Use the awk command to display only the fifth column of data and then the first column of IP address information.
sort can be sorted by the size of the number, and the redundant duplicates can be deleted by Uniq, and the number of duplicates can be counted.

#netstat -atn | awk -F"[: ]+" '{print $5}' Port number
netstat -atn |egrep ":80|:22|:21" | awk '{print $5}' |awk -F ":" '{print $1}' |sort -rn |uniq -c

16. The sum of all positive integers within 100 (1+2+3...). + 100)
eq 100 can generate 100 integers automatically and quickly

1 #!/bin/bash
2 # Author: Peter zh
3 # Blog: https://home.cnblogs.com/zhangwduoduoj/
4 # Time: 2019-08-17 21:10:05
5 # Name: qiuhe.sh
6 # Version: v1.0
7 # Description: This is a Script.
8 
9 sum=0
10 for i in `seq 100`
11 do
12 let sum+=i
13 done
14 echo $sum
15 

Keywords: Linux less vsftpd yum network

Added by twistedmando on Thu, 15 Aug 2019 15:44:33 +0300