1, write script/root/bin/systeminfo.sh, display the current host system information, including host name, IPV4 address, operating system version, kernel version, CPU model, memory size, hard disk size.
#!/bin/bash# author:huiping# version:1.0.1# date:2016-08-11# describe: Displays the current host system information, Includes host name, IPV4 address, operating system version, kernel version, CPU model, memory size, hard disk size echo "the hosname is: $ (hostname)" echo "the kernel version is: $ (uname -r) "echo " the server_ip is: $ ( ifconfig |grep ' inet\b ' |grep -v ' 127.0.0.1 ' |sed ' s/.*addr:\b//' |sed ' s/bcast.*//') "echo " the cpu is: $ (lscpu |grep -i ' Model name ' |tr -s ' ' |sed ' s/.*://') "echo " the os version is: $ ( cat /etc/redhat-release) "echo " the memorysize is: $ (free |sed -n ' 2p ' |tr : ' ' |tr -s ' ' |cut -d ' ' -f2 ' "echo " the disksize is: $ (fdisk -l |sed -n ' 2p ') "[[Email protected] bin] # bash -n&Nbsp;systeminfometion.sh [[email protected] bin]# bash systeminfometion.sh the hosname is: localhost.localdomainthe kernel version is: 2.6.32-642.el6.x86 _64the server_ip is: 10.1.253.35 the cpu is: intel (R) Core (TM) i3-2350m cpu @ 2.30ghzthe os version is: centos release 6.8 (Final) the memorysize is: 1004108the disksize is: disk /dev/ Sda: 128.8 gb, 128849018880 bytes[[email protected] bin]# bash -n systeminfometion.sh [[email protected] bin]# vim systeminfometion.shYou Have new mail in /var/spool/mail/root
2, scripting/root/bin/backup.sh, can be implemented daily/etc/directory backup to/root/etcyyyy-mm-dd
#!/bin/bash# author:huiping# version:1.0.1# date:2016-08-11# describe: Enabling daily backup of/etc/directories to/ROOT/ETCYYYY-MM-DD
Backdir= "/root/etc$ (date +%f)" Echo $BACKDIRCP-a/etc/. $backdir && echo "Backup finished" unset backdir[[email protected] ~]# lldrwxr-xr-x. 4096 root root 05:11 etc2016-08-11
3. Write script/root/bin/disk.sh, showing the maximum space utilization value in the current hard disk partition
#!/bin/bash# author:huiping# version:1.0.1# date:2016-08-11# describe: Displays the maximum space utilization value in the current hard disk partition
disk_max=$ (DF |tr-s '% |cut-d%-f5 |grep ' [0-9] ' |sort-nr |head-1) echo "The Diskused_max is $disk _max" unset disk_ma X
[Email protected] bin]# Bash disk.sh the Diskused_max is 53
4. Write Script/root/bin/links.sh, show the IPV4 address and connection number of each remote host connecting to this host, and sort by the number of connections from large to small
#!/bin/bash# author:huiping# version:1.0.1# date:2016-08-11# describe: Shows the number of IPV4 addresses and connections for each remote host connecting to this host, sorted by number of connections from large to small
Netstat-nt |tr-s ' |cut-d '-f5 |grep ' [0-9] ' |sed ' s/:.*//' |sort |uniq-c |sort[[email protected] bin]# bash link. SH 1 10.10.10.1 1 10.1.250.69 3 10.1.50.10
5, write a script/root/bin/sumid.sh, calculate the/etc/passwd file of the 10th user and the 20th user ID of the sum
#!/bin/bash# author:huiping# version:1.0.1# date:2016-08-11# describe: Calculates the sum of the 10th and 20th user IDs in the/etc/passwd file user10_id= $ (sed-n ' 10p '/etc/passwd |cut-d:-f3) user20_id=$ (sed-n ' 20p '/etc/passwd |cut-d:-f3) sumid=$[$user 10_id+ $user 20_id]ec Ho "The sum_id is: $sumid" unset user10_idunset user20_idunset Sumid
6, write a script/root/bin/sumspace.sh, pass two file paths as parameters to the script, calculate the sum of all the blank lines in these two files
#!/bin/bash# author:huiping# version:1.0.1# date:2016-08-11# describe: Pass two file paths as parameters to the script, calculate the sum of all the blank lines in these two files
file1=$ (grep ' ^$ ' $ |wc-l) file2=$ (grep ' ^$ ' $ |wc-l) sumspace=$[$file 1+ $file 2]echo "The total spacelines is $sumspace" u Nset file1unset file2[[email protected] bin]# bash sumspace.sh/etc/fstab/etc/profilethe Total Spacelines is 12
6, write a script/root/bin/sumfile.sh, statistics/etc,/var,/usr directory total number of sub-directories and files
#!/bin/bash# author:huiping# version:1.0.1# date:2016-08-11# describe: statistics/etc, Var,/usr Total number of sub-directories and files in the directory
etc_sum=$ (ls-a/etc |wc-l) var_sum=$ (Ls-a/var |wc-l) usr_sum=$ (ls-a/usr |wc-l) sumfile=$[$etc _sum+ $var _sum+ $usr _sum]e Cho "The total file was $sumfile" unset etc_sumunset var_sumunset usr_sum[[email protected] bin]# bash sumfile.sh the total File is 208
7, write a script/root/bin/argsnum.sh, accept a file path as a parameter, if the number of parameters is less than 1, the user is prompted "at least one parameter should be given" and immediately exit, if the number of arguments is not less than 1, the number of blank lines in the file pointed to by the first parameter is displayed
#!/bin/bash# author:huiping# version:1.0.1# date:2016-08-11# describe: Accepts a file path as a parameter, and if the number of parameters is less than 1, the user is prompted "at least one parameter should be given", and immediately exits; if the number of arguments is not less than 1, the number of blank lines in the file pointed to by the first parameter is displayed
[$#-lt 1] && echo "Please give a argument" | | grep ' ^$ ' $ |wc-l[[email protected] bin]# bash ARGSNUM.SH/ETC/FSTAB1
8, write a script/root/bin/hostping.sh, accept a host's IPv4 address as parameters, test whether it can be connected. If Ping is available, the user is prompted to "the IP address is accessible" and if it is not ping, the user is prompted "The IP address is inaccessible"
#!/bin/bash# author:huiping# version:1.0.1# date:2016-08-11# describe: Accept the IPV4 address of a host as a parameter to test whether it can be connected. If Ping is available, the user is prompted to "the IP address is accessible" and if it is not ping, the user is prompted "The IP address is inaccessible"
[$#-ge 1] && echo "$ |egrep-o" (\< ([1-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \.\< ([0-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \.) {2}\< ([0-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) ' &>/dev/null && (ping-c 3 &>/dev/null && ECHO-E "\e[31m This IP was accessed\e[0m" | | echo-e "\E[31M This IP was not accessed \e[0m") [[email protected] bin]# bash hostping.sh 10.1.1.1this IP isn't accessed [[email protected] bin]# bash hostping.sh 10.1.253.35this IP is accessed
9, Chmod-rw/tmp/file1, write script/root/bin/per.sh, determine whether the current user/tmp/fiile1 file is not readable and not writable
[Email protected] tmp]# CHMOD-RW File1[[email protected] tmp]# ll file1----------. 1 User1 user1 0 08:10 file1
#!/bin/bash# author:huiping# version:1.0.1# date:2016-08-11# describe: Determine if the current user is not readable and not writable by the/tmp/fiile1 file
[-r/tmp/file1-a-w/tmp/file1] && echo "yous can not to read and writ file1" [[email protected] bin]# bash per. SH yous can not to read and writ file1
10, write Scripts/root/bin/nologin.sh and login.sh, implement prohibit and allow ordinary user login system.
#!/bin/bash# author:huiping# version:1.0.1# date:2016-08-11# describe: Disable and allow ordinary user login system
[-f/etc/nologin] && (rm-f/etc/nologin;echo user enable login) | | echo User Disable login Already[[email protected] bin]# bash login.sh user Disable login already
11, write a script/root/bin/hostping.sh, accept a host of the IPV4 address as a parameter, first determine whether the IP, no, prompt IP format is not valid and exit, is, test whether can be connected. If Ping is available, the user is prompted to "the IP address is accessible" and if it is not ping, the user is prompted "The IP address is inaccessible"
#!/bin/bash# author:huiping# version:1.0.1# date:2016-08-11# describe: Accept a host's IPV4 address as a parameter, first determine whether the IP is qualified, no, prompt IP format is not valid and exit, is, test whether can be connected. If you can ping, prompt the user "This IP address is accessible"; if not ping, the user is prompted "The IP address is not accessible #!/bin/bash[ $# -ge 1 ] && echo " |egrep -o " (\< ([1-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \.\< ([0-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \.) {2}\< ([0-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) ' &> /dev/null && (ping -c 3 $1 &>/dev/null && echo -e "\e[31m this ip is accessed\e[0m " | | echo -e "\e[31m this ip is not accessed \e[0m") [[email protected] bin]# bash hostping.sh 10.1.1.1 this ip is not accessed [[email protected] bin]# bash hostping.sh 10.1.253.35 this ip is accessed
12, calculate the value of 1+2+3+...+100
#!/bin/bash# author:huiping# version:1.0.1# date:2016-08-11# describe: Calculate the value of 1+2+3+...+100
echo {1..10} |tr ' + |bc[[email protected] bin]# bash sum1.sh 5050i=1sum=0for i in {1..100};d o sum=$[$sum + $i] i= $i +1doneecho "The sum is $sum" unset iunset sum[[email protected] bin]# bash sum.sh the sum is 5050
13. Calculate the sum of all the numbers from the first parameter a of the script, to the second parameter B, to determine if B is greater than a, to indicate the error, and to exit.
#!/bin/bash# author:huiping# version:1.0.1# date:2016-08-11# describe: Calculates the sum of all the numbers from the first parameter a of the script to the second parameter B, judging if B is greater than a, If the error is not prompt and exits, the calculation
#!/bin/bash[[$1-lt [$]] && (echo $ (seq $) |tr "+ |BC) | | echo "Error,please input effective number" [[email protected] bin]# bash f1.sh 30420[[email protected] bin]# bash f1.sh 30error,please Input Effective number
Basic job of Shell script