今天写了个linux shell脚本,通过不同命令行参数来执行不同操作,使用到了linux shell的命令行参数输入和case语句,此篇做备忘录吧。
#!/bin/sh
case $1 in
webp) cd webp;;
jpeg) cd jpeg;;
zlib) cd zlib;;
lz) cd lz;;
*) echo "Invalid parameter!"
exit -1
;;
esac
make clean
make
make install
cd ..
exit
为了进行交叉编译,需要将.dll转化为.def和.a文件,所以我写了个批量处理的脚本,如下:
#!/bin/bash
#文件夹是否存在
if [ ! -d lib ]; then
mkdir lib
fi
for file in $(ls *.dll)
do
#echo $file
i686-w64-mingw32-dlltool --dllname $file --def ${file%.*}.def --output-lib ./lib/${file%.*}.a
done
exit 0
在做调试spiceserver时,有时候我需要将图片进行保存,为此我需要写一个脚本,将文件夹中的所有图片进行tar压缩,然后拷贝到指定位置:
#!/bin/bash
IMAGE_DIR=/tmp/image
CURR_DIR=$(pwd)
#文件夹是否存在
if [ ! -d $IMAGE_DIR ]; then
mkdir $IMAGE_DIR
fi
#文件夹是否为空
if [ -n "`ls -A $IMAGE_DIR`" ]; then
cd $IMAGE_DIR
#将当前文件夹中的所有文件打包压缩,排除当前目录和父目录
tar -czvf ./image.tar.gz --exclude . --exclude .. *
cp -f image.tar.gz $CURR_DIR/image/
rm -rf *
else
echo "dir $IMAGE_DIR is empty!"
fi
exit 0
接下来来一个1到100求和的:
#!/bin/sh
x=1
sum=0
while [ "$x" -le 100 ]; do
sum=$(($sum+$x))
x=$(($x+1))
done
echo $sum
exit 0