Linux Shell 基础记录

来源:互联网 发布:java基础实例教程pdf 编辑:程序博客网 时间:2024/04/30 04:25

Shell脚本首行一般都是:

#! /bin/bash

#! 指定shell,这里指定是/bin/bash,也可以是其他。#! 与/bin/bash可用可不用空格,为了美观建议使用。


注释一般都是用#开头

如果一行命令太长,可以使用反斜杠“\”在下一行继续这条命令

单引号‘ 可以组织变量替换,一般变量定义都建议使用双引号“”


变量:VARIABLE=value  #等号两边可以没有空格

函数:

[function]    function-name()

{

commands

}

关键字function是可选的,function-name为调用该函数使用的函数名

控制结构:

if  test-command 

then

commands

fi

或者

if  test-command;   then  #多了个分号,因为在同一行

commands

fi


例子:

 #! /bin/bash                                                                                                             
 if test $# -eq 0 #特殊参数#$表示命令行参数的个数
    then
         echo "You must supply at least one argument."
         exit 1  #退出状态1,可以使用echo $?查看退出状态
 fi
 echo "End."


1 #! /bin/bash
  2 if test $# -eq 0
  3     then
  4         echo "You must supply at least one argument."
  5         exit 1
  6 fi
  7 if test -f "$1"
  8     then 
  9         echo "$1 is a ordinary file in the working directory"
 10     else
 11         echo "$1 is NOT a ordinary file int working directory"
 12 fi                                                                                                                       
 13 echo "End."

内置命令test的选项

-d    检查文件是否存在以及该文件是否是目录文件

-e   检查文件是否存在

-f 检查文件是否存在以及该文件是否是普通文件(不是目录)

-r    检查文件是否存在以及该文件是否可读

-s 检查文件是否存在以及文件是否大于0字节

-w  检查文件是否存在以及是否可写

-x    检查文件是否存在以及是否可执行

test命令也可以一对中括号[ ]代替:

if [ $# -eq 0 ]  #中括号前后都要用空格隔开

  3     then
  4         echo "You must supply at least one argument."
  5         exit 1
  6 fi


if...then...elif结构:

#! /bin/bash

echo -n "word 1:"
read word1
echo -n "word 2:"
read word2
echo -n "word 3:"
read word3

if [ "$word1" = "$word2" -a "$word2" = "$word3" ]  #"-a"作为布尔运算AND,=等号两边要留空格,不然比较无效,一直为真
    then 
        echo "Match: words 1, 2, & 3"
elif [ "$word1" = "$word2" ]
    then
        echo "Match: words 1 & 2"
elif [ "$word1" = "$word3" ]
    then
        echo "Match: words 1 & 3"
elif [ "$word3" = "$word2" ]
    then
        echo "Match: words 2 & 3"
else
        echo "No match"
fi


调试shell脚本:bash -x shell.sh

或者在shell.sh中加入set -x,关闭set +x


控制结构:

for  loop-index in argument-list

do 

commands

done

解析:此结构把argument-list中的第一个参数值赋予loop-index变量,并执行do语句和done语句之间的命令。

  1 #! /bin/bash
  2 for fruit in $@    #打印所有的输入参数的名称,#@表示所有的参数
  3 do
  4     echo "$fruit"
  5 done
  6 echo "Task Complete"
  7 


while test-command 

do 

commands

done

解析:只要test-command的退出状态为真,while结构就继续执行do与done之间的命令。

  1 #! /bin/bash
  2 
  3 number=0;
  4 while [ "$number" -lt 10 ] #对于数值比较 -ne(不等于)、-eq(等于)、-gt(大于)、-ge(大于等于)、-lt(小于)、-le(小于等于),对于字符串:=(等于) !=(不等) 
  5 do 
  6     echo -n "$number"
  7     ((number +=1))           #算术赋值必须加双括号                                                                                        
  8     sleep 1
  9 done
 10 
 11 echo "Task Complete"


until test-commands

do 

commands

done 

解析:until会一直循环,知道test-commands返回的退出状态为真

  1 #! /bin/bash
  2 
  3 secretname=zach
  4 name=noname
  5 echo "Try to guss the secret name!"
  6 until [ "$name" = "$secretname" ]
  7 do 
  8     echo -n "Your guess:"
  9     read name
 10 done
 11 
 12 echo "Task Complete"                                                                                                     
 13 


break和continue

#! /bin/bash
for index in 1 2 3 4 5 6 7 8 9 10
do 
    if [ $index -le 3 ]; then 
        echo "continue"
        continue
    fi
    echo $index


    if [ $index -ge 8 ]; then 
        echo "break"
        break
    fi
    
done
echo "Task Complete"


case结构:

case test-string in

pattern-1)

commands-1

;;

pattern-2)
commands-2

;;
pattern-3)

commands-3

;;

*) #万能匹配

commands-4

esac


#! /bin/bash

echo "Enter A, B, or C:"
read letter
case "$letter" in 
    a|A)
        echo "You entered A"
        ;;
    b|B)
        echo "You entered B"
        ;;
    c|C)
        echo "You entered C"
        ;;
    *)
        echo "You did not enter A, B, or C"
        ;;
esac

echo "Task Complete"


select控制结构:

select varname [ in arg .....]

do 

commands

done

例子:

#! /bin/bash
PS3="Choose you favorite fruit from these possibilities: "
select fruit in  apples orange pears bananas blueberry kiwi watermelon STOP
do 
    if [ "$fruit" == "" ]; then
        echo -e "Invalid entry.\n"
    elif [ $fruit = STOP ]; then
        echo "Thanks for playing!"
        break;
    fi
echo "You choose $fruit as you favorite."
echo -e "That is choice number $REPLY.\n"
done

运行:

dawu@ateteam-S3210SH:~/ucm6130/network/shell$ ./select   
1) apples      3) pears       5) blueberry   7) watermelon
2) orange      4) bananas     6) kiwi        8) STOP
Choose you favorite fruit from these possibilities: 5
You choose blueberry as you favorite.
That is choice number 5.


Choose you favorite fruit from these possibilities: 8
Thanks for playing!
dawu@ateteam-S3210SH:~/ucm6130/network/shell$ 



0 0
原创粉丝点击