Shell scripting exercises may continue to append ~ ~ ~

Source: Internet
Author: User

1, write script/root/bin/systeminfo.sh, display the current host system information, including hostname, IPV4 address, operating system version, kernel version, CPU model, memory size, hard disk size

#!/bin/bashecho "hostname: `hostname`"echo "ipv4 addr: `ifconfig |egrep -o "\<(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>" | head -1`"echo "ipv4 addr: `ifconfig ens33 | grep "inet " | tr -s " " | awk ‘{print $2}‘`"echo "OS version: `cat /etc/redhat-release`"echo "kernel version: `uname -r`"echo "CPU version: `lscpu | grep "Model name" | tr -s " " | cut -d: -f2`"echo "memery max_size: `cat /proc/meminfo | head -1 | cut -d: -f2`"echo "disk max_size: `fdisk -l | head -2 | tail -1| cut -d" " -f3,4`"  


2, scripting/root/bin/backup.sh, can be implemented to backup/etc/directory to/ROOT/ETCYYYY-MM-DD

#!/bin/bashecho "copy will start...."sleep 3cp -av /etc /root/etc`date +%F`                                                                                 echo "copy finished "


3. Write script/root/bin/disk.sh, showing the maximum space utilization value in the current hard disk partition

#!/bin/bashecho "the max value is : `df | grep "/dev/sd*" | tr -s " " "%" | cut -d% -f5 | sort -nr | head -1`"  


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/bashecho "the connecting hosts: ` netstat -tun | tr -s ‘ ‘ | awk ‘{print $5}‘ | grep -v "[a-zA-Z]" | uniq -c | sort -nr `"


5. Write script/root/bin/sumid.sh, calculate the sum of the ID of the 10th user and the 20th user in the/etc/passwd file

#!/bin/bash                                                                                                     Id1=`cat /etc/passwd | sed -n "10 p" | awk -F: ‘{print $3}‘`Id2=`cat /etc/passwd | sed -n "20 p" | awk -F: ‘{print $3}‘`echo "two user‘s id sum=$[$Id1+$Id2]"


6, write the script/root/bin/sumspace.sh, pass two file path as parameters to the script, calculate the sum of all the blank lines in the two files

#!/bin/bashread -p "please input file name no.1: " first_nameread -p "please input file name no.2: " second_name****f1=`cat $first_name | grep "^[[:space:]]*$" | wc -l`f2=`cat $second_name | grep "^[[:space:]]*$" | wc -l`echo "the two file space line sum=$[$f1+$f2]"


7, Scripting/root/bin/sumfile.sh, Statistics/etc,/var,/usr directory total number of sub-directories and files

#!/bin/bash                                                                                                   sum1=`ls -A /etc | wc -l`sum2=`ls -A /var | wc -l`sum3=`ls -A /usr | wc -l`echo "/etc /var /usr file sum=$[$sum1+$sum2+$sum3]"


8, write script/root/bin/argsnum.sh, accept a file path as a parameter, if the number of parameters is less than 9, the user is prompted "at least one parameter should be given" and immediately exit, if the number of parameters is not less than 1, the number of blank lines in the file pointed to by the first parameter is displayed

#!/bin/bash                                                                                                   # 接收一个文件路径作为参数,如果参数个数小于1,则提示用户至少应该给一个参数,并且立即退出,如果参数#个数不小于1 则显示第一个参数所指向的文件中的空白行数if (($#<"1"))        then                echo "至少应该给一个参数"        else                echo "space line: `cat $1 | grep "^[[:space:]]*$" | wc -l`"fi


9, write script/root/bin/hostping.sh, accept the IPV4 address of a host as parameter, test whether can connect. 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                                                                                                   ping -c1 $1 &>/dev/nullif (($?<"1"))        then                echo "this host is up"        else                echo "can not arrived"fi


10, 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/bashb_max=`df | grep -E "^/dev/sd*" | tr -s " " "%" | cut -d% -f5 | sort -nr |head -1`i_max=`df -i | grep -E "^/dev/sd*" | tr -s " " "%" | cut -d% -f5 | sort -nr |head -1`[ $b_max -gt 80 -o $i_max -gt 80 ] && wall hard disk full || echo "not warning"unset b_maxunset i_max


After the value is deliberately lowered

11, write the script/bin/per.sh, determine whether the current user to the specified parameter file, is not readable and not writable

#!/bin/bashread -p "please input file name: " fnif [ ! -r $fn -a ! -w $fn ]        then                echo "`whoami` can not read and write "        else                echo "`whoami` can read and write file"fi


12, write script/root/bin/excute.sh, determine whether the parameter file is the sh suffix of the ordinary file, if it is not empty, add everyone can execute permissions, otherwise prompt the user non-script file

#!/bin/bashread -p "please input filename: " fnif [[ $fn =~ ..*\.sh$ ]]        then                if [[ -s $fn && -f $fn ]]                        then                                chmod a+x $fn                                echo "OK"                        else                                echo "file is not suit"                                exit                fi        else                echo "please input suit file "                exitfi

1 and 1.sh are empty files per.sh is a shell script that was previously practiced

13, write Scripts/root/bin/nologin.sh and login.sh, implement prohibit and allow ordinary user login system

#!/binbashif [ -e /etc/nologin ]        then                echo "stop user loading "        else                touch /etc/nologin                echo "stop user load" fi        
#!/bin/bashif [ -e /etc/nologin ]        then                rm -rf /etc/nologin                echo "user can load"        else                echo "user can load"fi




Shell scripting exercises may continue to append ~ ~ ~

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.