Shell之三
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Shell之三相关的知识,希望对你有一定的参考价值。
第1章 while循环语句
在编程语言中,while循环是一种控制流程的陈述,利用一个返回结果为布林值的表达式,当这个表达式的返回值为真时,则反复执行循环的条件,若表达式的返回值为假,则不在执行循环的条件
1.1 while语法:
while 条件
do
命令
done
1.2 while使用场景:
1.2.1 多用于创建守护进程
[[email protected] tmp]# cat test.sh
#!/bin/bash
##############################################################
# File Name: test.sh
# Version: V1.0
# Author: da ya
# Organization: [email protected]
# Created Time : 2018-03-24 19:48:06
# Description:
##############################################################
while true
do
echo "ok" | nc -l 81
done
[[email protected] tmp]# sh test.sh
GET / HTTP/1.1
User-Agent: curl/7.29.0
Host: 10.0.0.31:81
Accept: */*
GET / HTTP/1.1
User-Agent: curl/7.29.0
Host: 10.0.0.31:81
Accept: */*
1.2.2 可以创建定时任务,执行时间小于一分钟的定时任务
[[email protected] tmp]# cat test.sh
#!/bin/bash
while true 使返回值总是为真,所以会一直循环
do
uptime
sleep 1
done
[[email protected] tmp]# sh test.sh
19:48:45 up 2 days, 4:12, 4 users, load average: 0.08, 0.04, 0.05
19:48:46 up 2 days, 4:12, 4 users, load average: 0.08, 0.04, 0.05
19:48:47 up 2 days, 4:12, 4 users, load average: 0.08, 0.04, 0.05
19:48:48 up 2 days, 4:12, 4 users, load average: 0.07, 0.04, 0.05
^C
1.3 while作用:
补充定时任务功能,执行小于一秒的任务
循环执行某些操作,例如水果菜单
1.4 遍历文件中的每一行:
1.4.1 方法一:
while read i
do
echo "$i"
done < /etc/hosts
[[email protected] scripts]# sh test.sh
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.16.200 mirrors.aliyun.com
1.4.2 方法二:
cat /etc/hosts|while read i
do
echo "$i"
done
1.4.3 方法三:
exec < /etc/hosts
while read i
do
echo "$i"
done
获取一行中每个单词
while read i
do
echo "$i"
for w in $i
do
echo "$w"
done
done < /etc/hosts
第1章 循环控制脚本
1.1 示例:Break
for i in {1..5}
do
if [ "$i" -eq 2 ];then
break
fi
echo "$i"
done
echo OK
[[email protected] scripts]# sh test.sh
1
OK
1.2 示例:continue
for i in {1..5}
do
if [ "$i" -eq 2 ];then
continue
fi
echo "$i"
done
echo OK
[[email protected] scripts]# sh test.sh
1
3
4
5
OK
1.3 示例:exit
for i in {1..5}
do
if [ "$i" -eq 2 ];then
exit
fi
echo "$i"
done
echo OK
[[email protected] scripts]# sh test.sh
1
第2章 shell数组
简答的说:数组就是多个变量的组合
[[email protected] scripts]# name=(jiang bo yang)
[[email protected] scripts]# echo ${name[1]}
bo
[[email protected] scripts]# echo ${name[2]}
yang
[[email protected] scripts]# unset name[1]
[[email protected] scripts]# echo ${name[1]}
[[email protected] scripts]# echo ${name[*]}
jiang yang
[[email protected] scripts]# echo ${name[@]}
jiang yang
[[email protected] scripts]# name[1]=bo
[[email protected] scripts]# echo ${name[@]}
jiang bo yang
第1章 shell函数
1.1 定义函数:
jiang(){
echo nihao
}
jiang 表示调用上面定义的函数
[[email protected] scripts]# sh test.sh
nihao
1.2 函数的扩展用法:添加一条命令
[[email protected] scripts]# source test.sh
nihao
[[email protected] scripts]# jiang
nihao
1.3 函数的传参
jiang(){
echo nihao $1
}
daya(){
echo buhao $1
}
jiang $1
daya $1
[[email protected] scripts]# sh test.sh 1
nihao 1
buhao 1
1.4 basename命令的用法
[[email protected] scripts]# basename /server/scripts/test.sh
test.sh
[[email protected] scripts]# basename /server/scripts/test.sh .sh
test
第2章 shell调试技巧:
2.1 断电调试:
jiang(){
echo nihao $1
}
daya(){
echo buhao $1
}
jiang
exit 在可能有问题地方加上exit,不在继续向下执行脚本,缩小排查范围
daya
2.2 使用-x参数:
[[email protected] scripts]# sh -x test.sh
+ jiang
+ echo nihao
nihao
+ daya
+ echo buhao
buhao
2.3 指定调试范围:
[[email protected] scripts]# sh test.sh
nihao
+ daya
+ echo buhao
buhao
+ set +x
以上是关于Shell之三的主要内容,如果未能解决你的问题,请参考以下文章