shell脚本编程进阶:
-d:确定文件是否为目录
-f:确定文件是否为普通文件
-e:确定文件是否存在
-r:确定文件是否可读
-w:确定文件是否可写
-x:确定文件是否可执行
-s:确定文件名长度是否大于零
#!/bin/bash
if [ -d $1 ]
then
ls $1
else
echo "$1 is not a directory"
fi
#保存,isadir.sh。source isadir.sh ~
#!/bin/bash
case "$1" in
[A-Z]|[a-z])
echo "letter";;
[0-9])
echo "digit";;
*)
echo "other symbol";;
esac
#->iscase.sh #source iscase.sh hello #source iscase.sh ""
#!/bin/bash
for var in hello world !
do
echo $var
done #hello.sh
#!/bin/bash
for((i=0;i<5;i++))
do
echo $i
done #iValue.sh
#!/bin/bash
index=`expr length "$1"`
while [ $index -gt 0 ]
do
str=$str `expr substr "$1" $index 1`
index =$[$index-1]
done
echo $str
#->whileShell.sh
#source whileShell.sh "HelloWorld!反转倒置字符串!"
#!/bin/bash
for var in Jack Hose Mark Hellen
do
if [ $var = Mark ]
then
break;
fi
echo $var
done
echo -e "---------------------------------------------------\n"
for var in Jack Hose Mark Hellen
do
if [ $var = Mark ]
then
continue;
fi
echo $var
done
#test.sh #source test.sh
sh [选项] [文件]:
-n:检查shell脚本语法错误,如果没错误就没有输出信息
-x:显示脚本实际执行过程
sh -x ./test.sh
#!/bin/bash
if [! $# -eq 2]
then
echo "please input two parameters"
exit
fi
#divideNew.sh
#!/bin/bash
count=`who | grep "\(192\.168\.126\.[1-9][0-9]*\)" | wc -l`
if [ $count -gt 0 ]
then
date>> checkLoginIP.log
echo `who | grep "\(192\.168\.126\.[1-9][0-9]*\)" `>>checkLoginIP.log
fi
#检查过滤 #checkLoginIP.sh
#!/bin/bash
if[ $# -ne 1 ]
then
echo "please input the backup directory"
exit
fi
filelist=`ls $1`
backupdir=$HOME/backup`date"+%Y%m%d%H%M%S"`
mkdir $backupdir
echo "now start backuping,the directory name is $backupdir"
for filename in $filelist
do
if [ -f "$1/$filename" ]&&[ -x "$1/$filename" ]
then
cp "$1/$filename" "$backupdir/$filename"
fi
done
#backupDir.sh