shell编程总结


一、编写Shell 脚本

一般步骤:1.编写一个脚本   2.使脚本文件可执行   3.把脚本放置到 shell 能够找到的地方

例如:
1.编写hello_world脚本
[plain]  view plain  copy
  1. #!/bin/bash  
  2. # This is our first script.  
  3. echo 'Hello World!'  

2.赋予可执行权限
[plain]  view plain  copy
  1. chmod 700 hello_world  

3.执行脚本
[plain]  view plain  copy
  1. ./hello_world  


4.若想让系统中的每个用户都可以使用, 那么可将脚本放到 /usr/local/bin,系统管理员使用的脚本经常放到 /usr/local/sbin 目录下。


二、Shell中输出文本 使用echo方式
例如:
[plain]  view plain  copy
  1. echo "a string"  

  • 使用"here document"方式(可以随意的嵌入引号)
格式如下:
command << token
text
token
这里的 command 是一个可以接受标准输入的命令名,token 是一个用来指示嵌入文本结束的字符串
把操作符"<<"改为 "<<-",shell 会忽略开头的 tab 字符,可使用缩进提高可读性


例如:
[plain]  view plain  copy
  1. cat << _EOF_  
  2. a string  
  3. _EOF_  

[plain]  view plain  copy
  1. cat <<- _EOF_  
  2.     a string  
  3. _EOF_  


  • 例子:使用 一个 here document 将一系列的命令传递到 ftp 程序中以从一个远端 FTP 服务器中得到一个文件
[plain]  view plain  copy
  1. #!/bin/bash  
  2. # Script to retrieve a file via FTP  
  3. FTP_SERVER=***  
  4. FTP_PATH=***  
  5. REMOTE_FILE=***  
  6. ftp -n << _EOF_  
  7. open $FTP_SERVER  
  8. user anonymous me@linuxbox  
  9. cd $FTP_PATH  
  10. hash  
  11. get $REMOTE_FILE  
  12. bye  
  13. _EOF_  
  14. ls -l $REMOTE_FILE  





三、Shell脚本中的变量
  • 创建变量或给变量赋值

variable=value(注:等号左右不能有空格)

可以在同一行中对多个变量赋值

a=5 b="a string"

使用变量

$variable 或 ${variable}

例如

[plain]  view plain  copy
  1. b="a string"   
  2. c="a string and $b"   
  3. mv $filename ${filename}1  




四、Shell中的函数
  • 函数语法形式(其中name为函数名,commands为一系列包含在函数中的命令,函数中须至少包含一条命令,return为可选)
function name {
    commands
    return
}

name () {
    commands
    return
}


*例如
[plain]  view plain  copy
  1. #!/bin/bash  
  2. function_1 () {  
  3.    echo "Function_1 executed."  
  4.   return  
  5. }  
  6. cat << _EOF_  
  7. $(function_1)  
  8. _EOF_  


  • 局部变量
*例如
[plain]  view plain  copy
  1. funct_1 () {  
  2.     local foo  #funct_1中的局部变量  
  3.     foo=1  
  4.     echo "funct_1: foo = $foo"  
  5. }  





五、流程控制:if 分支结构
  • if 语句语法如下
if commands; then
     commands
[elif commands; then
     commands...]
[else
     commands]
fi


*例如:
[plain]  view plain  copy
  1. x=5  
  2. if [ $x = 5 ]; then  
  3.     echo "x equals 5."  
  4. else  
  5.     echo "x does not equal 5."  
  6. fi  



  • 退出状态
当命令执行完毕后会给系统发送一个值,叫做退出状态。Shell 提供了一个参数$?可用来检查退出状态
例如:(状态0为命令执行成功)
$ ls -d /usr/bin
/usr/bin
$ echo $?
0


  • 测试
*经常与 if 一块使用的命令是 test。test 命令执行各种各样的检查与比较,它有两种等价模式:
test expression
[ expression ]

*其中expression 为一个表达式,其执行结果是 true 或者是 false。当表达式为真时,这个 test 命令返回一个0退出状态,当表达式为假时,test 命令退出状态为1


  • 测试表达式
*文件表达式
file1 -ef file2:file1 和 file2 拥有相同的索引号(通过硬链接两个文件名指向相同的文件)
file1 -nt file2:file1新于 file2
file1 -ot file2:file1早于 file2
-b file:file 存在并且是一个块(设备)文件
-c file:file 存在并且是一个字符(设备)文件
-d file:file 存在并且是一个目录
-e file:file 存在
-f file:file 存在并且是一个普通文件
-g file:file 存在并且设置了组 ID
-G file:file 存在并且由有效组 ID 拥有
-k file:file 存在并且设置了它的“sticky bit”
-L file:file 存在并且是一个符号链接
-O file:file 存在并且由有效用户 ID 拥有
-p file:file 存在并且是一个命名管道
-r file:file 存在并且可读(有效用户有可读权限)
-s file:file 存在且其长度大于零
-S file:file 存在且是一个网络 socket
-t fd:fd 是一个定向到终端/从终端定向的文件描述符 。 这可以被用来决定是否重定向了标准输入/输出错误
-u file:file 存在并且设置了 setuid 位
-w file:file 存在并且可写(有效用户拥有可写权限)
-x file:file 存在并且可执行(有效用户有执行/搜索权限)


*文件表达式例子(其中在表达式中参数$FILE两边的引号并不是必需的,但这可防范空参数)
[plain]  view plain  copy
  1. test_file () {  
  2.     # test-file: Evaluate the status of a file  
  3.     FILE=~/.bashrc  
  4.     if [ -e "$FILE" ]; then  
  5.         if [ -f "$FILE" ]; then  
  6.             echo "$FILE is a regular file."  
  7.         fi  
  8.         if [ -d "$FILE" ]; then  
  9.             echo "$FILE is a directory."  
  10.         fi  
  11.         if [ -r "$FILE" ]; then  
  12.             echo "$FILE is readable."  
  13.         fi  
  14.         if [ -w "$FILE" ]; then  
  15.             echo "$FILE is writable."  
  16.         fi  
  17.         if [ -x "$FILE" ]; then  
  18.             echo "$FILE is executable/searchable."  
  19.         fi  
  20.     else  
  21.         echo "$FILE does not exist"  
  22.         return 1  
  23.     fi  
  24. }  



*字符串表达式
string:string 不为 null
-n string:字符串 string 的长度大于零
-z string:字符串 string 的长度为零
string1 = string2或string1 == string2:string1 和 string2 相同. 单或双等号都可以,不过双等号更受欢迎
string1 != string2:string1 和 string2 不相同
string1 > string2:sting1 排列在 string2 之后
string1 < string2:string1 排列在 string2 之前
注:> 和 <表达式操作符必须用引号引起来或者是用反斜杠转义,否则会被 shell 解释为重定向操作符,造成潜在地破坏结果



*字符串表达式例子:
[plain]  view plain  copy
  1. #!/bin/bash  
  2. # test-string: evaluate the value of a string  
  3. ANSWER=maybe  
  4. if [ -z "$ANSWER" ]; then  
  5.     echo "There is no answer." >&2  
  6.     exit 1  
  7. fi  
  8. if [ "$ANSWER" = "yes" ]; then  
  9.     echo "The answer is YES."  
  10. elif [ "$ANSWER" = "no" ]; then  
  11.     echo "The answer is NO."  
  12. elif [ "$ANSWER" = "maybe" ]; then  
  13.     echo "The answer is MAYBE."  
  14. else  
  15.     echo "The answer is UNKNOWN."  
  16. fi  



*整型表达式
integer1 -eq integer2:integer1 等于 integer2
integer1 -ne integer2:integer1 不等于 integer2
integer1 -le integer2:integer1 小于或等于 integer2
integer1 -lt integer2:integer1 小于 integer2
integer1 -ge integer2:integer1 大于或等于 integer2
integer1 -gt integer2:integer1 大于 integer2

*整型表达式例子:
[plain]  view plain  copy
  1. #!/bin/bash  
  2. # test-integer: evaluate the value of an integer.  
  3. INT=-5  
  4. if [ -z "$INT" ]; then  
  5.     echo "INT is empty." >&2  
  6.     exit 1  
  7. fi  
  8. if [ $INT -eq 0 ]; then  
  9.     echo "INT is zero."  
  10. else  
  11.     if [ $INT -lt 0 ]; then  
  12.         echo "INT is negative."  
  13.     else  
  14.         echo "INT is positive."  
  15.     fi  
  16.     if [ $((INT % 2)) -eq 0 ]; then  
  17.         echo "INT is even."  
  18.     else  
  19.         echo "INT is odd."  
  20.     fi  
  21. fi  



  • 更现代的测试版本
*目前的 bash 版本包含一个复合命令:[[ expression ]],与test相比增加了一个重要的新的字符串表达式:string1 =~ regex,若string1匹配扩展的正则表达式 regex则返回真


例子:

[plain]  view plain  copy
  1. #!/bin/bash  
  2. # test-integer2: evaluate the value of an integer.  
  3. INT=-5  
  4. if [[ "$INT" =~ ^-?[0-9]+$ ]]; then  
  5.     if [ $INT -eq 0 ]; then  
  6.         echo "INT is zero."  
  7.     else  
  8.         if [ $INT -lt 0 ]; then  
  9.             echo "INT is negative."  
  10.         else  
  11.             echo "INT is positive."  
  12.         fi  
  13.         if [ $((INT % 2)) -eq 0 ]; then  
  14.             echo "INT is even."  
  15.         else  
  16.             echo "INT is odd."  
  17.         fi  
  18.     fi  
  19. else  
  20.     echo "INT is not an integer." >&2  
  21.     exit 1  
  22. fi  



*为整数设计的复合命令:(( ))


例子:

[plain]  view plain  copy
  1. #!/bin/bash  
  2. # test-integer2a: evaluate the value of an integer.  
  3. INT=-5  
  4. if [[ "$INT" =~ ^-?[0-9]+$ ]]; then  
  5.     if ((INT == 0)); then  
  6.         echo "INT is zero."  
  7.     else  
  8.         if ((INT < 0)); then  
  9.             echo "INT is negative."  
  10.         else  
  11.             echo "INT is positive."  
  12.         fi  
  13.         if (( ((INT % 2)) == 0)); then  
  14.             echo "INT is even."  
  15.         else  
  16.             echo "INT is odd."  
  17.         fi  
  18.     fi  
  19. else  
  20.     echo "INT is not an integer." >&2  
  21.     exit 1  
  22. fi  



  • 通过使用逻辑操作符来结合表达式

操作符测 试[[ ]] 和 (( ))
AND-a&&
OR-o||
NOT !!


  • 控制操作符:分支的另一种方法

*bash 支持两种可以执行分支任务的控制操作符。这个 &&(AND)和||(OR)操作符作用如同复合命令[[ ]]中的逻辑操作符


*语法:command1 && command2 和 command1 || command2


*对于 && 操作符,只有当command1 执行成功后,才会执行 command2。对于 || 操作符,只有当command1 执行失败后, 才会执行command2


*例如:
$ mkdir temp && cd temp :在成功创建目录temp后跳转到temp
$ [ -d temp ] || mkdir temp :若目录 temp 不存在则创建这个目录
[ -d temp ] || exit 1 : 若目录temp不存在则返回退出状态1




六、读取键盘输入(交互)
  • read - 从标准输入读取单行数据

*语法形式:read [-options] [variable...]


*读取一个整数:
[plain]  view plain  copy
  1. echo -n "Please enter an integer -> "  
  2. read var  
  3. echo "var = '$var'"  


*给多个变量赋值:
[plain]  view plain  copy
  1. echo -n "Enter one or more values > "  
  2. read var1 var2  
  3. echo "var1 = '$var1'"  
  4. echo "var2 = '$var2'"  


*若没有提供变量名,shell 变量 REPLY 会包含数据行
[plain]  view plain  copy
  1. echo -n "Enter one or more values > "  
  2. read  
  3. echo "REPLY = '$REPLY'"  


*read选项
-a array:把输入赋值到数组 array 中,从索引号零开始
-d delimiter:用字符串 delimiter 中的第一个字符指示输入结束,而不是一个换行符
-e:使用 Readline 来处理输入。这使得与命令行相同的方式编辑输入
-n num:读取 num 个输入字符,而不是整行
-p prompt :为输入显示提示信息,使用字符串 prompt
-r:Raw mode. 不把反斜杠字符解释为转义字符
-s:Silent mode. 不会在屏幕上显示输入的字符。当输入密码和其它确认信息的时候,这会很有帮助
-t seconds:超时. 几秒钟后终止输入。read 会返回一个非零退出状态,若输入超时
-u fd:使用文件描述符 fd 中的输入,而不是标准输入

*read选项例子:
[plain]  view plain  copy
  1. #!/bin/bash  
  2. if read -t 10 -sp "Enter secret pass phrase > " secret_pass; then  
  3.     echo -e "\nSecret pass phrase = '$secret_pass'"  
  4. else  
  5.     echo -e "\nInput timed out" >&2  
  6.     exit 1  
  7. fi  
*此脚本提示用户输入一个密码,若在10秒内没有完成输入,则脚本会退出并返回一个错误。因包含了一个 -s 选项,所以输入的密码不会出现在屏幕上
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值