Shell Scripting Exercises
1, write script/root/bin/checkdisk.sh, check the disk partition space and inode utilization, if more than 80%, the broadcast warning space will be full
#!/bin/bash# script/root/bin/checkdisk.sh, check the disk partition space and inode utilization, if more than 80%, the broadcast warning space will be full #********************************** #!/bin/bashdisk_max=$ (DF |grep "SDA" |grep-o "[: digit:]]\{1,3\}% "|tr-d"% "|sort |tail-1" inode_max=$ (df-i|grep "SDA" |grep-o "[[:d igit:]]\{1,3\}%" |tr-d "%" |sort | TAIL-1) [$disk _max-gt 80-o $inode _max-gt] && wall space would be full
2, write the script/bin/per.sh, judge the current user to the specified parameter file, is unreadable and not writable (available shadow file validation)
#!/bin/bash# writes the script/bin/per.sh, determines whether the current user is not readable and is not writable for the specified parameter file #******************************************************** Read-p "Please enter the file:" Fileif [! -R $file-A! -W $file]; Then echo "Unreadable and not writable" fi
3, Write script/root/bin/excute.sh, determine whether the parameter file is an sh suffix of the ordinary file, if it is, add everyone can execute permissions, otherwise prompt the user non-script file (create a file test.sh authentication)
#!/bin/bash# script/root/bin/excute.sh, determine whether the parameter file is the sh suffix of the ordinary file, if it is, add everyone can hold # permission, otherwise prompt the user non-script file #**************************** Read-p "Please input a filename:" Fileif [[$file =~. *sh$] ; Then if [-f $file]; Then chmod a+x $file echo "Success" fielse Echo ' non-script file ' fi
4, write Scripts/root/bin/nologin.sh and login.sh, implement prohibit and allow ordinary user login system
#!/bin/bash# writes scripts/root/bin/nologin.sh and login.sh to enable the prohibition and the admission of ordinary user login system #************************************************ Read-p "Please enter prohibited User:" useruid=$ (id $user |tr "" @ |cut [email protected]-F1 |grep -O "[0-9]\{3,\}") [$uid-ge] && usermod-s/bin/nologin $user | | Echo ' This user is a system user and cannot be banned! ' Echo ' The user has forbidden login '
#!/bin/bashread-p "Please enter the user allowed to log in:" Useruid=$ (id $user |tr "" @ |cut [email protected]-f1 |grep-o "[0-9]\{3,\}") [$uid- GE && usermod-s/bin/bash $user | | Echo ' This user is a system user ' echo ' that the user has allowed to login '
5, let all users of the PATH environment variable value One more path, for example:/usr/local/apache/bin
echo "path= $PATH:/usr/local/apache/bin" >>/etc/profile.d/path.sh
6, write the user's environment initialization script reset.sh, including aliases, login prompt, vim settings, environment variables, etc.
Once the reset.sh is executed, the following is set
Create an alias for the Ipconfig feature that shows the IP of the first NIC
Set the prompt to Green
Set VIM to automatically display line numbers
Set the Welcome screen after login (content customization)
#!/bin/bash# writes the user's environment initialization script reset.sh, including aliases, login prompt, vim settings, environment variables, etc. once the reset.sh is executed, the following is set up # Create an alias for the Ipconfig function is to display the first NIC ip# Set the prompt to Green # set Vim to automatically display line numbers # Set the Welcome screen after login (content customization) #*********************************************************************** Cat <<eof >> ~/.bashrcalias ipconfig= ' ifconfig etho ' ps1= ' \[\e[ 32;40m\][\[email protected]\h \w]\$ ' eofcat <<eof >> ~/.vimrcset Nueofecho ' echo Welcome ' >>/etc/ Bashrc
7, write a script to create users, the following features are required
A. Prompt the user to enter the user name that you want to create (when the user times out for 8 seconds, the prompt times out and exits)
B. Detect if the user name already exists, and if present, prompt the user name already exists and exits.
C. The user is prompted to continue to set the password after creation, if the user enters Yes, yes, Y, Y will continue to the next step, such as input other, or timeout 8 seconds to exit.
D. Next, set the password for the user, first prompt the user to enter the password to be set, the password input process is not visible.
E. To detect the length of the password entered by the user in the previous step, if less than 5 bits (including 5 bits), prompt the user password is too short, and exit.
F. To detect the complexity of the password entered by the user in step D, if the input is exactly the same as a line entry in the/usr/share/dict/words dictionary, then prompt the password is a common word and exits.
G. Once again let the user enter the password, if the second input password and in step D input inconsistent, prompts the user two times password inconsistent and exit.
After the H.d to G step is all passed, set the password for the user and prompt for the password to be set successfully. Finally exits correctly.
All of the above exit conditions should give different return values.
#!/bin/bash#**************************************** #a. Prompts the user to enter the user name that you want to create (when the user times out for 8 seconds, the prompt times out and exits) # B. Detect if the user name already exists and, if present, the user name already exists and exit #c. Prompts the user to continue setting the password after user creation is complete, and if the user enters Yes, yes, Y, Y, continue to the next step, such as entering another, or 8 seconds to exit. #d. Next, set the password for the user, first prompt the user to enter the password to be set, the password input process is not visible #e. Length detection of the password entered by the user in the previous step, if less than 5 bits (including 5-bit), prompt the user password is too short, and exit # F. For complexity detection of the password entered by the user in step D, if the input is exactly the same as a line entry in the/usr/share/dict/words dictionary, the prompt password is a common word and exits # G. Once again let the user enter the password, if the second password entered inconsistent with step D input, the user is prompted two times password inconsistency and exit #h.d to the G step after all passed, after the user set the password, and prompted the password has been successfully set. Finally exits correctly. #以上的所有退出情况应给出不同的返回值. #*****************************************read -t 8 -p "Please enter the user: created" userif [ "$ (echo $?)" -ne "0" ] ; then echo ' timeout ' exit 1fia=$ (cat /etc/passwd |cut -d: -f1 |grep "$user \>") if [ -n "$A" ] ; then echo ' user already exists ' &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; exit 2fiuseradd $userread -t 8 -p "Set password? " answer | | exit 3case $answer in yes | yes | y | Y ) read -s -p "Please enter at least five-digit password: " password if [ "$ (echo $password |grep -o "." |grep -c ".") " -le "5" ] ; then echo ' password too short ' exit 4 else if [ "$password" == "$ (cat /usr/share/dict/words |grep -o " \< $password \> " ) " ] ; then echo ' password is a common word ' exit 5 fi fi ;; * ) exit 6 ;; esacread -p "Please enter your password again: " password_confirmif [ "$password" == "$password _confirm " ] ; then echo ' password set success ' exitelse echo ' Two times password input inconsistent ' exit 7fi
Linux Learning-Scripting exercises