Shell基础

来源:互联网 发布:vmware mac os 10.9 编辑:程序博客网 时间:2024/05/19 11:34
来自http://www.runoob.com/linux/linux-shell.html的学习笔记

Shell脚本

Shell种类

  • Bourne Shell(/usr/bin/sh 或/bin/sh)
  • Bourne Again Shell(/usr/bash)
  • C Shell(/usr/bin/csh)
  • K Shell(/usr/bin/ksh)
  • Shell for Root(/sbin/sh)等等

#!/bin/bash

    #!告诉系统其后路径所指定的程序即是解释此脚本文件的Shell程序。

运行Shell脚本的两种方法

  • 作为可执行程序
    test.sh,在文件所在目录下
    chmod +x ./test.sh #使脚本具有执行权限
    #执行脚本,直接写test.sh,linux系统会去PATH里寻找有没有叫test.sh的,而./test.sh会在当前目录里找
    ./test.sh

  • 作为解释器参数
    直接运行解释器,参数为shell脚本的文件名
    比如:
    /bin/sh test.sh

Shell变量

your_name="2huo"
:变量名和等号之间不能有空格

变量名命名规则

  • 首字母为字母(a-z,A-Z)
  • 中间不能有空格,可以使用下划线
  • 不能使用标点符号
  • 不能使用bash里的关键字

例子:循环中的例子
for file in \`ls /etc` #将/etc目录下的文件名循环取出

使用变量

your_name="2huo"echo $your_nameecho ${your_name}
for skill in Ada Coffe Action Java do    echo "I am good at ${skill}Script"done

    {}的好处在于不会将$skill误解释为$skillScript,所以最好是加上花括号,这是一个好的习惯。


字符串

单引号,双引号都行,但是有区别

单引号

  • 单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的;
  • 单引号字串中不能出现单引号(对单引号使用转义符后也不行)。

双引号

your_name='qinjx'str="Hello, I know your are \"$your_name\"! \n"
  • 双引号里可以有变量
  • 双引号里可以出现转义字符

字符串长度

your_name="2huo"echo ${#your_name}    #输出 4

提取子字符串

your_name ="alibaba is a great company"echo ${ your_name:1:4} #输出liba

查找子字符串

str="alibaba is a great company"echo `expr index "$str" is` 

:以上脚本中 “`” 是反引号,而不是单引号 “’”,不要看错了。


Shell数组

Bash支持一维数组,不支持多维数组,下标从0开始

定义数组(空格隔开,或者用换行也行)

数组名={值1 值2 … 值n}

读数组

${数组名[下标]}

使用@符号可以获取数组中的所有元素,例如:

echo ${array_name[@]}

# 取得数组元素的个数

length=${#array_name[@]}

# 或者

length=\${#array_name[*]}

# 取得数组单个元素的长度

lengthn=${#array_name[n]}

Shell注释

#是注释,只能每行加一个#号,没有多行注释

显示换行

echo -e "OK!\nOK" #  -e 开启转义

显示不换行

echo -e "OK! \c" # -e 开启转义 \c 不换行

显示结果定向至文件

echo "It is a test" > myfile

原样输出字符串,不进行转义或取变量(用单引号)

echo '$name\"'

Shell test命令

    Shell中的test命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。

数值测试

参数 说明 -eq 等于则为真 -ne 不等于则为真 -gt 大于则为真 -ge 大于等于则为真 -lt 小于则为真 -le 小于等于则为真

例子:

num1=100num2=100if test $[num1] -eq $[num2]then    echo 'The two numbers are equal!'else    echo 'The two numbers are not equal!'fi

输出结果:
The two numbers are equal!

字符串测试

参数 说明 = 等于则为真 != 不相等则为真 -z字符串 字符串长度伪则为真 -n字符串 字符串长度不伪则为真

例子:

num1=100num2=100if test num1=num2then    echo 'The two strings are equal!'else    echo 'The two strings are not equal!'fi

输出结果:
The two strings are equal!

文件测试

参数 说明 -e 文件名 如果文件存在则为真 -r 文件名 如果文件存在且可读则为真 -w 文件名 如果文件存在且可写则为真 -x 文件名 如果文件存在且可执行则为真 -s 文件名 如果文件存在且至少有一个字符则为真 -d 文件名 如果文件存在且为目录则为真 -f 文件名 如果文件存在且为普通文件则为真 -c 文件名 如果文件存在且为字符型特殊文件则为真 -b 文件名 如果文件存在且为块特殊文件则为真

例子:

cd /bin

if test -e ./bashthen    echo 'The file already exists!'else    echo 'The file does not exists!'fi

输出结果:
The file already exists!

    另外,Shell还提供了与( -a )、或( -o )、非( ! )三个逻辑操作符用于将测试条件连接起来,其优先级为:”!”最高,”-a”次之,”-o”最低。例如:

cd /bin

if test -e ./notFile -o ./bashthen    echo 'One file exists at least!'else    echo 'Both dose not exists!'fi

输出结果:
One file exists at least!


Shell流程控制

if

if conditionthen    command1     command2    ...    commandN fi

if else

if conditionthen    command1     command2    ...    commandNelse    commandfi

if else-if else

if condition1then    command1elif condition2    command2else    commandNfi

for循环

for var in item1 item2 ... itemNdo    command1    command2    ...    commandNdone

while语句

while conditiondo    commanddone

例子:

#!/bin/shint=1while(( $int<=5 ))do        echo $int        let "int++"done

until循环
    until循环执行一系列命令直至条件为真时停止。
    until循环与while循环在处理方式上刚好相反。
    一般while循环优于until循环,但在某些时候—也只是极少数情况下,until循环更加有用。

until conditiondo    commanddone

case

case 值 in模式1)    command1    command2    ...    commandN    ;;模式2)    command1    command2    ...    commandN    ;;esac

例子:

echo '输入 1 到 4 之间的数字:'echo '你输入的数字为:'read aNumcase $aNum in    1)  echo '你选择了 1'    ;;    2)  echo '你选择了 2'    ;;    3)  echo '你选择了 3'    ;;    4)  echo '你选择了 4'    ;;    *)  echo '你没有输入 1 到 4 之间的数字'    ;;esac

跳出循环

break命令

#!/bin/bashwhile :do    echo -n "输入 1 到 5 之间的数字:"    read aNum    case $aNum in        1|2|3|4|5) echo "你输入的数字为 $aNum!"        ;;        *) echo "你输入的数字不是 1 到 5 之间的! 游戏结束"            break        ;;    esacdone

continue
    continue命令与break命令类似,只有一点差别,它不会跳出所有循环,仅仅跳出当前循环。

#!/bin/bashwhile :do    echo -n "输入 1 到 5 之间的数字: "    read aNum    case $aNum in        1|2|3|4|5) echo "你输入的数字为 $aNum!"        ;;        *) echo "你输入的数字不是 1 到 5 之间的!"            continue            echo "游戏结束"        ;;    esacdone

Shell函数

function funname(){action;return int;}
#!/bin/bashfunWithReturn(){    echo "The function is to get the sum of two numbers..."    echo -n "Input first number: "    read aNum    echo -n "Input another number: "    read anotherNum    echo "The two numbers are $aNum and $anotherNum !"    return $(($aNum+$anotherNum))}funWithReturnecho "The sum of two numbers is $? !"

输出结果:

The function is to get the sum of two numbers...Input first number: 25Input another number: 50The two numbers are 25 and 50 !The sum of two numbers is 75 !

    函数返回值在调用该函数后通过 $? 来获得。

:所有函数在使用前必须定义。这意味着必须将函数放在脚本开始部分,直至shell解释器首次发现它时,才可以使用。调用函数仅使用其函数名即可。

函数参数

    在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值,例如,\$1表示第一个参数,\$2表示第二个参数…

带参数的函数示例:

#!/bin/bashfunWithParam(){    echo "The value of the first parameter is $1 !"    echo "The value of the second parameter is $2 !"    echo "The value of the tenth parameter is $10 !"    echo "The value of the tenth parameter is ${10} !"    echo "The value of the eleventh parameter is ${11} !"    echo "The amount of the parameters is $# !"    echo "The string of the parameters is $* !"}funWithParam 1 2 3 4 5 6 7 8 9 34 73

输出结果:

The value of the first parameter is 1 !The value of the second parameter is 2 !The value of the tenth parameter is 10 !The value of the tenth parameter is 34 !The value of the eleventh parameter is 73 !The amount of the parameters is 11 !The string of the parameters is 1 2 3 4 5 6 7 8 9 34 73 !

    注意,$10 不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数。

另外,还有几个特殊字符用来处理参数:

参数处理 说明 $# 传递到脚本的参数个数 $* 以一个单字符串显示所有向脚本传递的参数 $$ 脚本运行的当前进程ID号 $! 后台运行的最后一个进程的ID号 $@ 与$*相同,但是使用时加引号,并在引号中返回每个参数。 $- 显示Shell使用的当前选项,与set命令功能相同。 $? 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。
0 0
原创粉丝点击