bash 的测试、判断、循环、函数、数组

一、bash的测试

1.test能够理解3种类型的表达式
(1)字符串比较
(2)数字比较
(3)文件测试

2.字符串比较(STRING)

-n STRING      如果这个字符串的长度不是0

if [ -n "abc" ];then
echo haha
fi

-z STRING       如果这个字符串的长度是0

STRING1 = STRING2      如果这两个字符串相等

STRING1 != STRING2      如果这两个字符串不相等

3.数字

greater than大于    less than小于    equal等于

eq  等于

ne  不等于

ge  大于等于

gt    大于

le     小于等于

lt     小于

方法一:if   test   5 -eq 3 ;then
echo  hello
fi      

方法二:if [ 5 -eq 3 ];then
echo  hello
fi

4.文件
-f 存在且是正规文件
-d 存在且是目录
-b 块设备
-c 字符设备
-h 存在且是符号链接
-e 文件存在
-S socket套结字
-p pipe管道

-r
-w
-x


判断文件是否存在:

方法一

if test -f /etc/passwd ; then

   echo  "存在"

fi

方法二

if [  -f /etc/passwd  ]; then       注意[后面空格]

    echo  "存在"

fi


二、bash判断

1.  exit语句:

退出程序的执行,并返回一个返回码,返回码为0正常退出,非0为非正常退出

例如:
exit 0


2.条件判断
If代码返回0表示真,非0为假

if语句语法如下:
if commans list1
;then
commands list2
commands list2_1
elif commands list3
;then
commands list4
elif list5
;then
commands list6
else
commands list7
fi

例子:

echo"Press y to continue"
read yn    ----->接受键盘输入的值符给变量
if[ "$yn" = "y" ]; then
echo "script is running..."
else
echo "STOP!"
fi


3.多条件联合

(1)逻辑与

if [  $condition1  ] && [  $condition2  ]

(2)逻辑或

if [  $condition1  ] || [  $condition2  ]

4.小判断:
[  -e /etc/passwd  ] && echo "文件存在"


5.case多条件判断

#!/bin/bash
read -p   "Do you want to continue [Y/N]? "  answer
case  $answer  in
Y|y)
echo  "fine ,continue" ;;
N|n)
echo  "ok,good bye" ;;
*)
echo  "error choice" ;;
esac
exit 0

命令;;表明流应该跳转到case语句的最后,类似C语言中的break指令。


6.  shell使用正则  匹配
#!/bin/bash
if [[  5  =~  [0-4]  ]];then
echo hello
fi

注意中括号和=~两侧的空格,语法要求很严格

=~匹配 !~不匹配

[[ ]] --其中一个代表test


三、bash循环

1.  for循环

for...do...done

第一种:

(1)for((i=0;i<=10;i++))
    do
    echo $i
    done

(2)x=0
    y=10
    for((i=$x;i<=$y;i++))
    do
    echo $i
    done

第二种:
(1)for i in 2 5 7 9 8 hello ad
    do
    echo $i
    done

(2)for i in `ls /tets/`
    do
    echo $i
    done

(3)for i in /etc/a* ;do
    echo $i
    done

(4)LIST="Tomy  Jony  Mary   Geoge"
    for i in $LIST
    do
    echo $i
    done

2.while循环

while...do...done

while [ $num -lt 100 ]
do
let num++             -----类似num=$[  $num+1  ]
echo hello
done

while  :  --- 死循环(:等于true)
do
echo hello
done

3.序列生成指令:seq
(1)#seq 10      输出1-10

(2)#seq 5 10    输出5-10

(3)$num =5
          $num1 =10
          for i in `seq $num $num1`

4.select循环

[root@xuniji /]# select i in  'ls -F'  'pwd '  'date'  'df -v'
> do

>$ i

>break

> done
1) ls -F
2) pwd
3) date
4) df -v
#? 3
2018年 05月 06日 星期日 14:05:56 CST

5.循环控制
break ---直接跳出大循环
continue ----跳出当前这一次循环,继续执行下一次循环

四、bash函数

myfunc( )     //函数定义
{
echo “This is my first shell function”
}
myfunc  //函数调用
unset   myfunc   //取消函数

1.命名空间
#!/bin/bash
c=5
local()
{
local c=4 ------(定义的c值只在函数中取4,函数之外仍取原来定义的值)
echo $c
}
echo $c

#bash local.sh
4
5

2.返回值
return value
value 0-255

设置返回值(默认为0)


3.例:

lsl( ){
ls –l
}
cd "$1" && lsl
bash   /etc           解释:(/etc传给$1)


五、数组

1.定义方法一:

# array=( one two three four five six )
#echo ${arry[0]}     ---输出数组第一个内容
one


#echo ${arry[*]}      ----输出数组所有内容

#arry[0]=3   ---赋值


#echo ${#arry[*]} ---- 数组长度

unset arry[0]=3   ----取消赋值


# array3=(`cat /etc/passwd`)     将该文件中的每一个行作为一个元素赋值给数组array3

# array5=(1 2 3 4 5 6 7   "linux shell"   [20]=saltstack)
跨越定义第21个  [20]=saltstack


2.定义方法二、

#!/bin/bash
  area[11]=23
  area[13]=37
  area[51]=UFOs
# 数组成员不必一定要连贯或连续的.
# 数组的一部分成员允许不被初始化.
# 数组中空缺元素是允许的.


echo  ${area[11]}        # {大括号}是需要的.


# 两个数组元素的和被赋值给另一个数组元素
  area[5]=`expr ${area[11]} + ${area[13]}`


3.定义方法三、

# declare -a myarry=(5 6 7 8)     declare定义数据类型 -a (arrays 数组)
# echo ${myarry[2]}
显示结果为 7


4.Declare命令定义数据类型
[test @test test]# declare [-afirx]
参数说明:
-a :定义为数组array
-f :定义为函数function
-i :定义为整数integer
-r :定义为只读      ------declare -r a=4 设置只读变量a=4后 再定义a不行


5.查看数组:
显示所有的数组元素:

(方法1)#declare -p array
(方法2)#echo ${array[*]}

(方法3)#echo ${array[@]}

6.查看数组长度:
#echo ${#array[*]}
5

7.查看数组元素:按下标
# echo ${array[0]}
one

8.获取数组元素的索引(下标):
# echo ${!array[*]}
0 1 2 3 4 5

9.数组切片:

# array=( one two three four five six )

[root@wing shell]# echo ${array[*]:1}
two three four five six
[root@wing shell]# echo ${array[*]:1:3} //从第2个元素开始往后打印3个
two three four

10.遍历数组:
for i in 循环


11.关联数组:下标是字符串
声明关联数组: 关联数组必须事先声明
# declare -A ass_array1
或者

# declare -A ass_array4='([index4]="bash shell" [index1]="tom" [index2]="jack" [index3]="alice" )'


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值