/* More than one answer, only listed as the most elegant existence * *
Job Requirements: Write a bash script to implement the content required by the topic.
First, display the current host system information, including hostname, IPV4 address, operating system version, kernel version, CPU model, memory size, hard disk size
1#!/usr/bin/Bash2 printf3 "Hostname: $ (Hostname) \ n \4IPV4: $ (IP addr |grep-op'(\d+\.) {3}\d+/\d+'|awk 'begin{ors= ""}{print}') \ n5Os_version: $ (Cat/etc/gentoo-release) \ n6Kernel_version: $ (uname-r) \ n7CPU: $ (uname-p) \ n8Totalmem (KiB): $ (Cat/proc/meminfo |grep-op'(? <=memtotal:) \s*\d+'|sed 's/\+//g') \ n9Totaldisksize (KiB): $ (cat/sys/block/sd*/size | gawk ' begin{printf "("; ors= "+"}{print}end{printf "0)/2\n"} "| BC) \ n "
[Email protected] ~/bash_script $ bash systeminfo.SHhostname:z IPv4:127.0.0.1/8 172.18.16.4/ -Os_version:funtoo Linux-Baselayout2.2.1kernel_version:4.4. -Cpu:intel (R) Core (TM) i7-4790k CPU @4. 00GHz Totalmem (KiB):16437364totaldisksize (KiB):1211193648
Second, calculate the sum of the ID of the 10th user and the 20th user in the/etc/passwd file
#!/bin/bashsed'10p;20p' /etc/passwdgawk' begin{ors= "+";p rintf "("}{print $3}end{printf "0)" \ n "}' | BC
[Email protected] ~/bash_script $ bash sumid. SH 1010
Three, pass two file path as parameters to the script, calculate the sum of all the blank lines in these two files
[email protected] ~/bash_script $ cat sum_blanklines.sh # !/bin/bashread -P " " File1read -p Please enter a file path again: " file2printf " Total number of blank lines in these two files: $ (grep-p ' ^\s*$ ' $file 1 $file 2 | wc-l) \ n
[Email protected] ~/bash_script $ bash sum_blanklines. SH passwd6
Iv. statistics/etc, Var,/usr Total number of sub-directories and files in the directory
#!/bin/bashfind1WC -L
Accept a file path as a parameter; If the number of parameters is less than 1, the prompt "cannot be empty" and prompts for input again, if the number of arguments is not less than 1, displays the number of blank lines in the file pointed to by the first argument
[Email protected] ~/bash_script $CatArgs_num.SH #!/bin/Bashpath () {printf"Please enter a file path:"}pathread Pathuntil[[$Path! ="" ]] Doprintf"can't be empty! \ n"Path Read Path Done grep-P'^\s*$'$Path |WC-L
Six, accept a host's IPV4 address as a parameter, test whether it can be connected, if you can ping, then prompt the user "The IP can be accessed"; If not, prompt the user "This IP is not accessible"
#!/bin/Bashread-P"Please enter a IPV4 address:"IPv4Ping-W 1-C1$IPv 4 >/dev/NULLif[[ $? =="0" ]] Thenprintf"the IP can access \ n"Elseprintf"the IP is not accessible \ n"fi
Seven, determine whether each partition space of the hard disk and inode utilization is greater than 80%, if so, send email notification root disk will be full
#!/bin/bashinodeused=$(DF-I. |gawk '{if ($ ~/[8-9][0-9]%/) Print $ ""') spaceused=$(DF|gawk '{if ($ ~/[8-9][0-9]%/) Print $ ""')if[[$InodeUsed! =""|| $SpaceUsed! ="" ]] Thenprintf"The following partition space utilization has exceeded 80%:\n${spaceused}\n the following partition Inode utilization has exceeded 80%:\n${inodeused}\n"| Mail-s disk_space_warning Rootfi
Viii. calculating the number of integers between 1 and 100
#!/bin/bashecho {1.. - TR ' ' ' + ' | Bc
Nine, determine whether the IP input is a legitimate IP (rough judgment, the focus is on the application of the script)
#!/bin/bashif [[$1 =~ ([0-9]+\.) {3} [0-9]+ ]] then"IP legal \ n" Else "IP not valid \ n" fi
Ten, enter the starting value A and the last value B, calculate the sum from A + (a+1) ... + (B-1) +b
[Email protected] ~/bash_script $CatSum100diy.SH #!/bin/Bashread-P"Please enter a two positive integer:"Goal1 Goal2sum=0if[[Goal1-GT Goal2]] ThenG1=Goal1 G2=Goal2ElseG1=Goal2 G2=Goal1fiuntil[[$g 1-le $g 2]] Do sum=$(($sum+$g 1)) G1=$ ($g 1-1)) Donesum=$(($sum+$g 2)) printf"$sum \ n"
Linux Basic Notes--[007]--8 month 10th after class homework