Exercise 1:Monitor network card
requirements: every 10 minutes to detect the specified network card traffic, if the network card traffic is 0, restart the network card! (Use cron Task Scheduler!) )
Reference Answer:
#!/bin/bash# date:2018 March 21 lang=enn1= ' sar-n DEV 1 60|grep ens33|grep-i Average|awk ' {print $} ' |sed ' s/\.//g ' n2= ' Sar- N DEV 1 60|grep ens33|grep-i average|awk ' {print $6} ' |sed ' s/\.//g ' if [$n 1 = = ' "] && [$n 2 = ="]then " Ifdown Ens33 ifup Ens33fi
Exercise 2: Judging whether the date is legal
Requirements: Use the shell script to determine whether the date entered is legal. is to judge the date is a true date, such as 20170110 is the legal date, 20171332 is not legal.
Reference Answer:
#!/bin/bash# date:2018 March 21 if [ $# -ne 1 ] | | [ ${#1} -ne 8 ]then echo "Usage: bash $0 YYYYMMDD " exit 1fidate=$1year=${date:0:4}month=${date:4:2}day=${date:6:2}if echo $day |grep -q ' ^0 ' then day= ' echo $day |sed ' s/^0//' fiif cal $month $year > /dev/null 2> /dev/nullthen daym= ' cal $month $year |grep -v "$year" |grep -w "$day" if [ "$daym" != " ] then echo "OK" else echo "Error:please input a right date" exit 1 fielse echo "Error: please input a right date" exit 1fi
Exercise 3: checking Services
Requirements: first determine whether to install HTTP and MySQL, no installation to install, installed to check whether to start the service, if not started, you need to start the service.
Reference Answer:
#!/bin/bash# date:2018 year March 21 If_install () { n= ' rpm -qa|grep -cw ' if [ $n -eq 0 ] then echo "$1 not install." else echo "$1 installed." fi}if_install httpdif_install mysqlchk_ser () { p_n= ' PS -C "$" --no-heading |wc -l ' if [ $p _n -eq 0 ];then echo "$1 not start." systemctl start $1 else echo "$1 started." fi}chk_ser httpdchk_ser mysqld
Exercise 4:3-bit random numbers
Requirements: write a script to generate a random 3-bit number, and can be based on the user's input parameters to determine the output of a few groups. For example, the script name is number3.sh.
Execution method:
Bash number3.sh
Produces a set of 3-digit numbers directly.
Bash number3.sh 10
Plug in 10 groups of 3 digits.
Idea: You can use the Echo $RANDOM to get a random number, then divide by 10, take the remainder to get 0-9 random numbers, and get a set of three operations.
Reference Answer:
#!/bin/bash# date:2018 March 21 Get_num () { n=$[$RANDOM%10] echo $n}get_numbers () { for i in 1 2 3 do a[$i]= ' Get_num ' done echo "${a[*]}"}if [ -n "$ ];then m= ' echo $1 |sed ' s/[0-9]//g ' if [ -n ' $m ' ];then echo "Usage: bash $0 n, n is a number." exit 1 else for i in ' seq 1 $1 ' do get_numbers &nbsP; done fielse get_numbersfi
Exercise 5: three lines change line
Reference Answer:
Shell exercises (15)