(6-2)shell编程 (2)


for循环:通过使用一个变量去遍历给定列表中的每个元素,在每次变量赋值时执行一次循环体,直至赋值完成所有元素退出循环。

-----------------------------------------格式 1 -----------------------------:
写法一:
[root@baolibin shell]# vi for1.sh
[root@baolibin shell]# more for1.sh
#!/bin/bash
#----------
for ((i=0;i<10;i++))
do
echo $i
done
[root@baolibin shell]# chmod +x for1.sh
[root@baolibin shell]# ./for1.sh
0
1
2
3
4
5
6
7
8
9
[root@baolibin shell]#



写法二:
[root@baolibin shell]# cp for1.sh for2.sh
[root@baolibin shell]# vi for2.sh
[root@baolibin shell]# more for2.sh
#!/bin/bash
#----------
for ((i=0;i<10;i++));do
echo $i
done
[root@baolibin shell]# ./for2.sh
0
1
2
3
4
5
6
7
8
9
[root@baolibin shell]#


-----------------------------------------格式 2 -----------------------------:

[root@baolibin shell]# cp for1.sh for3.sh
[root@baolibin shell]# vi for3.sh
[root@baolibin shell]# more for3.sh
#!/bin/bash
#----------
for i in 0 1 2 3 4 5 6 7 8 9
do
echo $i
done
[root@baolibin shell]# ./for3.sh
0
1
2
3
4
5
6
7
8
9
[root@baolibin shell]#




-----------------------------------------格式 3 -----------------------------:

[root@baolibin shell]# cp for1.sh for4.sh
[root@baolibin shell]# vi for4.sh
[root@baolibin shell]# more for4.sh
#!/bin/bash
#----------
for i in {0..9}
do
echo $i
done
[root@baolibin shell]# ./for4.sh
0
1
2
3
4
5
6
7
8
9
[root@baolibin shell]#





条件测试:
bash条件测试:命令执行成功与否即为测试。

第一种:test EXPR
第二种:[ EXPR ]    注意:中括号与表达式之间有空格。

整型测试:
-gt: 大于,如 [ $num1 -gt $num2 ]
-lt:小于
-ge:大于等于
-le:小于等于
-eq:等于
-nq:不等于
字符串测试:
>:大于  [ "$str1" \> "$str2" ]
<:小于
=
!=




算数运算:
let varName=算数表达式
varName=$[算数表达式]
varName=$((算数表达式))
varName=` expr $num1 + $num2 `



[root@baolibin shell]# num=1+1
[root@baolibin shell]# echo $num
1+1
[root@baolibin shell]# let num=1+1
[root@baolibin shell]# echo $num
2
[root@baolibin shell]# num=$[1+2]
[root@baolibin shell]# echo $num
3
[root@baolibin shell]# num=$((1+3))
[root@baolibin shell]# echo $num
4
[root@baolibin shell]# num=` expr 1 + 4 `
[root@baolibin shell]# echo $num
5
[root@baolibin shell]# num = 1 + 1
-bash: num: command not found
[root@baolibin shell]#






区分大小写:
[root@baolibin shell]# touch TEST.sh
[root@baolibin shell]# ll
总用量 32
-rwxr-xr-x. 1 root root 52 4月  18 21:58 test.sh
-rw-r--r--. 1 root root  0 4月  19 13:40 TEST.sh




while/until循环
适用于循环次数未知,或不便用for直接生成较大的列表时
格式:
while 测试条件;do
循环体
done

如果测试条件为真,则进入循环,退出条件为测试条件为假
until循环的格式和while循环的格式一致,但是与while循环的意思相反,如果测试条件为假则进入循环,测试条件为真时则退出循环。



-------------------------------------while格式1----------------------------------------
[root@baolibin shell]# vi while1.sh
[root@baolibin shell]# more while1.sh
#!/bin/bash
#----------
i=0
while test $i -lt 10
do
echo $i
let i++
done
[root@baolibin shell]# chmod +x while1.sh
[root@baolibin shell]# ./while1.sh
0
1
2
3
4
5
6
7
8
9
[root@baolibin shell]#



-------------------------------------while格式2----------------------------------------
[root@baolibin shell]# cp while1.sh while2.sh
[root@baolibin shell]# vi while2.sh
[root@baolibin shell]# more while2.sh
#!/bin/bash
#----------
i=0
while [ $i -lt 10 ]
do
echo $i
let i++
done
[root@baolibin shell]# ./while2.sh
0
1
2
3
4
5
6
7
8
9
[root@baolibin shell]#




-------------------------------------until格式----------------------------------------
[root@baolibin shell]# cp while1.sh until.sh
[root@baolibin shell]# vi until.sh
[root@baolibin shell]# more until.sh
#!/bin/bash
#----------
i=0
until [ $i -gt 10 ]
do
echo $i
let i++
done
[root@baolibin shell]# ./until.sh
0
1
2
3
4
5
6
7
8
9
10
[root@baolibin shell]#








if判断:

单分支:
if 测试条件;then
选择分支
fi

双分支:
if 测试条件
then
 选择分支1
else
选择分支2
fi

多分支:
if 条件1;then
分支1
elif 条件2;then
分支2
elif 条件3;then
分支3
...
else
分支n
fi



单分支:
[root@baolibin shell]# vi if1.sh
[root@baolibin shell]# more if1.sh
#!/bin/bash
#-----------
if [ $1 -eq 1 ]
then
echo "one"
fi
[root@baolibin shell]# chmod +x if1.sh
[root@baolibin shell]# ./if1.sh 1
one
[root@baolibin shell]# ./if1.sh 2
[root@baolibin shell]#



双分支:
[root@baolibin shell]# cp if1.sh if2.sh
[root@baolibin shell]# vi if2.sh
[root@baolibin shell]# ./if2.sh 1
one
[root@baolibin shell]# ./if2.sh 2
none
[root@baolibin shell]#


多分支:
[root@baolibin shell]# cp if2.sh if3.sh
[root@baolibin shell]# vi if3.sh
[root@baolibin shell]# more if3.sh
#!/bin/bash
#-----------
if [ $1 -eq 1 ]
then
echo "one"
elif [ $1 -eq 2 ]
then
echo "two"
elif [ $1 -eq 3 ]
then
echo "three"
else
echo "none"
fi
[root@baolibin shell]# ./if3.sh 1
one
[root@baolibin shell]# ./if3.sh 2
two
[root@baolibin shell]# ./if3.sh 3
three
[root@baolibin shell]# ./if3.sh 4
none
[root@baolibin shell]#




case判断:
有多个测试条件时,case语句会使语法结构更清晰
格式:
case 变量引用 in
     PATTERN1)
	分支1
	;;
     PATTER2)
	分支2
	;;
     ...
     *)
	分支n
	;;
esac


PATTERN:类同于文件名通配机制,但支持使用 | 表示或者
a|b:a或者b
*:匹配任意长度的任意字符
?:匹配任意单个字符
[]:指定范围内的单个字符



[root@baolibin shell]# vi case.sh
[root@baolibin shell]# more case.sh
#!/bin/bash
#----------
case $1 in
1)
echo "one"
;;
2)
echo 'two'
;;
3)
echo "three"
;;
*)
echo "none"
;;
esac
[root@baolibin shell]# chmod +x case.sh
[root@baolibin shell]# ./case.sh 1
one
[root@baolibin shell]# ./case.sh 2
two
[root@baolibin shell]# ./case.sh 3
three
[root@baolibin shell]# ./case.sh 4
none
[root@baolibin shell]#




自定义函数
function 函数名(){
...
}
引用自定义函数文件时,使用source func1.sh有利于代码的重用性。


[root@baolibin shell]# vi func1.sh
[root@baolibin shell]# more func1.sh
#!/bin/bash
#------------
function test(){
echo 'this is a function'
}

test
[root@baolibin shell]# chmod +x func1.sh
[root@baolibin shell]# ./func1.sh
this is a function
[root@baolibin shell]#




[root@baolibin shell]# cp func1.sh func2.sh
[root@baolibin shell]# vi func2.sh
[root@baolibin shell]# more func2.sh
#!/bin/bash
#------------
source /usr/local/shell/func1.sh
test
[root@baolibin shell]# ./func2.sh
this is a function
this is a function
[root@baolibin shell]#





date:
显示当前时间
格式化输出: +%Y-%m-%d
格式化%s:表示自1970-01-01 00:00:00以来的描述
指定时间输出:--date='2009-01-01 11:11:11'
指定时间输出:--date='3 days ago'


[root@baolibin shell]# date
2015年 04月 19日 星期日 14:38:40 CST
[root@baolibin shell]# date +%Y-%m-%d
2015-04-19
[root@baolibin shell]# date +%Y-%m-%d %H:%M:%S
date: 额外的操作数 "%H:%M:%S"
请尝试执行"date --help"来获取更多信息。
[root@baolibin shell]# date +%Y-%m-%d_%H:%M:%S
2015-04-19_14:41:03
[root@baolibin shell]# date "+%Y-%m-%d %H:%M:%S"
2015-04-19 14:41:15
[root@baolibin shell]#



[root@baolibin shell]# date +%s
1429425733
[root@baolibin shell]# date --date="2015-01-05"
2015年 01月 05日 星期一 00:00:00 CST
[root@baolibin shell]# date --date="2015-01-05" +%Y-%m-%d
2015-01-05
[root@baolibin shell]# date +%s
1429425804
[root@baolibin shell]# date --date="2015-01-05" +%s
1420387200
[root@baolibin shell]#



内建命令与外部命令:
[root@baolibin shell]# date
2015年 04月 19日 星期日 14:38:40 CST
[root@baolibin shell]# type date
date is hashed (/bin/date)
[root@baolibin shell]# type cd
cd is a shell builtin
[root@baolibin shell]# man date



read:
read命令接收标准输入(键盘)的输入,或者其它文件描述符的输入。得到输入后,read命令将数据放入一个标准变量中。
格式:
read VAR_NAME
read -p "Enter your name:"VAR_NAME
read如果后面不指定变量,那么read命令会将接收的数据放置在环境变量REPLY中。
read -t 5 -p "Enter your name:"VAR_NAME
read -s -p "Enter your password:"pass


[root@baolibin shell]# echo $baozi

[root@baolibin shell]# read baozi
www.hadoop.com
[root@baolibin shell]# echo $baozi
www.hadoop.com
[root@baolibin shell]# read
hahahehe
[root@baolibin shell]# echo $REPLY
hahahehe
[root@baolibin shell]# read -p "Enter your name:" baozi
Enter your name:xiaobaoziya
[root@baolibin shell]# echo $baozi
xiaobaoziya
[root@baolibin shell]# read -t 3 -p "Enter your name:" baozi
Enter your name:[root@baolibin shell]#

[root@baolibin shell]# read -s -p "Enter your name:" baozi
Enter your name:[root@baolibin shell]#
[root@baolibin shell]# echo $baozi
hehe
[root@baolibin shell]#




declare:
用来限定变量的属性:
-r:只读
-i:整数:某些算数计算允许在被声明为整数的变量中完成,而不需要特别使用expr或let来完成。
-a:





[root@baolibin shell]# vi declare1.sh
[root@baolibin shell]# more declare1.sh
#!/bin/bash
#-----------
num=0
echo $num
declare -r num
num=1
echo $num
[root@baolibin shell]# chmod +x declare1.sh
[root@baolibin shell]# ./declare1.sh
0
./declare1.sh: line 6: num: readonly variable
0
[root@baolibin shell]#




[root@baolibin shell]# cp declare1.sh declare2.sh
[root@baolibin shell]# vi declare2.sh
[root@baolibin shell]# more declare2.sh
#!/bin/bash
#-----------
declare -i num
num=1+1
echo $num
[root@baolibin shell]# ./declare2.sh
2
[root@baolibin shell]#








  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值