当我通过将脚本放在/ usr / local / bin中全局使用脚本时,bash脚本标志参数不起作用

时间:2019-04-17 19:06:52

标签: bash macos shell

我有以下代码。文件名是vb。

#!/bin/bash

valid='/version/'
versionJson=""
bumpThis=""
bumped=""
while read line; do
if echo "$line" | grep '"version": '; then
 versionJson=$line
fi
done < package.json

version=$(echo "$versionJson" | sed -e 's/"version": "\(.*\)",/\1/')
IFS='.'; splitVersion=($version); unset IFS;

while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in
  -m | -minor )
    bumpThis=${splitVersion[1]}
    bumped="\"version\": \"${splitVersion[0]}.$((bumpThis + 1)).${splitVersion[2]}\","
    ;;
  -M | -major )
    bumpThis=${splitVersion[0]}
    bumped="\"version\": \"$((bumpThis + 1)).${splitVersion[1]}.${splitVersion[2]}\","
    ;;
  -p | -patch )
    bumpThis=${splitVersion[2]}
    bumped="\"version\": \"${splitVersion[0]}.${splitVersion[1]}.$((bumpThis + 1))\","
    ;;
  *)
    bumpThis=${splitVersion[2]}
    bumped="\"version\": \"${splitVersion[0]}.${splitVersion[1]}.$((bumpThis + 1))\","
    ;;
esac; shift; done

if (( $OPTIND == 1 )); then
   echo no commands
   bumpThis=${splitVersion[2]}
   bumped="\"version\": \"${splitVersion[0]}.${splitVersion[1]}.$((bumpThis + 1))\","
fi

echo "new version: " $bumped
sed -i '' "s/$versionJson/$bumped/g" package.json
npm install

当我在./vb -minor之类的目录中运行脚本时,它运行正常。但是,如果我将文件放在/ usr / local / bin中并尝试运行vb -minor,它将不会传递该标志,并且始终会运行默认行为。我究竟做错了什么?我尝试创建一个简单的测试脚本,该脚本仅回显/ usr / local / bin中的一个参数,效果很好。

0 个答案:

没有答案