一天一linux命令(17) shell scripts 脚本

本文介绍了Shell脚本的基础知识,包括执行方式、注释、变量定义、读取内容到变量、脚本参数传递、逻辑语句(if、case)、循环(for、while、until)及调试方法等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注释。 以#号开始到行尾

执行方式。(1)直接执行。先用chmod命令允许执行权限,就可以以执行程序的方式运行;(2)sh命令执行。如 $ sh script.file

例一:简单shell文件

#!/bin/shell   #+!+shell名称,用于宣告使用的shell(此行必须要)
#hello.sh  the shell script hello world  #文件名,脚本功能说明
#2013.4.13   #创建日期
#written by hfpang   #作者
hello=Hello\ \!\ How\ are\ you\ \?        #变量定义
echo hello      #命令执行
变量的使用参考 http://blog.youkuaiyun.com/fuxingdaima/article/details/8778317

交互     read命令。读取内容到变量中

脚本参数

$ myscript   opt1   opt2   opt3   opt4  
           $0      $1     $2      $3    $4 

逻辑语句

判断:

法1:if [condition] ; then <sentence>  [elif [condition]; then <sentence> ]  [else <sentence> ]  fi

   例:

#!/bin/bash
#judge.sh  test usage for judge
if [ "$1" = "hello" ]; then
     echo "hello,shell"
elif [ "$1" = "" ]; then
    echo "usage: $0 hello"
else
    echo "argument error"
fi

法2:case  ... esac  

语法:

 case  条件值(string) in    #case开始。string可以$1或交互式输入方式提供
    匹配值一)  
       程序执行段  
       ;;                    #两个分号。结束符号  
    匹配值二)  
       程序执行段  
       ;;  
    *)   #其它匹配值
       echo "Usage: $1 { 种类方式一|种类方式二}"   #列出可以利用的参数值 
       exit 1  
esac                      #case结束
说明:  类拟C++的switch语句,此方式可以在执行脚本时提供条件值。

例:

  1 #!/bin/bash
  2 #case.sh    test case ... esac
  3 case $1 in
  4  one)
  5     echo "your choice is one"
  6     ;;
  7  two)
  8     echo "your choice is two"
  9     ;;
 10  three)
 11     echo "your choice is three"
 12     ;;
 13  *)
 14     echo "usage: $0 <one|two|three>"
 15     exit
 16 esac
$ sh case.sh

usage: case.sh <one|two|three>

$ sh case.sh one

your choice is one

循环

语法:

· for (( 条件1; 条件2; 条件3))  
·  for variable in variable1 variable2 .....   
·  while [ condition1 ] && { | | } [ condition2 ] ...   
·  until [ condition1 ] && { | | } [ condition2 ] ...   

说明:
· until:直到条件符合的时候才退出程序;  
· while :当条件符合时,就继续做。  

  1 #!/bin/bash
  2 #loop.sh    demo for loop
  3 #for()
  4 declare -i i
  5 declare -i s
  6 s=0
  7 for (( i=1; i<11; i=i+1 ))
  8 do  
  9     s=s+i
 10 done 
 11 echo "the count for (1,...,10) is $s"
 12 #for in
 13 list="pang hua fu"
 14 for c in $list
 15 do  
 16     echo "$c"   
 17 done
 18 #while
 19 i=1
 20 s=0
 21 while [ "$i" != "11" ]
 22 do  
 23     s=s+i
 24     i=i+1
 25 done 
 26 echo "the count for (1,...,10) is $s"
 27 #until
 28 i=1
 29 s=0
 30 until [ "$i" = "11" ]
 31 do
 32     s=s+i
 33     i=i+1
 34 done
 35 echo "the count for (1, ..., 10) is $s"

test

语法:$ test [expression ]  [option ]

说明:判断条件是否成立

参数:

  <exp1> -a/-o <exp2>   条件1且/或条件2成立

  -n/-z <string>     字符串为非0/为0

  <data1> =/-eq/!=/-ne/>/gt/</-lt/-ge/-le <data2>   data1 等于/不等于/大于/小于/大于并等于/小于并等于data2

  <file1> -ef/-nt/-ot <file2>     file1 (最后修改时间)等于/新于/旧于 file2

  -f/-d/-c/-b/-L/-h/-S/-P <file>  文件存在并且类型是普通文/目录/字符/块/链接/硬链接/socket/命令管道

  -r/-w/-x/-s <file>  文件存在且可读/可写/可执行/非空

  -G/-O/-g/-u  文件为当前组ID拥有/当前用户ID拥有/ / 

declare  

语法: $ declare [-afirx]  

说明:声明变量。不是必须的,但变量默认为字符串,如需要其它类型则可用此声明。
参数:  
-a  :定义为数组 array 
-f  :定义为函数 function   
-i  :定义为整数 integer  
-r  :定义为只读  
-x  :定义为通过环境输出变量  
范例:  
[test @test test]# declare -i a=3  
[test @test test]# declare -i b=5  
[test @test test]# declare -i c=$a*$b  
[test @test test]# echo $c  
15     #变成数字了

调试脚本

语法:$ sh [-nvx] scripts  

参数:
-n  :不执行脚本,查询脚本内的语法,若有错误则列出  
-v  :在执行脚本之前,先将脚本的内容显示在屏幕上;  
-x  :将用到的脚本内容显示在屏幕上,与-v稍微不同  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值