30-way Shell exercises

Source: Internet
Author: User
Tags parent directory touch command disk usage

1 First Test "/etc/vsftpd", "/etc/hosts" is the directory, and through the "$?" Variable view returns a status value to determine the test result.

[Email protected] ~]# [-d/etc/vsftpd][[email protected] ~]# echo $?1[[email protected] ~]# [-d/etc/hosts][[email Protected] ~]# echo $?1



2 Test the existence of "/media/cdrom/server" and its parent directory, or "YES" if present, or output no information.

[Email protected] ~]# if [-d/media/cdrom] && [-f/media/cdrom/server]; Then echo "Yes"; Fi


3 log in with a normal user teacher and test if the "/etc/passwd" file has read, write permissions, or "Yes" if it is.

[[Email protected] ~]$ [-R/ETC/PASSWD] && echo "yes" yes[[email protected] ~]$ [-W/ETC/PASSWD] && Ech O "Yes"


4 test if the number of users currently logged on to the system is less than or equal to 10, the output is "yes".

[[Email protected] ~]# [' Who | wc-l '-le] && echo "Yes" Yes


5 Extract the disk usage of the "/boot" partition and determine if it is more than 95% (for ease of understanding, operation steps to properly decompose)

[[Email protected] ~]# [' df | grep '/boot ' | awk ' {print $} ' | awk-f '% ' {print '} '-ge-up] && Echo ' Warning "


6 Prompt the user to enter a file path and determine if it is "/etc/inittab", if the output is "yes"


[Email protected] ~]# read-p "localtion:" Filepathlocaltion:/etc/inittab[[email protected] ~]# [$FIlePath = '/etc/initt AB '] && echo "Yes" Yes




7 If the content of the current environment variable Lang is not "en." US ", the value of the lang variable is output, otherwise there is no output.

[Email protected] ~]# if [' echo $LANG '! = ' en.us ']; then echo $LANG; Fien_us. UTF-8

8 Use the Touch command to create a new file, test whether its contents are empty, write to the file, and test again

[[email protected] ~]# Touch 1.txt[[email protected] ~]# [-Z 1.txt] && echo "yes" [[email protected] ~]# [-Z ' CA T 1.txt '] && echo "yes" yes[[email protected] ~]# echo 123 > 1.txt[[email protected] ~]# [-Z ' cat 1.txt '] &am p;& echo "Yes"


9 Test If the current user is teacher, if not teacher

[[Email protected] ~]# if [' echo $USER '! = ' teacher ']; then echo "Not teacher"; Finot teacher


10 only one of the "/etc/rc.d/rc.local" or "/etc/init.d/rc.local" is a file, then "YES" is displayed, otherwise there is no output.

[[Email protected] ~]# [-f/etc/rc.d/rc.local] | | [-f/etc/init.d/rc.local] && echo "Yes" Yes


11 Test if the "/etc/profile" file has executable permissions, and if you do not have executable permissions, then prompt "no x mode." The information.

[[Email protected] ~]# [! -x/etc/profile] && echo "no x mode" no X mode

12 If the current user is root and the shell program used is "/bin/bash", then "YES" is displayed, otherwise there is no output.

[[Email protected] ~]# [' echo $USER ' = = ' root '] && [' echo $SHELL ' = = '/bin/bash '] && echo "Yes" Yes


13 Check the existence of "/var/log/messages" file, if there is the number of rows and output of the statistics file content, otherwise do nothing (reasonable use of variables, can improve writing efficiency).

[[Email protected] ~]# [-f/var/log/messages] && echo ' wc-l/var/log/messages ' 6/var/log/messages


14 The user is prompted to specify the path to the backup directory, and if the directory already exists, it is skipped after displaying the prompt, otherwise the directory is created after displaying the appropriate message.

#!/bin/bashread-p "Please import a backup dirname:" Bakdirif [! -D $BakDir] then mkdir $BakDirfi


15 count the number of users currently logged in to the system, and determine whether more than three, if the actual number and give a warning message, otherwise list the login user account name and the terminal.

#!/bin/bashread-p "Please import a backup dirname:" Bakdirif [! -D $BakDir]thenmkdir $BakDirfi

16 Check to see if the Portmap process already exists and output "Portmap service if running" if it already exists. Otherwise check for the existence of a "/etc/rc.d/init.d/portmap" executable script, and then start the Portmap service if it exists. Otherwise prompt "No Portmap script file."

#!/bin/bashprocess= ' ps aux | grep Portmap | grep-v grep ' If [-N $Process]; thenif [!-f/etc/rc.d/init.d/portmap]; Thenecho "No portmap scipt file" Fielseecho "Portmap Sercice is running" fi

17 Monitor the running state of the MYSQLD service every five minutes, append the log information (including time) to the "/var/log/messages" file, and restart the MYSQLD service if the mysqld process is found to be terminated;

#!/bin/bashprocess= ' Ps-ef | grep "Mysqld_safe" | Grep-v "grep" | Wc-l ' if [$Process! = 0]; Thenecho "Mysql process is running" >>/dev/nullelseecho "' Date +"%y-%m-%d%h:%m:%s "' Error:mysql process was not r Unning ">>/var/log/messages/etc/init.d/mysqld start >>/DEV/NULLFI*/5 * * * */bin/bash/root/1.sh



18 output three text messages in turn, including "Moring", "Noon", "Evening" string in one day.

#!/bin/bashfor TM in "Morning" "Noon" "Evening" does echo "the $TM of the day." Done

19 for system users who use "/bin/bash" as the login shell, check the number of subdirectories or files they have in the "/opt" directory, and if more than 100, list the specific values and corresponding user accounts

#!/bin/bashuser= ' grep '/bin/bash '/etc/passwd | Awk-f ': ' {print $} ' number= ' Find/opt-user $User | Grep-v ^/opt$ | Wc-l ' If [$Number-gt 100]; Thenecho $User: $Numberfi


20 calculating the total amount of space occupied by all "*.conf" profiles in the "/etc" Directory

#!/bin/bashfilesize=$ (Ls-l $ (find/etc-type f-name *.conf) | awk ' {print $} ') Total=0for I in $FileSizedototal =$ (expr $total + $i) doneecho "Total size of conf file is $total"


21 The user enters an integer greater than 1 from the keyboard, such as 50, and calculates the number of integers from 1 to

#!/bin/bashsum=0read-p "InPort a >1 sumber:" Numberfor i in $Numberdosum =$[$Number + $i]doneecho "Sum is $sum "


22 Batch Add 20 system user accounts, the user name is "stu1", "STU2", "Stu3" 、...... "Stu20", each user's initial password is set to "123456"


23 Write a script that deletes the user in bulk, removing the 20 users added in the previous example.

#!/bin/bashif [$ = = "Add"]; thenfor i in ' seq 1 ' douseradd stu$i >>/dev/nullecho 123 | passwd--stdin Stu$iecho "stu$i is Create" doneelif [$ = = "Del"]; thenif [-N ' grep stu/etc/passwd ']; Thenecho "Stu U Ser isn't create,please inport options add "Exit 1elsefor i in ' seq 1 ' douserdel-r stu$iecho" stu$i is Remove "donefiels Eecho "Please inport options Add or del" fi



24 The user enters a character from the keyboard and determines whether the character is a letter, number, or other character, and outputs the appropriate message.

#!/bin/bashread-p "Please input a character:" Cham= ' echo $Cha | Sed s/[a-za-z]//g ' n= ' echo $Cha | Sed s/[0-9]//g ' s= ' echo $Cha | Sed s/[a-za-z0-9]//g ' If [-Z $m]; Thenecho "Letter" Elif [-Z $n]; Thenecho "Number" elif [-Z $s]; Thenecho "Digital letter" Elseecho "your input is special character" fi


25 Write a shell program, calculate the value of multiple integers, and the values that need to be computed are given by the user as command-line arguments when executing the script

[email protected] ~]# cat 1.sh#!/bin/bashecho $[$1+$2+3]


26 loop prompts the user to enter a string, and saves each input to the temporary file "/tmp/input.txt", when the user enters the "END" string to exit the loop body, and to count the number of lines in the Input.txt file, the number of words, the number of bytes and other information, after the statistics to delete the temporary file

#!/bin/bashwhile truedoread-p "Please input a string:" Strecho $Str >>/tmp/test.txtif [$STR = = "End"];thenbreak Fidonewc/tmp/test.txt && Rm-rf/tmp/test.txt

27 Delete stu1~stu20 user accounts in the system, except Stu8, stu18

#!/bin/bashfor i in ' seq 1 ' doif [$i-eq 8] | | [$i-eq 18]; Thencontinuefiuserdel-r stu$i >>/dev/nulecho ' stu$i is remove ' done


28 Define a Help function in the script, load the function when the user enters a script parameter other than "Start" or "Stop", and give the information about the command usage, otherwise give the corresponding prompt

#!/bin/bashhelp () {echo "Usage:" $ "start|stop"}case "$ Instart) echo" Starting ... ";; Stop) echo "stoping ...";; *) Helpesac


29 defines an addition function in the script that calculates the sum of two numbers and calls the function to calculate 12+34, 56+789, and

#!/bin/bashsum () {echo ' expr $ + $}sum 34sum 56 789


30 from 1 to 100

#!/bin/bashfor i in ' seq 1 ' do sum=$[$sum + $i] echo $idone echo $sum



This article is from the "Wesley" blog, please be sure to keep this source http://szk5043.blog.51cto.com/8456440/1892069

30-way Shell exercises

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.