Bash Little Little Script

Source: Internet
Author: User

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

[[email protected] scripts]# cat systeminfo.sh#!/bin/bashhostname= ' Uname -n ' Ipv4= ' ifconfig |sed -nr  ' 2s#.*addr: (. *)   bca.*$#\1#gp ' cpu= ' lscpu|sed -n  ' 13p ' |tr  -s  '   ' mem= ' free -hm |grep  "Mem" |tr -s  '   '   ': ' |cut -d:  -f2 ' disk= ' fdisk -l |grep  '/dev/sda: ' |sed -r  ' s#.*:  (. *),. *$#\1#g '     echo the hostname is:  $HostNameecho  the server_ip is: $ ipv4echo the opeartion system is: $ (cat /etc/redhat-release) echo the  kernel verison is: $ (uname -r) echo the cpuinfo is:  $Cpuecho  the  memery size is:  $Memecho  the Hardisk size is:  $Disk [[email  Protected] scripts]# sh systeminfo.shthe hostname is: centos6.localdomainthe  server_ip is: 10.1.249.94the opeartion system is: centos release 6.8  (Final) the  kernel verison is: 2.6.32-642.el6.x86_64the cpuinfo is: model name:  amd a8-4500m apu with radeon (tm)  hd graphicsthe memery size  is: 980mthe hardisk size is: 128.8 gb[[email protected] scripts] #

2, scripting/root/bin/backup.sh, can be implemented daily/etc/directory backup to/root/etcyyyy-mm-dd

[email protected] scripts]# cat backup.sh#!/bin/bashbackup_dir= "/tmp/etc-$ (date +%f)" cp-ar/etc/$backup _dir & & Echo "The $backup _dir is backup successfull" [[email protected] scripts]# sh backup.shthe/tmp/etc-2016-08-11 was back Up Successfull[[email protected] scripts]# ll/tmp/total dosage 40drwxr-xr-x. 122 root root 12288 August 19:03 etc-2016-08-11 [[email protected] scripts]#

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

[email protected] scripts]# cat disk.sh#!/bin/bashmaxuse= ' df |grep '/dev/sda ' |sed-r ' s#.* ([0-9]+%]. *#\1#g ' |sort-nr| Head-1 ' echo ' The maximum space utilization in the current hard disk partition is: $MaxUse "[[email protected] scripts]# sh disk.sh The maximum space utilization value in the current hard disk partition is: 22%[[email protected ] scripts]#

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

[email protected] scripts]# cat links.sh#!/bin/bashlinks= ' netstat-tn |grep tcp |tr-s ': ' |cut-d:-f6|sort-nr|uniq The number of IPV4 connections and addresses for each remote host of the-C ' echo-e ' host are: $Links "[[email protected] scripts]# sh links.sh host's IPV4 connections per remote host and address: 2 10.1.250.79 [Email protected] scripts]#

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

[[email protected] scripts]# cat sumid.sh#!/bin/bashuuid1= ' cat/etc/passwd|sed-n ' 10p ' |cut-d:-f3 ' uuid2= ' cat/etc/ Passwd|sed-n ' 20p ' |cut-d:-f3 ' sum=$ (($uuid 1+ $uuid 2) echo "10th user and 20th User ID sum: $sum" [[email protected] scripts]# sh Sumid.sh the sum of the 10th user and 20th User ID is: 180[[email protected] scripts]#

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

[[email protected] scripts]# cat sumspace.sh#!/bin/bash[$#-lt 2] && echo "Please input path:" && ex It 1 args1= ' grep ' ^$ "$ |wc-l ' args2= ' grep" ^$ "$ |wc-l ' let sumspace= $Args 1+ $Args 2 echo" The sum of all blank lines in these two files is: $sumspace "[[Em AIL protected] scripts]# sh sumspace.sh/etc/issue/etc/issue All blank lines in these two files are: 2[[email protected] scripts]# sh sumspace.sh Please input path:[[email protected] scripts]#

6, write a script/root/bin/sumfile.sh, statistics/etc,/var,/usr directory total number of sub-directories and files

[[Email protected] scripts]# sh sumfile.shplease input at least three dir:[[email protected] scripts]# sh sumfile.sh/etc /VAR/USR/ETC,/VAR,/USR a total of 292 sub-directories and files [[email protected] scripts]# cat sumfile.sh#!/bin/bash[$#-lt 3] && echo " Please input at least three dir: "&& exit 1 total1= ' ls-a $ |wc-l ' total2= ' ls-a $ |wc-l ' total3= ' ls-a $ |WC- L ' sumfile=$ (($total 1+ $total $total 3)) echo "$1,$2,$3 a total of sub-directories and Files $sumfile" [[email protected] scripts]#

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

[[Email protected] scripts]# sh argsum.shat least input one args:[[email protected] scripts]# sh argsum.sh/etc/issuethe A RGS you input have 1 lines space[[email protected] scripts]# cat Argsum.sh#!/bin/bash [$#-lt 1] && echo "at Lea St input One args: "&& exit 1 | | echo "The args you have input has ' grep" ^$ "$1|wc-l ' lines space" [[email protected] scripts]#


8, write a script/root/bin/hostping.sh, accept a host's IPv4 address as parameters, test whether it can be connected. 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"

[[Email protected] scripts]# sh hostping.shplease input one IP address:[[email protected] scripts]# sh hostping.sh 10.1.2 49.94you can access the website! [[Email protected] scripts]# sh hostping.sh 10.1.249.93the IP is not unreachable[[email protected] scripts]# cat hostping . sh#!/bin/bash[$#-lt 1] && echo "Please input one IP address:" && exit 1PING-C1-W1 $ &>/dev/n Ull && echo "You can access the website!" | | echo "The IP is not unreachable" [[email protected] scripts]#



This article is from "Wake up your not alarm clock but dream" blog, please be sure to keep this source http://purify.blog.51cto.com/10572011/1837417

Bash Little Little Script

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.