Shell编程一定要注意空格的格式,不然会出错
4、测试语句
(1)、字符串
相等: == 、=
不等: !=
字符串是否为空: test -z "" zero 字符串的长度是否为0,如果0就是空
字符串是否为非空: test -n "abcd" 这个表示是非空。
命令执行的返回值:如果成立的话,返回0,表示为真。
(2)、数值
大于: -gt
小于: -lt
不等于: -ne
等于: -eq
大于等于: -ge
小于等于: -le
a=100
b=200
test $a -lt $b
可以查看下面这个表
(3)、文件:
判断是不是一个文件: -f filename
判断是不是一个目录: -d filename
判断是不是一个符号连接:-L filename
判断可读属性: -r
判断可写属性: -w
判断可执行的属性: -x
判断文件是否存在并且长度不为0: -s
判断f1是否比f2新: f1 -nt f2
判断f1是否比f2旧: f1 -ot f2
(4)、逻辑关系:
与: &&
或: ||
非: !
示例代码
s1="abc"
s2="abc"
test "$s1" == "$s2"
#test "$s1" = "$s2"
#test "$s1" != "$s2"
#test "$s1" < "$s2"
#test -n ""
echo "\$? : $?"
a=100
b=200
test $a -lt $b
echo "a b \$? : $?"//知道上条语句的结果
面试题
a=0
if [ $a = 0 ]
then
echo corret
elif [ $a = 1 ]
then
echo error
fi
5、结构性语句
(1)、if语句
if test xxxx
then
do something
fi
if [ -e "a.txt" ]
then
cat "a.txt"
else
do something
fi
if [ -e "a.txt" ]; then
cat "a.txt"
fi
if [ ]
then
do something
elif [ ]
then
do something
else
do something
fi
判断一个文件是否存在,然后是否有可读的属性,如果都成立,把这个文件cat, 如果文件存在但是没有可读的权限
先给它加一个可读的权限,然后在cat出来,如果没有这个文件,那把这个文件创建一下。
把有一段代码注释掉:
:<<!EOF!
代码段
!EOF!
:<<!EOF!
if [ -e "a.txt" ];then
cat "a.txt"
else
echo "a.txt not exist"
fi
!EOF!
echo "aaaa"
if [ -e "a.txt" ] && [ -r "a.txt" ]
then
cat "a.txt"
echo "cat a.txt"
elif [ -e "a.txt" ]
then
chmod +x "a.txt"
cat "a.txt"
echo "chmod +x a.txt"
else
touch "a.txt"
echo "touch a.txt"
fi
(2)、多路分支:
case 字符串变量 in
情况1)
。。。。。
;; #这个;;的作用类似break,但是在shell里面,一定要有这两个分号
情况2)
。。。。。。。。。。。
;;
*)
如果以上的情况都不匹配的话,就匹配这里
esac
1、辣子鸡
2、红烧鱼
3、白切鸡
4、锅包肉
今天你想吃什么菜:
示例代码:
#!/bin/bash
echo "1. 辣子鸡"
echo "2. 红烧鱼"
echo "3. 白切鸡"
echo "4. 锅包肉"
#echo "你今天想吃什么:"
#read num
read -p "xxxxxx: " num
case $num in
1)
echo "你选的是辣子鸡"
;;
2)
echo "你选的是红烧鱼"
;;
*)
echo "今天不知道吃什么"
esac
(3)、循环语句
while [ 条件 ] ===>逻辑:当条件成立的时候,一直循环,直到条件不成立
do
do something
done
expr: 表示里面的是一个算术表达式,只能用于整数的简单运算。
加(+) 减(-) 乘(\*) 除(\/) 求模(%)
ret=` expr 5 + 3 \* 4 `
echo $ret
从1加到100
i=0
sun=0
while [ $n -le 100 ]
do
i=$(($i+1)) # <===> i++
done
echo "1+2+3+...100=$sum"
===================================================================================================
until [ 条件 ] =====> 直到条件成立,退出循环
do
do something
done
echo "1+2+3+...100=$sum"
示例代码
Until
#!/bin/bash
declare -i i=0
declare -i sum=0
until [ $i -gt 99 ]
do
i=i+1
sum=sum+i
# i=$(($i + 1))
# sum=$(($sum + $i))
# sum=` expr $sum + $i `
done
echo "1+2+3...+100=$sum"
===================================================================================================
for((i=0;i<100;i++ ))
do
do something
if [ $i -eq 10 ];then
break;#退出整个循环 #continue 退出本次循环
fi
done
通过for循环求1到100的和
示例代码
#!/bin/bash
sum=0
for((i=0;i<100;i=i+1))
do
sum=$(($sum+$i+1))
if [ $i -eq 10 ]
then
break
fi
done
echo 1+2+3+...100=$sum
===================================================================================================
for var in 列表 a b c d e f
do
echo $var
Done
多次放
#!/bin/bash
file=`ls`
for i in $file
do
echo $i
Done
测IP:
ip_file=./ip.txt
>$ip_file
for i in 192.168.5.{1..10}
do
ping -c 1 $i >> /dev/null
if [ $? -eq 0 ];then
echo $i
echo "Life ip: $i" 1>> $ip_file
fi
Done
===================================================================================================
数组:
在shell里面,数组的类型,有两种,一种是整数,一种是字符串。
fun() {
arry=(88 99 22 33 55 337)
len=${#arry[@]}
max=${arry[0]}
for((i=1;i<$len;i++))
do
if [ ${arry[i]} -gt $max ];then
max=${arry[i]}
fi
done
echo "The max number is $max"
}
从键盘输入数组的值:
read -a array
===================================================================================================
(4)、函数
function(这个关键字可写可不写) func_name()
{
arg1 ===> $1
arg2 ===> $2
。。。。
echo 一个值(这个值就是返回)
}
调用函数,传递参数,并且获得函数的返回值
1)获得返回值的方法:ret=` func_name arg1 arg2 `
2)调用函数最后一个命令或者表达式的返回值(最后用return语句返回一个值)
fun_name arg1 arg2
ret=$?
练习:写一个函数,实现在函数里面求和,并且把结果返回,在调用函数的时候,把参数传递过去。
通过函数求和(带参数)
#!/bin/bash
sum1()
{
a=$1
b=$2
sum=` expr $a + $b `
echo $sum
}
#ret=` sum1 3 4 `
#echo "ret :$ret"
sum2()
{
a=$1
b=$2
sum=` expr $a + $b `
return $sum
}
sum2 2 3
ret=$?
echo "ret :$ret"
通过函数求和
#!/bin/bash
fun()
{
a1=$1
a2=$2
a3=$(($a1+$a2))//统一用这种会好一点
echo $a3
}
#ret=` fun 3 4 `
#echo $ret
sum2()
{
a=$1
b=$2
sum=$(($a+$b))
return $sum
}
sum2 3 4
ret=$?
echo $ret
echo $max
6、什么是正则表达式?
通过我们这个表达式去找到我们感兴趣的一些信息,把这些信息提取出来。
通过一个一定规则写出来的表达式,去匹配我们所需要的信息。这个表达式就叫做正则表达式。
正则表达式的作用?
- 、用它来搜索我们感兴趣的信息。//要用的时候查看下面这个表
|
|
|
|
| 2056 grep -P 'h.m' test.txt |
|
|
| 2057 grep -P '^deer' test.txt |
|
|
| 2058 grep -P '$88' test.txt |
|
|
| 2059* grep -P '' test.txt |
|
|
| 2060 grep -P '88$' test.txt |
|
|
| 2061 history |
|
|
| 2062 ls |
|
|
| 2063 cat -n test.txt |
|
|
| 2064 grep -P 'hi' test.txt |
|
|
| 2065 grep -P '\bhi\b' test.txt |
|
|
| 2066 vim test.txt |
|
|
| 2067 grep -P '\bhi\b.*\bLucy\b' test.txt |
|
|
| 2068 grep -P '0\d\d-\d\d\d\d\d\d\d\d' test.txt |
|
|
| 2069 grep -P '0\d{2}-\d{8}' test.txt |
|
|
| 2070 grep -P '^\d{5,12}$' test.txt |
|
|
| 2071 grep -P 't[erm]i' test.txt |
|
|
|
|
|
|
|
|
|
|
|
|
| |
|
|
|
|
正则表达式的使用 | https://blog.youkuaiyun.com/yufenghyc/article/details/51078107 |
|
|
|
|
|
|
正则表达式的使用 |
|
| |
|
|
|
|
shell脚本编程 | https://blog.youkuaiyun.com/erica_1230/article/details/71330725 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|