This article mainly introduced 8 practical shell script sharing, this article has given the judgment input number character, the character or other, asks the average, the self decrement output, adds the prefix in the file, the batch test file existence and so on the practical script, needs the friend may refer to under
A few examples of shell scripts, it feels good.
"Example: 001" to judge the input as a number, character or other
The code is as follows:
#!/bin/bash
Read-p "Enter a number or string here:" Input
Case $input in
[0-9]) echo-e "Good job, Your input is a numberic! 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" asking for an average
The code is as follows:
#!/bin/bash
# Calculate The average of a series of numbers.
Score= "0"
Average= "0"
sum= "0"
num= "0"
While true; Todo
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]
Fi
Done
echo "Exiting."
"Example: 003" self-minus output
The code is as follows:
[Scriptname:doit.sh]
while (($# > 0))
Todo
Echo $*
Shift
Done
/>./doit.sh a b c d E
A b c d E
b c D E
C d E
D E
E
"Example: 004" Add a prefix to a file
The code is as follows:
# List of names
# Cat NameList
Jame
Bob
Tom
Jerry
Sherry
Alice
John
# Script Program
# Cat Namelist.sh
#!/bin/bash
For name in $ (cat namelist)
Todo
echo "Name=" $name
Done
echo "The name is out of namelist file"
# Output Results
#./namelist.sh
Name= Jame
Name= Bob
Name=, Tom.
Name= Jerry.
Name= Sherry
Name= Alice
Name= John
"Example: 005" Does the bulk test file exist
The code is as follows:
[Root@host ~]# Cat testfile.sh
#!/bin/bash
For file in test*.sh
Todo
If [f $file];then
echo "$file existed."
Fi
Done
[Root@host ~]#./testfile.sh
Test.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 populating the hard disk with the specified size file
The code is as follows:
[Root@host ~]# Df-ih/tmp
FileSystem inodes iused ifree iuse% mounted on
/dev/mapper/vg00-lvol5
1000K 3.8K 997K 1%/tmp
[Root@host ~]# Cat cover_disk.sh
#!/bin/env Bash
Counter=0
max=3800
Remainder=0
While True
Todo
((counter=counter+1))
If [${#counter}-gt $max];then
Break
Fi
((remainder=counter%1000))
If [$remainder-eq 0];then
Echo-e "counter= $countertdate =" $ (date)
Fi
Mkdir-p/tmp/temp
Cat < Testfile > "/tmp/temp/myfile. $counter"
If [$?-ne 0];then
echo "Failed to write file to Disk."
Exit 1
Fi
Done
echo "done!"
[Root@host ~]#./cover_disk.sh
counter=1000 date= Wed Sep 09:20:39 HKT 2014
counter=2000 date= Wed Sep 09:20:48 HKT 2014
counter=3000 date= Wed Sep 09:20:56 HKT 2014
Cat:write error:no spaces left on device
Failed to write file to Disk.
DD If=/dev/zero of=testfile bs=1m count=1
"Example: 006" reads a configuration file by traversing the method
The code is as follows:
[Root@host ~]# Cat Hosts.allow
127.0.0.1
127.0.0.2
127.0.0.3
127.0.0.4
127.0.0.5
127.0.0.6
127.0.0.7
127.0.0.8
127.0.0.9
[Root@host ~]# Cat readlines.sh
#!/bin/env Bash
I=0
While Read Line;do
hosts_allow[$i]= $LINE
((i++))
Done < Hosts.allow
For ((i=1;i<=${#hosts_allow [@]};i++)); Todo
echo ${hosts_allow[$i]}
Done
echo "Done"
[Root@host ~]#./readlines.sh
127.0.0.2
127.0.0.3
127.0.0.4
127.0.0.5
127.0.0.6
127.0.0.7
127.0.0.8
127.0.0.9
Done
"Example: 007" Application of simple regular expressions
The code is as follows:
[Root@host ~]# Cat regex.sh
#!/bin/env SH
#Filename: regex.sh
Regex= "[A-za-z0-9]{6}"
if [[$ =~ $regex]]
Then
Num=$1
Echo $num
Else
echo "Invalid Entry"
Exit 1
Fi
[Root@host ~]#./regex.sh 123abc
123abc
#!/bin/env Bash
#Filename: validint.sh
Validint () {
Ret= ' echo $ | awk ' {start = match ($1,/^-?[ 0-9]+$/); if (start = = 0) print "1"; else print "0"} '
Return $ret
}
Validint $
If [$?-ne 0]; Then
echo "Wrong Entry"
Exit 1
Else
echo "ok! Input number is: "$
Fi
"Example: 008" Simple backup files by date
The code is as follows:
#!/bin/bash
now=$ (date + "%m-%d-%y") # Current date
File= "Backup. $NOW. tar.gz" # Back up files
echo "Backing up data to/tmp/backup. $NOW. tar.gz file, please wait ..." #打印信息
Tar xcvf/tmp/backup. $NOW. tar.gz/home//etc//var # Simultaneously back up multiple files to the specified tar compressed file
echo "Done ..."