当前位置: 移动技术网 > 网络运营>服务器>Linux > 8个实用的Shell脚本分享

8个实用的Shell脚本分享

2017年12月12日  | 移动技术网网络运营  | 我要评论

几个shell脚本的例子,觉得还不错。

【例子:001】判断输入为数字,字符或其他

复制代码 代码如下:

#!/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 

【例子:002】求平均数
复制代码 代码如下:

#!/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] 
  fi 
 
done 
 
echo "exiting." 

【例子:003】自减输出
复制代码 代码如下:

[scriptname: doit.sh] 
while (( $# > 0 )) 
do 
  echo $* 
  shift 
done  
         
/> ./doit.sh a b c d e 
a b c d e 
b c d e 
c d e 
d e 


【例子:004】在文件中添加前缀
复制代码 代码如下:

# 人名列表 
# cat namelist 
jame 
bob 
tom 
jerry 
sherry 
alice 
john 
 
# 脚本程序 
# cat namelist.sh 
#!/bin/bash 
for name in $(cat namelist) 
do 
        echo "name= " $name 
done 
echo "the name is out of namelist file" 
 
# 输出结果 
# ./namelist.sh 
name=  jame 
name=  bob 
name=  tom 
name=  jerry 
name=  sherry 
name=  alice 
name=  john 

【例子:005】批量测试文件是否存在

复制代码 代码如下:

[root@host ~]# cat testfile.sh       
#!/bin/bash 
 
 
for file in test*.sh 
do 
  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. 

【例子:005】用指定大小文件填充硬盘
复制代码 代码如下:

[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 
do 
    ((counter=counter+1)) 
    if [ ${#counter} -gt $max ];then 
        break 
    fi 
    ((remainder=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 to disk." 
        exit 1 
    fi 
done 
echo "done!" 
[root@host ~]# ./cover_disk.sh 
counter=1000    date= wed sep 10 09:20:39 hkt 2014 
counter=2000    date= wed sep 10 09:20:48 hkt 2014 
counter=3000    date= wed sep 10 09:20:56 hkt 2014 
cat: write error: no space left on device 
failed to write file to disk. 
dd if=/dev/zero of=testfile bs=1m count=1 

【例子:006】通过遍历的方法读取配置文件
复制代码 代码如下:

[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++)); do 
    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 

【例子:007】简单正则表达式应用
复制代码 代码如下:

[root@host ~]# cat regex.sh 
#!/bin/env sh 
#filename: regex.sh 
regex="[a-za-z0-9]{6}" 
if [[ $1 =~ $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 $1 | awk '{start = match($1,/^-?[0-9]+$/);if (start == 0) print "1";else print "0"}'` 
    return $ret 

 
validint $1 
 
if [ $? -ne 0 ]; then 
    echo "wrong entry" 
    exit 1 
else 
    echo "ok! input number is:" $1 
fi 

【例子:008】简单的按日期备份文件
复制代码 代码如下:

#!/bin/bash 
 
now=$(date +"%m-%d-%y")      # 当前日期 
file="backup.$now.tar.gz"    # 备份文件 
echo "backing up data to /tmp/backup.$now.tar.gz file, please wait..."  #打印信息 
tar xcvf /tmp/backup.$now.tar.gz /home/ /etc/ /var       # 同时备份多个文件到指定的tar压缩文件中 
echo "done..."          

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网