1. source命令
直接运行和source运行的区别,比如cd命令,如果直接运行,就达不到效果,还用source运行就能达到效果。
source会在当前shell环境中执行,如果直接执行,会开启一个子shell,在子shell中进入了该目录,脚本执行完后会退出子shell回到父shell环境
2. if语句
判断文件夹是否存在:
myPath="/var/log/httpd/"
if [ ! -d "$myPath"]; then
mkdir "$myPath"
elif [ "$1"x = "projectx"]; then
echo "build $1"
else
exit
fi
摘自:
http://blog.csai.cn/user1/14572/archives/2007/12090.html
3. 时间函数
按当前时间创建文件夹
da=`date +%y%m%d%H%M`
mkdir "dir$da"
4. 传递参数
#!/bin/sh
if [ $# != 2 ]; then
echo "Usage: test.sh input1 input2"
else
echo "Your input is:" $1 $2
fi
这里$#是参数个数, $1 $2 就是参数啦
5. 文件操作
判断文件是否存在
#!/bin/bash
filename=/home/jifeng/shell/file
if [ -e $filename ]
then
echo "$filename exited"
fi
判断文件是否为空
#!/bin/bash
filename=/home/jifeng/shell/file
echo $filename
if [[ ! -s $filename ]]
then
echo "file is null"
else
echo "file is not null"
fi
遍历一个目录下的所有文件
#!/bin/bash
readpath="/home/jifeng/AndesProject"
for file in $readpath/*
do
echo "$file"
done