在bash编程中也有最基本的条件测试,if和test。下面是一些基本的实例
文件拷贝输出检查
#!/bin/bash
#cpFile
if cp ./hello.c ../hello2.c; then
echo "copy file successfully"
exit 0
else
echo "copy file error" 2>&1
exit 1
fi
判断是否处于根目录
#!/bin/bash
#is in root directory
if [ "$(pwd)" == "/" ]; then
echo "OK,run!"
exit 0
else
echo "You need to be in the root directory not $DIRECTORY to run " 2>&1 > /dev/null
exit 1
fi
文件权限测试
#!/bin/bash
#!test file's authority
if [ $# -lt 1 ]; then
echo "please input : $0 filename"
exit 1
fi
if [ -w "$1" ]; then
echo " $1 write : permitted "
fi
变量测试变量是否为空
#!/bin/bash
#变量是否为空测试
#EDITOR="fire"
if [ -z $EDITOR ]; then
echo "Variable not set"
exit 0
else
echo "$EDITOR have been set"
fi