SHELL script activity
1. Write a script sumid.sh to calculate the sum of the ID of the 10th and 20th households in the / etc/passwd package
#! / bin/bash
S_O=head / etc/passwd | tail-1 | cut-d:-f3
S_T=head-20 / etc/passwd | tail-1 | cut-d:-f3
The sum of the ID of the 10th and 20th users in the echo-e "/ etc/passwd file is:\ e [1 × 33m$ [${Simpo} + ${Simpt}]\ e [0m"
two。 Write a script sumspace.sh, pass two parts paths as parameters to the script, and calculate the sum of all empty parts in the two parts.
#! / bin/bash
Read-p "please input One path:" ONE_PATH
Read-p "please input Two path:" TWO_PATH
S_ONE=grep "^ $" ${ONE_PATH} | wc-l
S_TWO=grep "^ $" ${TWO_PATH} | wc-l
Echo "file spaces is: ${S_ONE} ${S_TWO}"
Sum=$ [${S_ONE} + ${S_TWO}]
Echo-e "${ONE_PATH}, ${TWO_PATH} space sum is\ e [31m ${sum}\ e [0m"
3. Write the script sumfile.sh, count the number of level records and artifacts in / etc, / var, / usr records
#! / bin/bash
Read-p "please input first_dir_path:" FIRST
Read-p "please input second_dir_path:" SECOND
Read-p "please input three_dir_path:" THREE
FIR_F=find ${FIRST}-maxdepth 1 | wc-l
SEC_F=find ${SECOND}-maxdepth 1 | wc-l
THR_F=find ${THREE}-maxdepth 1 | wc-l
FIR_S=find ${FIRST}-maxdepth 1-type d | wc-l
SEC_S=find ${SECOND}-maxdepth 1-type d | wc-l
THR_S=find ${THREE}-maxdepth 1-type d | wc-l
Fir_dir=$ [${FIR_S} + ${SEC_S} + ${THR_S}-3]
Echo-e "\ e [33m ${FIRST}, ${SECOND}, ${THREE}\ e [0m three first dirs sum:\ e] 31m ${fir_dir}\ e [0m"
SUM=$ [${FIR_F} + ${SEC_F} + ${THR_F}]
Echo-e "\ e [1x 33m ${FIRST}, ${SECOND}, ${THREE}\ e [0m three files sum:\ e [1x 31m $[${SUM}-${fir_dir}]\ e [0m"
4. Write the script argsnum.sh and accept the number of parameters as parameters; if the number of parameters is less than 1, the user should be given one less parameter and quit immediately; if the number of parameters is not less than 1, the number of empty parameters in the package pointed to by the first parameter will be displayed
#! / bin/bash
[[$#-lt 1]] & & (echo "at least one argument" & & exit) | | echo-e "\ e [1x 31mgrep'^ $'$1 | wc-l\ e [0m"
5. Write a script hostping.sh that accepts the IPv4 addresses of each host as a parameter to test whether it is reachable. If ping is accessible, the IP address is accessible to the subscriber; if ping is not accessible, the IP address is inaccessible.
#! / bin/bash
Read-p "please input IP:" IP
LOG_COLOR='\ e [1x 31m'
AND_COLOR='\ e [0m'
A = [["$IP" = ~ ^ [0-9] {1Magne 3}. [0-9] {1pr 3}. [0-9] {1pr 3}. [0-9] {1pr 3} $]; echo $?
B=ping-C1-W1 $IP & > / dev/null;echo $?
If [$A-eq 0]; then
If [$B-eq 0]; then
Echo-e "${LOG_COLOR} this IP address can access ${AND_COLOR}"
Else
Echo-e "${LOG_COLOR} this IP address is not accessible ${AND_COLOR}"
Fi
Else
Echo-e "${LOG_COLOR} IP format incorrect input ${AND_COLOR}"
Exit
Fi
6. Write a script checkdisk.sh to check the disk partition space and the inode utilization rate. If it exceeds 80%, it will broadcast a warning that the space will be full.
#! / bin/bash
Disk=df | egrep / dev/sd | tr-s'%'| cut-d%-f5 | sort-nr | head-N1
Inode=df-I | egrep / dev/sd | tr-s'%'| cut-d%-f5 | sort-nr | head-N1
[$disk-ge 80-o $inode-ge 80] & & wall space will full.
7. Write a script per.sh to determine whether the current user pair of specified parameters is unreadable and unwritable.
#! / bin/bash
[!-r $1-a!-w $1] & & echo "$1 is not read and write" | | exit
8. Write a script excute.sh to determine whether the parameters are ordinary ones with the sh suffix, and if so, add all the executable permissions, otherwise mention the user's script.
#! / bin/bash
[[$1 = ~ .sh $]] & & [- f $1] & & (chmod axix $1 echo "$1 is not") | | echo "$1 is not
.sh "
9. Write scripts nologin.sh and login.sh to disable and allow ordinary customers to log in to the system.
#! / bin/bash
[- f / etc/nologin] & & echo "nologin" | | (touch / etc/nologin;echo "nologin")
! / bin/bash
[- f / etc/nologin] & & (rm-f / etc/nologin;echo "login") | | echo "login"
10. Write a script createuser.sh to achieve the following functions: make the user name of the specified parameter as a parameter, and if the user of the specified parameter exists, it will show that it exists, otherwise add it; show the information such as the id number of the added account
#! / bin/bash
Read-p "please input username:" user
Id $user & > / dev/null
[! $?-eq 0] & & (useradd $user & > / dev/null & & echo "add $user user") | | echo "the user is exits"
11. Write a script yesorno.sh to check whether the user input yes or no, and determine whether the input is yes or no, or other information.
#! / bin/bash
Read-p "please input yes or no:" ANS
[["$ANS" = ~ ^ (Yy?) $]] & & echo yes
[["$ANS" = ~ ^ (Nn?) $]] & & echo no
twelve。 Write a script filetype.sh to determine the path of household input parts, and show the type of parts (normal, record, link, other parts).
#! / bin/bash
Read-p "please input file path:" PT
File $PT > / testdir/shell/test.txt
Leixing=egrep-o "link | text | block | directory" / testdir/shell/test.txt
Case $leixing in
Text)
Echo "the path is file"
Link)
Echo "the path is Link"
Block)
Echo "the path is Block"
Directory)
Echo "the path is Directory"
*)
Echo "the path is Others"
Esac
13. Write a script checkint.sh to determine whether the parameter of "household input" is a positive integer.
#! / bin/bash
Read -p "please input a number:" num
[[$num = ~ ^ [0-9] + $]] & & echo "$num is int" | {echo "lease input a number"; exit;}
14. Make the value of the PATH environment variable of all subscribers more than one path, for example
/ usr/local/apache/bin
Vim / etc/profile
Export PATH=/usr/local/apache/bin:$PATH
Source / etc/profile effective
15. When the subscriber root logs in, change the command pointer to red, and activate the following alias:
Rm='rm-i'
Cdnet='cd / etc/sysconfig/network-scripts/'
Editnet='vim / etc/sysconfig/network-scripts/ifcfg-eth0'
Editnet='vim / etc/sysconfig/network-scripts/ifcfg-eno16777736 or
Ifcfg-ens33'
(if the system is CentOS7)
[root@CentOS] # vim ~ / .bashrc
Alias rm='rm-i'
Alias cdnet='cd / etc/sysconfig/network-scripts/'
Alias editnet1='vim / etc/sysconfig/network-scripts/ifcfg-eth0'
Alias editnet2='vim / etc/sysconfig/network-scripts/ifcfg-ens33'
Export PS1=' [\ e [1bot 31m] [\ u@\ h\ W]\ $[\ e [0m] 'red
Source. Bashrc takes effect.
16. When any customer logs in to the system, the red letter warning message "Hi,dangerous!" is displayed.
[root@CentOS ~] # vim / etc/issue
^ [031m "Hi,dangerous!" ^ [0m "
17. Write a script in a basic format, including author, contact, version, time, description, etc.
[root@CentOS ~] # vim .vimrc
Set nu
Set ignorecase
Set cursorline
Set autoindent
Autocmd BufNewFile * .sh exec ": call SetTitle ()"
Func SetTitle ()
If expand ("%: e") = = 'sh'
Call setline (1, "#! / bin/bash")
Call setline (2, "#")
Call
Setline (3, "#"
)
Call setline (4, "# Author: W")
Call setline (5, "# QQ: 1")
Call setline (6, "# Date:" .strftime ("% Y-%m-%d"))
Call setline (7, "# FileName:" .clients ("%"))
Call setline (8, "# URL: http://www.magedu.com")
Call setline (9, "# Description: The test script")
Call setline (10, "# Copyright (C):" .strftime ("% Y"). "All rights
Reserved ")
Call
Setline (11, "#
")
Call setline (12, ")
Endif
Endfunc
Autocmd BufNewFile * normal G
18. Write a script systeminfo.sh that displays the current host system information, including hostname, IPv4 address, operating system version, kernel version, CPU model, memory storage, hard disk storage.
#! / bin/bash
LOG_COLOR='\ e [1x 33m'
AND_COLOR='\ e [0m'
Echo-e "current host information is as follows:"
Echo-e "hostname: ${LOG_COLOR} ${HOSTNAME} ${AND_COLOR}"
Echo-e "IPV4 address is: ${LOG_COLOR} ifconfig | grep broadcast | tr-s"| cut-d"- f3" ${AND_COLOR}
The operating system version of echo-e is: ${LOG_COLOR} uname-r "${AND_COLOR}
Echo-e "kernel version is: ${LOG_COLOR} cat / etc/redhat-release | cut-d". "- F1-2" ${AND_COLOR}
Echo-e "CPU model is: ${LOG_COLOR} lscpu | grep Model | tail-1 | tr-s'| cut-d:-f2" ${AND_COLOR}
Echo-e "memory size is ${LOG_COLOR} free-mh | head-2 | tail-1 | tr-s'| cut-d"- f2" ${AND_COLOR}
Echo-e "hard disk capacity is ${LOG_COLOR} fdisk-l | head-2 | tail-1 | awk-F': |,'{print $2}'" ${AND_COLOR}
19. Write a script backup.sh to back up / etc/ records to / root/etcYYYY-mm-dd every time
#! / bin/bash
Cp / etc / testdir/etcdate +% F
Echo "/ etc backup complete"
20. Write a script disk.sh to show the most space efficient value in the current hard disk partition
#! / bin/bash
Echo "the maximum space utilization in the current hard disk partition is: df | grep / dev/sd | grep-o" [0-9] {1jue 3}% "| sort-rn | head-1"
21. Write a script links.sh that shows the IPv4 address and number of connections of each remote host that is connecting to this host, and sorts the number of connections from "to"
#! / bin/bash
Echo "the address and number of connections of the IPV4 of each remote host that is connecting to this host is:"
Echo "netstat-tan | tr-s": | cut-d:-f6 | grep ^ [0-9] | sort | uniq-c"
twenty-two。 Write a script reset.sh to configure the environment for the new system
#! / bin/bash
# Font color
LOG_COLOR='\ e [1x 33m'
AND_COLOR='\ e [0m'
# name of the network card
N_N=ifconfig | head-1 | tr-s "": | cut-d:-F1
# directory where the Nic configuration file is located
DateNet = "/ etc/sysconfig/network-scripts/ifcfg-$ {net}"
# directory where the Yum source configuration file is located
Dumped YUM = "/ etc/yum.repos.d/"
# turn off seliunx
Setenforce 0 & > / dev/null
Sed-I's # ^ SELINUX=.#SELINUX=disabled#g' / etc/selinux/config
Echo-e "${LOG_COLOR} SELINUX has been modified and the restart will take effect. ${AND_COLOR}"
Read-p "is about to change the host name. Please give the host a lovely name:" HOST.
# change the hostname
Hostname ${HOST}
Cat > / etc/hostname / dev/null
Mkdir ${D_YUM} backup & > / dev/null
Mv ${D_YUM} C ${D_YUM} backup
Cat > > ${D_YUM} CentOS.repo / etc/fstab
Yum clean all & > / dev/null
Yum makecache & > / dev/null
Echo-e "${LOG_COLOR} yum configuration complete. ${AND_COLOR}"
# turn off the firewall
Systemctl stop firewalld.service & > / dev/null
Systemctl disable firewalld.service & > / dev/null
Echo-e "${LOG_COLOR} firewall has been turned off and boot ${AND_COLOR} is prohibited"
# change IP
Read-p "next configure IP\ nType the IP address:" IPAD
Read-p "Please type the subnet mask:" NET
Read-p "Please type the gateway address:" GAT
Sed-I's # ^ BOOT.*#BOOTPROTO=static#g' ${D_NET}
Cat > > ${D_NET}