国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Linux > 正文

8個(gè)實(shí)用的Shell腳本分享

2019-10-26 18:46:08
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

幾個(gè)Shell腳本的例子,覺(jué)得還不錯(cuò)。

【例子:001】判斷輸入為數(shù)字,字符或其他
代碼如下:
#!/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】求平均數(shù)
代碼如下:
#!/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" 
 
# 輸出結(jié)果 
# ./namelist.sh 
name=  Jame 
name=  Bob 
name=  Tom 
name=  Jerry 
name=  Sherry 
name=  Alice 
name=  John 

【例子:005】批量測(cè)試文件是否存在
代碼如下:
[root@host ~]# cat testfile.sh       
#!/bin/bash 
 
 
for file in test*.sh 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 三穗县| 庆云县| 乐清市| 泰宁县| 邵阳市| 宽城| 应城市| 铁岭市| 垫江县| 江源县| 文成县| 观塘区| 顺平县| 岑巩县| 砀山县| 运城市| 伽师县| 太白县| 安远县| 文安县| 志丹县| 无为县| 江津市| 乌拉特中旗| 美姑县| 延寿县| 阿拉尔市| 襄城县| 奉节县| 沂源县| 弋阳县| 静海县| 德清县| 祁连县| 镇雄县| 定结县| 蚌埠市| 灵武市| 丁青县| 石楼县| 托克逊县|