初识shell脚本

shell字符串、shell数组、shell echo指令、shell test命令、shell if语句、shell case语句、shell for语句、shell while语句、shell break语句、shell 函数第一个Shell脚本

#!/bin/bash
# this is your first shell
echo "hello world"

#!” 是一个约定的标记,它告诉系统这个脚本需要什么解释器来执行,即使用哪一种Shell。

# 表示注释。

echo命令用于向窗口输出文本,相当于c语言中的printf函数。

chmod +x ./filename.sh  #使脚本具有执行权限
./test.sh  #执行脚本

bash filename.sh 执行脚本

shell字符串

#shell.str.sh
#!/bin/bash
str="my name is gjianw217"
str2="what's you name"
echo $str $str2
echo ${#str}
echo ${str:1:4}
echo `expr index "$str" is`

字符串可以用单引号,也可以用双引号,也可以不用引号。其中:

单引号字符串的限制:

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

双引号的优点:

  • 双引号里可以有变量
  • 双引号里可以出现转义字符

shell数组

#shell-array.sh
#!/bin/bash

array=(value1 value2 value3 value4 value4)
echo ${array[2]}
echo ${array[@]}
echo ${#array[@]}
echo ${#array[3]}
echo ${array[*]}

在Shell中,用括号来表示数组,数组元素用“空格”符号分割开。定义数组的一般形式为:
    数组名=(值1 值2 ... 值n)

读取数组元素值的一般格式是:
    ${数组名[下标]}

shell echo指令

#shell-echo.sh

#!/bin/bash
name="ok"
echo "$name it is a test"
echo date
echo '$name"'
echo "it is a test" > myfile
echo -e  "ok!c"
echo "it is a test"
echo -e "ok!
"
echo "it is a test"

echo $'
'

echo是Shell的一个内部指令,用于在屏幕上打印出指定的字符串,也可以使用echo实现更复杂的输出格式控制。如果变量与其它字符相连的话,需要使用大括号({ })。若需要原样输出字符串(不进行转义),则必须使用单引号。

shell test命令

#shell-test.sh

#!/bin/bash
num1=100
num2=100
if test $[num1] -eq $[num2]
then
    echo the two number equal
else
    echo the two number not equal
fi

if test num1=num2
then
    echo the two number equal
else
    echo the two number no equal
fi

cd /bin
if test -e ./bash
then
    echo 'the file already exits'
else
    echo 'the file does not exits'
fi

cd /bin
if test -e ./notFile -o ./bash
then
    echo 'the file exits'
else
    echo 'the file not exits'
fi

echo "hello world"

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

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

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

数值测试表

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

字符串测试表

y

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

 文件测试表

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

shell if语句

#shell-if.sh

#!/bin/bash
if test $[2+3] -eq $[1+5];then echo 'the two equal';else echo 'the two not equal';fi

num1=3
num2=4
if test $num1 -eq $num2
then 
    echo the number equal
else
    echo the number not equal
fi

 在sh/bash里,如果else分支没有语句执行,就不要写这个else。末尾的fi是if倒过来拼写。

shell case语句

#shell-case.sh

#!/bin/bash

echo 'Input a number between 1 to 4'
echo -e "Your number is :c"

read aNum

case $aNum in
    1) echo 'You select 1'
    ;;
    2) echo 'You select 2'
    ;;
    3) echo 'You select 3'
    ;;
    4) echo 'You select 4'
    ;;
    *) echo 'You do not select a number between 1 to 4'
    ;;
esac

 shell case语句为多选择语句。可以用case语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令。

 case工作方式如上所示,取值后面必须为单词in,每一模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至 ;;。

 取值将检测匹配的每一个模式,一旦模式匹配,则执行完匹配模式相应命令后不再继续其他模式。如果无一匹配模式,使用星号 * 捕获该值,再执行后面的命令。

shell for语句

#shell-for.sh

#!/bin/bash
for loop in 1 2 3 4 5 6
do
    echo "The value is :${loop}"
done

for str in "This is a string"
do 
    echo $str
done

当变量值在列表里,for循环即执行一次所有命令,使用变量名获取列表中的当前取值。命令可为任何有效的shell命令和语句。in列表可以包含替换、字符串和文件名。in列表是可选的,如果不用它,for循环使用命令行的位置参数。 


shell while语句

#shell-while.sh

#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 5 ]
do
    COUNTER=`expr $COUNTER + 1`
    echo $COUNTER
done

echo 'type <CTRL-D> to terminate'
echo -n 'enter your most liked file:'
while read FILM
do
    echo "yeah! great film teh $FILM"
done

 while循环用于不断执行一系列命令,也用于从输入文件中读取数据;命令通常为测试条件。命令执行完毕,控制返回循环顶部,从头开始直至测试条件为假。

shell break语句

#shell-break.sh

#!/bin/bash

while :
do
    echo -n "input a number between 0 to 5:"
    read aNum
    case $aNum in
    0) echo "Game over"
       break
    ;;
    1|2|3|4|5) echo "you select $aNum"
    ;;
    *) echo "you not select the 0-5 nuber"
       continue
    ;;
    esac
done

 在循环过程中,有时候需要在未达到循环结束条件时强制跳出循环,Shell使用两个命令来实现该功能:break和continue。其中:break命令允许跳出所有循环(终止执行后面的所有循环)。continue命令与break命令类似,只有一点差别,它不会跳出所有循环,仅仅跳出当前循环。

shell 函数

#shell-fun.sh

#!/bin/bash
demoFun(){
    echo "This is your first shell function"
}
echo "Function begin..."
demoFun
echo "Function end"

funWithReturn(){
    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))
}

funWithReturn
echo "the sum of two numbers is $?"

funWithParam(){
    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 76 98

 shell函数的命名可以是函数名或function 函数名,一般省略function的书写。在函数的定义中,简单的可分为三种,无返回值,有返回值和带参数的函数。函数返回值,可以显示增加return语句;如果不加,则将最后一条命令运行结果作为返回值(一般为0,如果执行失败则返回错误代码)。 return后跟数值(0-255)。在获取函数的参数时,一般使用$n,但当参数个数大于10时,需要使用${n}来获取参数

 

 

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

 

 参考资料:

Linux Shell脚本教程:30分钟玩转Shell脚本编程

SHELL脚本编程的常识

Linux Shell脚本编程基础

4 Advanced Bash-Scripting Guide

5 The Grymoire - Home for Unix Wizards

 

原文地址:https://www.cnblogs.com/gjianw217/p/3823453.html