shell script 用在系统管理上面是很好的一项工具,但是用在处理大量数值运算上,就不够好了,因为Shell scripts 的速度较慢,使用的CPU 资源较多,造成主机资源的分配不良。(常常会去呼叫外部的函式库)。

shell script的撰写和执行

执行

  1. # 绝对路径和相对路径执行
  2. /home/yanjing/test.sh
  3. ./test.sh
  4. # 放在PATH 指定的目录内,例如:~/bin/
  5. # shell.sh 在~/bin 内且具有rx 的权限
  6. test.sh # 如果test.sh
  7. # /bin/sh 其实就是/bin/bash (连结档),使用sh shell.sh亦即告诉系统,
  8. # 我想要直接以bash 的功能来执行shell.sh这个文件内的相关指令的意思,
  9. # 所以此时你的shell.sh 只要有r 的权限即可被执行喔
  10. # 不推荐!
  11. bash test.sh
  12. sh test.sh
  1. #!/bin/bash
  2. echo -e "hello world \a \n"
  3. # 执行成果告知(定义回传值)。离开script 并且回传一个0给系统(执行echo $?可得到)
  4. exit 0
  5. # 输出系统变量
  6. echo "PATH=$PATH"
  7. echo "USER=$USER"

案例

  1. ImageMagic 涵盖除法、浮点数计算 ```shell

    !/bin/bash

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 ```

  1. if后面的[]中的内容前后要有空格