预定义变量

  1. $#: 参数的个数
  2. $@: 使用空格分割的参数列表
  3. $*: 使用IFS分割的参数列表
  4. $0: 程序名称
  5. $$: 当前shellPID
  6. $!: 最后执行的后台进程的pid
  7. $?: 最后执行命令的返回值

if 判断字符串是否相等

  1. #坑爹到是中括号两边都得留空格
  2. sl=$SHELL
  3. if [ "$sl" == "/bin/zsh" ]
  4. then
  5. echo "zsh"
  6. fi

文件判断

  1. #判断文件是否存在
  2. if [ ! -f "~/zshrc" ]; then
  3. echo "no exist"
  4. else
  5. echo "exisst"
  6. fi

image.png
image.png

case 语法

  1. case $1 in
  2. start)
  3. /opt/nexus-3.19.1-01/bin/nexus start
  4. ;;
  5. stop)
  6. /opt/nexus-3.19.1-01/bin/nexus stop
  7. ;;
  8. restart)
  9. /opt/nexus-3.19.1-01/bin/nexus restart
  10. ;;
  11. *)
  12. echo "no this syn"
  13. ;;
  14. esac

获取脚本所在目录

  1. base=`cd $(dirname $0); pwd`