shell脚本
set -x 可以查看脚本的执行流程
test或者[ 条件表达式 ] 条件判断,有两种情况,一种是判断成功,一种是判断失败
如果判断成功,则返回值为0,如失败,返回值则为非零
查看返回值是 echo $?
for循环
for i in `ls`
do
echo "This is $i"
done
条件判断
if [ 条件表达式 ];then
条件成立……
fi
if [ 条件表达式 ];then
条件成立……
else
条件失败……
fi
if [ 条件表达式 ];then
条件成立……
elif [ 条件表达式 ];then
条件成立……
elif [ 条件表达式 ];then
条件成立……
else
默认的命令序列
fi
1.如果不存在study目录,则创建一个study目录
在study目录下创建1-100.txt
#!/bin/bash
#后边是注释信息
#判断目录是否存在,不存在则创建
if [ ! -d study ];then
mkdir study
fi
for i in `seq 100`
#seq 100 生成1-100的数字
do
#判断目录下是否存在文件,不存在则创建
if [ ! -f study/$i.txt ]; then
touch study/$i.txt
fi
done
while循环
当文件的列是以空格作为分隔,并且要按行处理文件可以使用while读行循环
while read lien
do
done
df -P |sed ‘1d’|while read line
do
done
1.判断文件系统使用率,如果文件系统使用率超过70%,则报告挂载点和使用率的信息
#!/bin/bash
df -P|sed '1d'|while read line
do
syl=`echo "$line"|awk '{print $5}'|awk -F'%' '{print $1}'`
gzd=`echo "$line"|awk '{print $6}'`
if [ $syl -gt 70 ];then
echo "$gzd的使用率是$syl"
fi
done
2.
#!/bin/bash
while read line
do
nu=`echo "$line"|awk '{print $2}'`
xm=`echo "$line"|awk '{print $1}'`
echo "$xm 的电话是 $nu"
done
set -x 可以查看脚本的执行流程
test或者[ 条件表达式 ] 条件判断,有两种情况,一种是判断成功,一种是判断失败
如果判断成功,则返回值为0,如失败,返回值则为非零
查看返回值是 echo $?
for循环
for i in `ls`
do
echo "This is $i"
done
条件判断
if [ 条件表达式 ];then
条件成立……
fi
if [ 条件表达式 ];then
条件成立……
else
条件失败……
fi
if [ 条件表达式 ];then
条件成立……
elif [ 条件表达式 ];then
条件成立……
elif [ 条件表达式 ];then
条件成立……
else
默认的命令序列
fi
1.如果不存在study目录,则创建一个study目录
在study目录下创建1-100.txt
#!/bin/bash
#后边是注释信息
#判断目录是否存在,不存在则创建
if [ ! -d study ];then
mkdir study
fi
for i in `seq 100`
#seq 100 生成1-100的数字
do
#判断目录下是否存在文件,不存在则创建
if [ ! -f study/$i.txt ]; then
touch study/$i.txt
fi
done
while循环
当文件的列是以空格作为分隔,并且要按行处理文件可以使用while读行循环
while read lien
do
done
df -P |sed ‘1d’|while read line
do
done
1.判断文件系统使用率,如果文件系统使用率超过70%,则报告挂载点和使用率的信息
#!/bin/bash
df -P|sed '1d'|while read line
do
syl=`echo "$line"|awk '{print $5}'|awk -F'%' '{print $1}'`
gzd=`echo "$line"|awk '{print $6}'`
if [ $syl -gt 70 ];then
echo "$gzd的使用率是$syl"
fi
done
2.
#!/bin/bash
while read line
do
nu=`echo "$line"|awk '{print $2}'`
xm=`echo "$line"|awk '{print $1}'`
echo "$xm 的电话是 $nu"
done
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26705770/viewspace-1296668/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/26705770/viewspace-1296668/