A few examples of shell scripts, feel good.
" Example: 001 " judging the input as a number, character or other
#!/bin/bashread-p ' Enter a number or string here: ' Inputcase $input in [0-9] ' echo-e ' good job, Your input is a numb eric! \ n "; [A-za-z]) Echo-e "Good job, Your input is a character! \ n "; *) echo-e "Your input is wrong, input again! \ n " ; Esac
" Example: 002 " Find Average
#!/bin/bash# Calculate The average of a series of numbers. Score= "0" average= "0" sum= "0" num= "0" while true; Do echo-n "Enter your score [0-100%] (' Q ' for quit):"; Read score; if (("$SCORE" < "0")) | | ("$SCORE" > "100"); Then echo ' be serious. Common, try again: " elif [" $SCORE "= =" Q "]; Then echo "Average rating: $AVERAGE%." Break Else sum=$[$SUM + $SCORE] num=$[$NUM + 1] average=$[$SUM/$NUM] fidoneecho "Exiting."
"
Example: 003 "self-reducing output
[Scriptname:doit.sh]while (($# > 0)) do echo $* shiftdone />./doit.sh a b c d ea b c D EB c D EC D Ed Ee
"
Example: 004 "Add a prefix to the file
# Human List # cat namelistjamebobtomjerrysherryalicejohn# Script # Cat namelist.sh#!/bin/bashfor name in $ (cat namelist) do echo "Name=" $namedoneecho "The name is out of namelist file" # output result #./namelist.shname= jamename= bobname= to Mname= jerryname= sherryname= alicename= John
"
Example: 005 "Does the batch test file exist
[email protected] ~]# cat testfile.sh #!/bin/bashfor file in Test*.shdo if [-F $file];then echo "$file Exis Ted. " Fidone[[email protected] ~]#./testfile.shtest.sh existed.test1.sh existed.test2.sh existed.test3.sh existed.test4.sh existed.test5.sh existed.test78.sh existed.test_dev_null.sh existed.testfile.sh existed.
"
Example: 005 "fills the hard disk with the specified size file
[[email protected] ~]# df-ih/tmpfilesystem inodes iused IFree iuse% mounted ON/DEV/MAPPER/VG00-LVOL5 1000K 3.8K 997K 1%/tmp[[email protected] ~]# cat cover_disk.sh#!/bin/env Bashcounter =0max=3800remainder=0while Truedo ((counter=counter+1)) if [${#counter}-gt $max];then break fi (REM ainder=counter%1000)) If [$remainder-eq 0];then echo-e "counter= $counter \tdate=" $ (date) Fi mkdir-p/ Tmp/temp Cat < Testfile > "/tmp/temp/myfile. $counter" If [$?-ne 0];then echo "Failed to write file T o Disk. " Exit 1 Fidoneecho "done!" [[email protected] ~]#./cover_disk.shcounter=1000 date= Wed Sep 09:20:39 HKT 2014counter=2000 date= Wed Sep Ten 09:20:48 HKT 2014counter=3000 date= Wed Sep 09:20:56 HKT 2014cat:write Error:no Space left on devicefailed to Write file to disk.dd If=/dev/zero of=testfile bs=1m count=1
"
Example: 006 "read the configuration file by traversing the method
[email protected] ~]# cat Hosts.allow127.0.0.1127.0.0.2127.0.0.3127.0.0.4127.0.0.5127.0.0.6127.0.0.7127.0.0.8127.0.0.9[[email protected] ~ ]# cat readlines.sh#!/bin/env bashi=0while read Line;do hosts_allow[$i]= $LINE ((i++)) Done < Hosts.allowfor (i=1;i<=${#hosts_allow [@]};i++)]; Do echo ${hosts_allow[$i]}doneecho ' Done ' [[email protected] ~]#. Readlines.sh127.0.0.2127.0.0.3127.0.0.4127.0.0.5127.0.0.6127.0.0.7127.0.0.8127.0.0.9done
"
example: 007 "Simple Regular expression application
[[email protected] ~]# cat regex.sh#!/bin/env sh#filename:regex.shregex= "[A-za-z0-9]{6}" if [[=~ $regex]]then nu M=$1 echo $numelse echo "Invalid entry" exit 1fi[[email protected] ~]#./regex.sh 123abc123abc#!/bin/env Bash#filename:validint.shvalidint () { ret= ' echo $ | awk ' {start = match ($1,/^-?[ 0-9]+$/); if (start = = 0) print "1", Else print "0"} ' return $ret}validint $1if [$?-ne 0]; Then echo "wrong Entry" exit 1else echo "ok! Input number is: "$1fi
A few good shell scripts