shell脚本,可以封装一系列命令,组合成一个命令,简化我们日常在linux下的工作量,这是我学shell最初的一个原因。当然shell还能敢其他事情。
shell脚本if else 语句的使用
#!/bin/bash
NUM1=100
NUM2=200
if(($NUM1>$NUM2));then
echo "num1>num2"
else
echo "num1<num2"
fi
shell脚本for循环的使用
seq 1 15 :是linux下的命令。从1到15.
#!/bin/bash
for i in `seq 1 15`
do
echo "数字 $i"
done
shell脚本while循环使用
while [ $i -le 100 ] 与 while [ $i -lt 100 ]
-le:小于等于,-lt:小于
#!/bin/bash
i=0
while [[ $i -lt 100 ]]
do
echo "数字 $i"
i=`expr $i + 1`
done
shell脚本file文件操作
检查文件是否存在,不存在创建,存在输出内容
#!/bin/sh
NDK=/root/text.txt
if [ ! -f $NDK ]; then
mkdir -p /root/wangyi
else
echo "目录已经存在"
cat $NDK
fi
shell脚本读取文件内容并逐行输出
#!/bin/bash
while read line
do
echo $line
done</root/text.txt
shell脚本字符串操作:
#!/bin/bash
a="abc"
b="efg"
if [ $a = $b ]
then
echo "$a = $b : a 等于 b"
else
echo "$a = $b: a 不等于 b"
fi
if [ -z $a ]
then
echo "-z $a : 字符串长度为 0"
else
echo "-z $a : 字符串长度不为 0"
fi
if [ $a ]
then
echo "$a : 字符串不为空"
else
echo "$a : 字符串为空"
fi
shell脚本压缩文件
#!/bin/bash
for i in `find /root -name "*.sh"`
do
tar -czf wangi.tgz $i
done
shell脚本函数的使用
#!/bin/bash
aa="this is aa"
bb="this is bb"
function test(){
echo $1
cc="this. is cc"
dd="this is dd"
echo $cc
echo "我是一个函数"
}
test 10