1.if语句
语法为:
if [ 条件表达式 ]; then
.....
fi
条件表达式类型:
文件表达式
if [ -f file ] 如果文件存在 if [ -d ... ] 如果目录存在 if [ -s file ] 如果文件存在且非空 if [ -r file]如果文件存在且可读 if [ -w file]如果文件存在且可写 if [ -x file]如果文件存在且可执行
整数变量表达式
if [ int1 -eq int2 ] 如果int1等于int2 if [ int1 -ne int2 ] 如果不等于if [ int1 -ge int2 ]如果>= if [ int1 -gt int2 ]如果> if [ int1 -le int2 ]如果<= if [ int1 -lt int2 ]如果<
字符串变量表达式
If[ $a = $b ]如果string1等于string2 字符串允许使用赋值号做等号 if[ $string1 !=$string2 ]如果string1不等于string2if [ -n $string ] 如果string 非空(非0),返回0(true)if[ -z $string]如果string 为空
if [ $sting ] 如果string 非空,返回0 (和-n类似)
2.cocos2dx编写android.mk文件时要列出需要编译的.cpp .c文件,一个个查找非常麻烦,于是自己写了个脚本,输入命令就可以了。
用脚本之前,要先让它生效:chmod +x getfilelist
用法:./getfilelist ../../Classes list.txt ##第一个参数为,shell脚本名,第二个为cpp文件的相对路径,第三个为输出文件
shell代码:
#!/bin/sh
#help 提示
help()
{
echo !! Input error
echo USAGE: ./getfilelist 目录名 输出文件名
echo USAGE EXAMPLE: ./getfilelist ../Class list.txt
exit 0
}
getlist()
{
#第2个参数为空
if [ -z $2 ]; then
help
exit 1;
fi
#第一个参数为文件夹,则查找该路径下的.cpp .c文件并输出
if [ -d "$1" ]; then
find $1 -name "*.c"|sed 's/\.c/\.c \\/' > $2
#find $1 -name "*.cpp" >> $2
find $1 -name "*.cpp"|sed 's/\.cpp/\.cpp \\/' >> $2
echo "Success!"
else
echo "$1 not exits"
fi
}
#第一个参数为空
if [ -z $1 ]; then
help
exit 1;
fi
#参数遍历
while [ -n "$1" ]; do
case $1 in
-h) help;shift 1;;#function help is called
-*) echo "error: no such option $1. -h for help";exit 1;;
*) getlist $1 $2;break;;
esac
done