reference to here
变量
1.特殊变量
$0 - # bash脚本本身名字。
$1 - $9 - # bash脚本后面带的参数。
$# - # 参数个数。
$@ - # 所有参数
$? - # 最近一次命令退出的状态码
$$ - # 当前脚本运行的process id
$USER - # 运行当前脚本的用户名
$HOSTNAME - # 主机名
$SECONDS - # 脚本的执行时间
$RANDOM - # 根据当前时间随机一个数字
$LINENO - #脚本的当前行数
2.自定义变量
variable=value
=
左右不能有空格
使用$
引用变量值。
#!/bin/bash
# A simple variable example
myvariable=Hello
anothervar=Fred
echo $myvariable $anothervar
echo
sampledir=/etc
ls $sampledir
输出:
Hello Fred
a2ps.cfg aliases alsa.d …
定义变量时候最好还是加上引号,一号可以是单引号,也可是双引号。有一些区别:双引号可以做变量替换,单引号原样输出。
a=hello
b=world
c="$a $b" # hello world
d='$a $b' # $a $b
还可以从命令的返回值中定义变量
myvar=$( ls /etc | wc -l )
输入
使用read
命令等待输入:
read var1
#!/bin/bash
# Ask the user for their name
echo Hello, who am I talking to?
read varname
echo It\'s nice to meet you $varname
与read命令配合的常用参数是-p
和-s
-p显示交互信息,-s用来隐藏输入
#!/bin/bash
# Ask the user for login details
read -p 'Username: ' uservar
read -sp 'Password: ' passvar
echo
echo Thankyou $uservar we now have your login details
read还可以读取多个变量
#!/bin/bash
# Demonstrate how read actually works
echo What cars do you like?
read car1 car2 car3
echo Your first car was: $car1
echo Your second car was: $car2
echo Your third car was: $car3
除了read还可以从stdin中获取输入
#!/bin/bash
# A basic summary of my sales report
echo Here is a summary of the sales data:
echo ====================================
echo
cat /dev/stdin | cut -d' ' -f 2,3 | sort
将上面脚本保存为summery.sh
假如salesdata.txt中有如下信息
Fred apples 20 July 4
Susy oranges 5 July 7
Mark watermelons 12 July 10
Terry peaches 7 July 15
那么
cat salesdata.txt | ./summary
输出:
Here is a summary of the sales data:
====================================
apples 20
oranges 5
peaches 7
watermelons 12
算数运算
1.let
bash中的let
函数可以让我们做一些简单的数学表达运算。
let <arithmetic expression>
#!/bin/bash
# Basic arithmetic using let
let a=5+4
echo $a # 9
let "a = 5 + 4"
echo $a # 9
let a++
echo $a # 10
let "a = 4 * 5"
echo $a # 20
let "a = $1 + 30"
echo $a # 30 + first command line argument
2. expr
#!/bin/bash
# Basic arithmetic using expr
expr 5 + 4
expr "5 + 4"
expr 5+4
expr 5 \* $1
expr 11 % 2
a=$( expr 10 - 3 )
echo $a # 7
3.双括号表达式
$(( expression ))
#!/bin/bash
# Basic arithmetic using double parentheses
a=$(( 4 + 5 ))
echo $a # 9
a=$((3+5))
echo $a # 8
b=$(( a + 3 ))
echo $b # 11
b=$(( $a + 4 ))
echo $b # 12
(( b++ ))
echo $b # 13
(( b += 3 ))
echo $b # 16
a=$(( 4 * 5 ))
echo $a # 20
4.变量长度
#!/bin/bash
# Show the length of a variable.
a='Hello World'
echo ${#a} # 11
b=4953
echo ${#b} # 4
if语句
1.基本判断
if [ <some test> ]
then
<commands>
fi
if [ $1 -gt 100 ]
then
echo Hey that's a large number.
pwd
fi
date
2.test命令
if语句中的方括号其实就是执行了test命令。
test 001=1 # false,=用来字符串比较的
test 001 -eq 1 # true,-eq用来数值比较
整理了test参数如下:
Operator | Description |
---|---|
! EXPRESSION | The EXPRESSION is false. |
-n | STRING The length of STRING is greater than zero. |
-z | STRING The lengh of STRING is zero (ie it is empty). |
STRING1 = STRING2 | STRING1 is equal to STRING2 |
STRING1 != STRING2 | STRING1 is not equal to STRING2 |
INTEGER1 -eq INTEGER2 | INTEGER1 is numerically equal to INTEGER2 |
INTEGER1 -gt INTEGER2 | INTEGER1 is numerically greater than INTEGER2 |
INTEGER1 -lt INTEGER2 | INTEGER1 is numerically less than INTEGER2 |
-d FILE | FILE exists and is a directory. |
-e FILE | FILE exists. |
-r FILE | FILE exists and the read permission is granted. |
-s FILE | FILE exists and it’s size is greater than zero (ie. it is not empty). |
-w FILE | FILE exists and the write permission is granted. |
-x FILE | FILE exists and the execute permission is granted. |
3. if..else
if [ <some test> ]
then
<commands>
else
<other commands>
fi
#!/bin/bash
# elif statements
if [ $1 -ge 18 ]
then
echo You may go to the party.
elif [ $2 == 'yes' ]
then
echo You may go to the party but be back before midnight.
else
echo You may not go to the party.
fi
4.逻辑运算
#!/bin/bash
# and example
if [ -r $1 ] && [ -s $1 ]
then
echo This file is useful.
fi
5.case语句
#!/bin/bash
# case example
case $1 in
start)
echo starting
;;
stop)
echo stoping
;;
restart)
echo restarting
;;
*)
echo don\'t know
;;
esac
循环
1.while循环
#!/bin/bash
# Basic while loop
counter=1
while [ $counter -le 10 ]
do
echo $counter
((counter++))
done
echo All done
2. for循环
#!/bin/bash
# Basic for loop
names='Stan Kyle Cartman'
for name in $names
do
echo $name
done
echo All done
for value in {1..5}
do
echo $value
done
echo All done
函数
#!/bin/bash
# Basic function
print_something () {
echo Hello I am a function
}
print_something
print_something
1.参数
同脚本参数一样,在函数体内,$1
代表该函数的第一个参数。
#!/bin/bash
# Passing arguments to a function
print_something () {
echo Hello $1
}
print_something Mars
print_something Jupiter
2.返回值
#!/bin/bash
# Setting a return status for a function
print_something () {
echo Hello $1
return 5
}
print_something Mars
print_something Jupiter
echo The previous function has a return value of $? # 上一条命令的返回值,即函数返回的5