http://www.runoob.com/linux/linux-shell-variable.html
零、前提
1.
Shell 脚本(shell script),是一种用shell编写的脚本程序
shell (开发shell语言)与 shell script (使用shell编程) 不同
2.
shell 脚本可执行,不是因为 .sh 扩展名称
3.运行方式
3.1
cd 到当前目录
chmod +x ./*.sh 是因为文件具有可执行的权限
./*.sh .表示当前目录
3.2
/bin/sh *.sh 不需修改文件权限,直接运行
bash *.sh
3.3
source *.sh
3.4
sh -x *.sh 测试运行,输出每一步的执行逻辑及细节
sh -n *.sh 检查语法错误
一、变量定义及赋值
1.变量声明赋值
yourname=hello
注意:
等号两侧无空格
与java不同,无需指明变量类型
2.变量使用
${yourname}
3.重新赋值
yourname=world
4.只读
readonly yourname
试图修改只读变量时,会有异常
/usercode/file.sh: line 9: yourname: readonly variable
5.删除
unset yourname
echo $yourname
无输出结果
6.异常示例
yourname=hello world
echo $yourname
输出hello
异常提示:/usercode/file.sh: line 5: world: command not found
空格被理解为分隔符号,空格前赋值结束,world被机器解释为命令,而无此命令,所以异常
修正
yourname="hello world"
echo $yourname
7.变量类型
- 系统变量,大写,定义在 /ect/profile 中
- 局部变量,写在函数内部的变量,作用域的范围在函数内部,函数外则失效
普通变量==》环境变量
cat /etc/profile | grep yourname ==> 结果无
export yourname
cat /etc/profile | grep yourname ==> 结果有
删除环境变量
vim /ect/profile 不同于普通变量 unset yourname 即可,否则,重启后,系统变量依然存在
环境变量使用
test=$LOGNAME
echo $test ==> 登录用户名
test=$HOME ==> home文件路径
test=$LANG ==> 编码格式 UTF-8
8.变量内容由其他命令提供
yourname=$(date +%F)
echo $yourname
yourname=`date +%F`
echo $yourname
二、字符串
echo "hello ${yourname}"
echo 'hello ${yourname}'
1.单引号
直接输出,不转义
echo ''
2.双引号
转义输出,引用变量
3.长度
yourname="hello"
strlength=${#yourname}
echo "strlength:${strlength}"
4.子串
strsub=${yourname:1:2}
echo "strsub:${strsub}"
输出:el
不同于java
System.out.println("hello".substring(1, 2));
输出:e
5.字符位置
string="string is string"
echo `expr index "$string" s`
输出:1
不同于java:
System.out.println("string is string".indexOf("s"));
0,从0开始计数
6.其他运算
str1="a"
str2="b"
str3="a"
#是否相等,一个等号
if [ $str1 = $str3 ];then echo "eq" ; else echo "ne" ;fi
[ $str1 != $str3 ]
# 为空,返回true
[ -z $str1 ]
#非空,返回true
[ -n $str1 ]
#非空为true
[ $str1 ]
三、数组
1.定义
以空格分隔元素
array=(1 2 3 4 5 6)
数组中的数据格式可不一致
array=(1 2 3 "4" 5.67 8 )
echo ${array[@]}
echo ${array} 若未指定下标,默认输出第一个元素
2.长度
echo ${#array[@]}
3.数组中元素引用
${array[1]}
4.所有元素
array=(1 2 3 4 5 6) for str in ${array[*]} do echo $str done
四、注释
#---------
#
#---------
#后为注释
五、接收传参
1.
param.sh
#文件名称
filename="filename:$0"
echo $filename
#参数个数
paramcount="param-count:$#"
echo $paramcount
#第一个参数
firstparam="firstparam:$1"
echo $firstparam
#第二个参数
secondparam="secondparam:$2"
echo $secondparam
#所有参数
allparam="allparam:$*"
echo $allparam
#所有参数
allparame="allparame:$@"
echo $allparame
currentpid="currentpid:$$"
echo $currentpid
#循环
for str in "$*"; do
echo $str
done
for str in "$@"; do
echo $str
done
#显示最后命令的退出状态。0表示没有错误
lastrunstate="lastrunstate:$?"
echo $lastrunstate
lastpid="lastpid:$!"
echo $lastpid
运行
./param.sh 1 2 3 4 5 6 7 8 9
用括号而非中括号,数据间以空格分割
2.$* 与 $@ 区别:
只有在双引号中体现出来。
"$*" 等价于 "1 2 3 ..."(传递了九个参数)
"$@" 等价于 "1" "2" "3" ...(传递了九个参数)。
3.第十个以后的参数
${10}
六、循环
#!/bin/bash
i=0
s=0
while [ $i -le 100 ]
do
i=$(($i+1))
s=$(($s+$i))
done
echo $s
while [ $i -le 100 ]
do
let "i++"
let s=s+i
done
echo $s
1.
array=(1 2 3 "4" 5.67 8 )
for str in ${array[@]}
do
echo ${str}
done
==>
for str in ${array[@]}; do echo ${str}; done
即,若去掉分行,以;间隔各个分界
后一种适用于命令行运行
init=0
while (($init < 5))
do
echo $init
let "init++"
done
num=3
case num in
1) echo "1" ;;
2) echo "2" ;;
*) echo "3" ;;
esac
s=0
i=0
while (($i <= 100))
do
s=` expr $s + $i `
let "i++"
done
echo $s
七、判断
if条件
if [ $a eq $b ]
then
echo
else
echo
fi
注意[]使用及内部空格
a=10
b=20
if [ $a -eq $b ]
then
echo "eq"
elif [ $a -gt $b ]
then
echo "gt"
else
echo "lt"
fi
==>
if [ $a -eq $b ] ; then echo "eq" ; elif [ $a -gt $b ] ; then echo "gt" ; else echo "lt" ;fi
八、运算
1.算术运算
a=10
b=20
#1
c=`expr $a + $b`
echo $c
#2
d=$(($a + $b))
echo $d
#3
let e=a+b
echo $e
#4
i=0
let "i++"
let i=i+1
#5
f=$[a+b]
echo $f
备注:
使用此种方式
s=`expr $a + $b`
其他:- / % \*(乘法需转义)
2.比较运算
关系运算符只支持数字,不支持字符串,除非字符串的值是数字
a=10
b=20
#相等
if [ $a -eq $b ]
then
echo "eq"
else
echo "ne"
fi
#不等
if [ $a -ne $b ]
then
echo "ne"
else
echo "eq"
fi
# >
if [ $a -gt $b ]
then
echo "gt"
else
echo "lt"
fi
# <
if [ $a -lt $b ]
then
echo "lt"
else
echo "gt"
fi
# >=
if [ $a -ge $b ]
then
echo "ge"
else
echo "le"
fi
# <=
if [ $a -le $b ]
then
echo "le"
else
echo "ge"
fi
3.布尔运算
! 非
a=10
b=20
if [ ! $a -gt $b ]
then
echo "1"
else
echo "2"
fi
-o 或
a=10
b=20
c=30
if [ $a -gt $b -o $a -lt $c ]
then
echo "1"
else
echo "2"
fi
-a 且
a=10
b=20
c=30
if [ $a -gt $b -a $a -lt $c ]
then
echo "1"
else
echo "2"
fi
4.逻辑运算
&& ||
a=10
b=20
c=30
if [[ $a -gt $b && $a -lt $c ]]
then
echo "1"
else
echo "2"
fi
注意:两个[[ ]] 如果写成一个[] ,会提示缺少 []
5.文件判断
file=./xxx.txt 文件路径
if [ -r $file ]
then
echo "1"
else
echo "2"
fi
-r 可读
-w 可写
-x 执行
九、输入、输出
1.>
覆盖输出
echo "hello" > test.sh
echo "yourname=\"hello\"" > test.sh
echo "$yourname" > test.sh
转义 $yourname 为空,添加为空
echo '@yourname' > test.sh
不转义,正常添加
2.>>
追加输出
3.显示执行结果
echo `ls -al /home`
echo `date`
十、test
用于数值、字符、文件判断
a=10
b=20
if [ $a -eq $b ]
then
echo "1"
else
echo "2"
fi
if test $a -eq $b
then
echo "1"
else
echo "2"
fi
if test $[a] -eq $[b]
then
echo "1"
else
echo "2"
fi
if [] ==> if test
十一、
while read -p "please input a num :" num
do
echo "$num"
done
ctrl+d 截止输入并推出
十二、使用命令赋值
shell
使用命令赋值
base=`basename /usr/local/`
`` 数字键1旁边的按键