Linux Shell 编程 笔记

--------------------------------------- 【Shell编程笔记】 ---------------------------------------
【1】变量
i=1 #等号两边不能有空格,所有变量只有一种类型——字符窜
echo $i # 引用变量要用$符号,如果变量不止由字母数字下划线组成,还需要加{},如${A[0]}

【2】 echo `date '+%Y-%m-%d %H:%M:%S'` # 后置引用,将date输出返回
【3】 : # 空命令,等价于NOP,也可认为与true相同,用途如下面:
while : # 同while true,死循环
do
...
done
if :l # 等价于if [ 1 ],等价于if test 1
then :
else
take-some-action
fi
: > filename #创建空文件或清空文件,同cat /dev/null > filename但不会产生新进程
【4】let执行整数运算
i=1
j=2
let "k=i+j"
((k=i+j))
k=$i+$j
【5】 格式化数字01,02,...
num=`printf "%02d" 1`
echo $num
==============================================================
【6】 test的用法 等价于[ contitions ]
1)判断表达式
test 表达式为真 等价于if test ! 表达式为假
test 表达式1 –a 表达式2 两个表达式都为真
test 表达式1 –o 表达式2 两个表达式有一个为真
2)判断字符串
test –n 字符串 字符串的长度非零
test –z 字符串 字符串的长度为零
test 字符串1 = 字符串2 字符串相等
test 字符串1 != 字符串2 字符串不等
3)判断整数
test 整数1 –eq 整数2 整数相等(equal)
test 整数1 –ge 整数2 整数1大于等于整数2 (greater or equal)
test 整数1 –gt 整数2 整数1大于整数2 (greater than)
test 整数1 –le 整数2 整数1小于等于整数2 (less or equal)
test 整数1 –lt 整数2 整数1小于整数2 (less than)
test 整数1 –ne 整数2 整数1不等于整数2 (not equal)
4)判断文件
test File1 –ef File2 两个文件具有同样的设备号和i结点号
test File1 –nt File2 文件1比文件2 新
test File1 –ot File2 文件1比文件2 旧
test –b File 文件存在并且是块设备文件
test –c File 文件存在并且是字符设备文件
test –d File 文件存在并且是目录
test –e File 文件存在
test –f File 文件存在并且是正规文件
test –g File 文件存在并且是设置了组ID
test –G File 文件存在并且属于有效组ID
test –h File 文件存在并且是一个符号链接(同-L)
test –k File 文件存在并且设置了sticky位
test –b File 文件存在并且是块设备文件
test –L File 文件存在并且是一个符号链接(同-h)
test –o File 文件存在并且属于有效用户ID
test –p File 文件存在并且是一个命名管道
test –r File 文件存在并且可读
test –s File 文件存在并且是一个套接字
test –t FD 文件描述符是在一个终端打开的
test –u File 文件存在并且设置了它的set-user-id位
test –w File 文件存在并且可写
test –x File 文件存在并且可执行

==============================================================
【7】数组
(1) name = (value1 ... valuen) 此时下标从0开始
(2) name[index] = value
数组下标的范围没有任何限制,同时也不必使用连续的分量.
$ A=(a b c def)
==================================================
$ echo ${A[@]} //取全部元素
a b c def
==================================================
$ echo ${A[0]} //取第一个元素,等价于echo $A
a
==================================================
# 取得数组元素的个数
$ echo ${#A[@]}
4
$ echo ${#A[*]}
4
$ echo ${#A[3]} //取得元素3即def的长度
3
==================================================
$ A[3]=yaoshuyin //将第三个元素重新赋值
$ echo ${A[@]}
a b c yaoshuyin
==================================================
//清除变量
$ unset A
$ echo ${A[@]}
$
=============示例 while循环===========
#建立数组
arrSource=("arrJobs.php" "arrSubHangye.php" "arrFirst.php" )
arrDest=("buildhr" \
"buildtrain/htdocs" \
"bankhr" \
"healthr" \
"elehr" \
)

#取数组无元素个数
lenArrSource=${#arrSource[*]}
lenArrDest=${#arrDest[*]}


#循环列出数组元素
i=0
while [ $i -lt $lenArrSource ]
do
echo ${arrSource[$i]}
let i++
done


i=0
while [ $i -lt $lenArrDest ]
do
echo ${arrDest[$i]}
let i++
done


==============示例: for循环==============

#源文件
arrSource=("/home/800hr/htdocs/login_jump.php")

#目标网站
arrDest=(ithr elehr buildhr bankhr healthr ctvhr chenhr mechr clothr cneduhr 56hr tourhr foodhr greenhr cnlawhr waimaohr)

for outer in ${arrSource[*]} #${arrSource[*]} 是数组中的所有元素
do
for inner in ${arrDest[*]}
do
echo "ln -s $outer /home/${inner}/campus/"
done
done

==============================================================
【8】预留变量
  $# 位置参数的数量
  $* 所有位置参数的内容,同$@
  $? 命令执行后返回的状态
  $$ 当前进程的进程号
  $! 后台运行的最后一个进程号
  $0 当前执行的进程名
$- SHELL的当前选项,和set相同

==============================================================
【9】控制语句
条件测试有:[[]],[],test 这几种,注意:[[]] 与变量之间用空格分开。
1)条件执行
if conditions; then
elif conditions; then
else
fi
for
2)条件执行
case 变量 in
值1)
语句...
;;
值2)
语句...
;;
...
值n)
语句...
;;
*) #其它情况
语句...
;;
esac
3)for循环
for i in $(seq 10); do
echo $i; # 输出1 2 3 4 5 6 7 8 9 10 不换行
done;
for((i=1;i<=10;i++));do
echo -n "$i "; # 输出1 2 3 4 5 6 7 8 9 10 不换行
done;
4)while循环
i=10;
while [[ $i -gt 5 ]];do
echo $i;
((i--));
done;
while read line; do
echo $line;
done < /etc/hosts; # 按行读取/etc/hosts内容并输出,同cat /etc/hosts
5)until循环(条件与while相反)
a=10;
until [[ $a -le 0 ]]; do # 同while [[ $a -gt 0 ]]; do
echo $a;
((a--));
done;
6)select选择语句
供用户选择选项,用case语句判断执行不同的语句。典型菜单。
select ch in "begin" "end" "exit"
do
case $ch in
"begin")
echo "start something"
;;
"end")
echo "stop something"
;;
"exit")
echo "exit"
break;
;;
*)
echo "Ignorant"
;;
esac
done;
执行效果如下(?后面是用户输入选择):
1) begin
2) end
3) exit
#? 1
start something
#? 2
stop something
#? 4
Ignorant
#? 6
Ignorant
#? 3
exit
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值