- 变量
首先shell是一个脚本语言,具有脚本语言一般都具有的特点之一:弱类型,shell 中 变量本身没有类型之分,不像c++,各种基本类型(int, long ,char ,double ……)。
1. 变量的使用方式
variable=value 是shell变量声明赋值的基本形式,变量事先不需要声明类型,因为本身没有类型这个概念
其中需要注意:”=”在作为赋值符号时,要求两边不能有空格,否则报错。因为”=”在其他地方还有比较的用法,当用作比较符号判断是否相等时,则必须要求”=”两边用至少一个空格隔开。
- 关于variable变量名的取值规范:我认为应当参考其他语言,以C语言为标准肯定不会犯错。其中包括不能以数字或其他非字母字符开头,不能占用关键字等。
- 关于value的形式说明:首先明确一点,value的取值是以字符串形式存在的。如果value本身包含空格字符(比如space tab)那么它必须加上双引号和单引号,否则单引号或者双引号不是必须的。
var=hello world # wrong
var="hello world" # right
var='hello world' # right too
下面这个代码段可以帮助我们很好的理解如何赋值以及弱类型的概念
var=3; # comments begins with # 这样代码完成变量的赋值
echo $var # this will print 3
let var++ # 自增1
echo $var # this will print 4
var="3" # assign 3 to var
echo $var # this will print 3
let var++
echo $var # this will print 4 too.
单引号和双引号的区别: 在shell中单引号包含的内容会原封不动的显示出来,而双引号里面的字符在某种情况下并不会原封不动显示。
2. 变量的值的获取
看到这里我想你已经知道了如何获取变量了,对没错,获取变量有两种方式:
$var 或者 ${var} var表示变量名
- 数组
shell 中数组分为两种:arrays 和 associactive arrays
arrays 类似C++数组,下标以0开头,而associative arrays 则类似于java中Map的概念,就是说associative arrays 的下标(index)可以是字符串,且可以自己声明。
下面通过代码看看如何定义这两种形式的数组
array_var=(1 2 3 4 5)
或者
array_var[0]=1
array_var[1]=2
array_var[2]=3
array_var[3]=4
array_var[4]=5
当然 数组还可以是其他字符串(其实已经说过了1,2,3都是字符串),如:
array_var[0]="test1"
array_var[1]="test2"
array_var[2]="test3"
array_var[3]="test4"
array_var[4]="test5"
一切都是字符串!
和数组有关的操作有两个,根据下标获取元素,计算数组长度,shell中用
${array_var[num]} #num 表示下标为num的那个元素,特别的当下标大于数组的长度时,并不会报错而是返回空
${array_var[*]} or ${array_var[@]} # 返回所有的元素以空格隔开
${#array_var[*]} #返回数组的长度
associative arrays 的声明方式有点特别
必须用 declare -A ass_array声明接着初始化,初始化方式有两种:
ass_array=([index1]=value1 [index2]=value2 [index3]=value3)
或者:
ass_array[idnex1]=value1
ass_array[idnex2]=value2
ass_array[idnex3]=value3
获取associative array单个元素值及数组长度的命令同arrays
,前面说到associative arrays 可以自己定义index,本质类似于java的Map,shell提供一种操作可以获取每一个成员的index的值(就是map中的key的值)以空格隔开,一起返回;
想想java map 的keyset方法
${!ass_array[*]} #获取数组中每一个元素的index 空格隔开一起返回
- 比较判断
判断语句:形式如下:
if condition;
then
commands;
fi
也可以else if 和 else 连用
if condition
then
commands
else if condition ; then
commands
else
commands
fi
数学上的判断语句可以放在 [] 里形式如下:
[ $var -eq 55 ]
-eq : 等于
-ne : 不等于
-lt : 小于
-gt : 不小于
-le : 不大于
也可以多个条件连用
[ $var1 -ne 0 -a $var2 -gt 2 ] -a : and
[ $var1 -ne 0 -o $var2 -gt 2 ] -o : or
!!!warning : there is a space between[ or ] and operands .
[$var -eq 0 ] and [ $var -eq 0] are all wrong.
only [ $var -eq 0 ] is right
How can we compare string?
- Two string can be compared to check whether they are the same in the following manner
[[ $str1 = $str2 ]] or [[ $str1 == $str2 ]] this return true when str1 equals str2,that is, the text contents of str1 and str2 are the same
[[ $str1 != $str2 ]] : this return true when str1 and str2 mismatch
[[ $str1 > $str2 ]] : this return true when str1 is alphabetically greater than str2
[[ $str1 < $str2 ]]: this return true when str1 is alphabetically smaller than str2
specially :
[[ -z $str1 ]] : this return true if str1 holds an empty string
[[ -n $str1 ]] : this return true if str1 holds a nonempty string
- 循环结构
like other programing language such as C++. shell has while loop and for loop well.
1. while loop
while condition
do
commands;
done
2 for loop
for var in list:
do
commands;
done;
list can be a string or sequence
such as print 1-5:
for i in {1..5}
do
echo $i;
done;
we also can use the format in C
for(( i=0;i<10;i++ ))
{
commands;
}
3 A special loop : until
let’s look at the following code segment:
x=0;
until [ $x -eq 9 ] # [ $x -eq 9 ] is the condition
do
let x++;
echo $x;
done;
until loop means :until the condition first becomes true then the loop end ,before this , the loop execute
- 函数调用
A function can be defined as follows:
function fname() { statements; } or fname() { statements; }
A function can be invoked just by its name:
fname; or fname arg1 arg2 ;# passing args
The recursive function
F() { echo $1 ; F hello ; sleep 1; }
there are some special variables in function :
$0 is the function name $1 is the first argument $2 is the second argument $n is the nth argument $@ is a list that contains all argument that every argument is a single string $* is a string that contains all argument.all argument together is a string. $? return value of a command exit status