shell script 用在系统管理上面是很好的一项工具,但是用在处理大量数值运算上,就不够好了,因为Shell scripts 的速度较慢,使用的CPU 资源较多,造成主机资源的分配不良。(常常会去呼叫外部的函式库)。
shell script的撰写和执行
执行
# 绝对路径和相对路径执行
/home/yanjing/test.sh
./test.sh
# 放在PATH 指定的目录内,例如:~/bin/
# shell.sh 在~/bin 内且具有rx 的权限
test.sh # 如果test.sh
# /bin/sh 其实就是/bin/bash (连结档),使用sh shell.sh亦即告诉系统,
# 我想要直接以bash 的功能来执行shell.sh这个文件内的相关指令的意思,
# 所以此时你的shell.sh 只要有r 的权限即可被执行喔
# 不推荐!
bash test.sh
sh test.sh
#!/bin/bash
echo -e "hello world \a \n"
# 执行成果告知(定义回传值)。离开script 并且回传一个0给系统(执行echo $?可得到)
exit 0
# 输出系统变量
echo "PATH=$PATH"
echo "USER=$USER"
案例
source=”model.jpg” tmp=”tmp.jpg” dest=”dest.jpg”
包含服装主体的模特主体
modelDestHeight=$[800 - 20] echo “modelDestHeight=” $modelDestHeight
取模特高度的2/3,如果缩放后宽度小于800,则按宽度800缩放
ratio=echo "scale=5;$modelDestHeight / (2600 * 2 / 3)"|bc
if [ $(echo “scale=5;2000 * $ratio < 800”|bc) -eq 1 ];then
ratio=echo "scale=5;800 / 2000"|bc
fi
echo “ratio=” $ratio
tmpImgHeight=echo "scale=5;3000 * $ratio"|bc
echo “tmpImgHeight=” $tmpImgHeight
tmpOffsetTop=echo "scale=5;284 * $ratio"|bc
echo “tmpOffsetTop=” $tmpOffsetTop
cutTop=echo "scale=5;$tmpOffsetTop - 20"|bc
echo “cutTop=” $cutTop
offsetY=echo "scale=5;$tmpImgHeight / 2 - 800 / 2 - $cutTop"|bc
echo “offsetY=” $offsetY
convert $source -resize x$tmpImgHeight $tmp convert $tmp -gravity center -crop 800x800+0-$offsetY $dest ```
坑
- if后面的[]中的内容前后要有空格