shell 如何退出bash脚本

whhtz7ly  于 2023-01-31  发布在  Shell
关注(0)|答案(1)|浏览(252)

我有一个case命令,它有几个选项,其中一个是失败的存储空间大小。

#################### System Information Tests ##############################
export HOME=`cd;pwd`
export Storage_Space_Size=$(df -h --output=avail ${HOME} | awk 'NR==2 {print $1}' | tr -cd '[:digit:]')
export Storage_Space_Units=$(df -h --output=avail ${HOME} | awk 'NR==2 {print $1}' | tr -cd '[:alpha:]')
export Storage_Space_Required="350"

echo " Testing for Storage Space for installation"
echo " "

case $Storage_Space_Units in
    [Pp]* )
      echo "Sufficient storage space for installation found" ;;
    [Tt]* )
      echo "Sufficient storage space for installation found" ;;
    [Gg]* )
      if [[ ${Storage_Space_Size} -lt ${Storage_Space_Required} ]]; then
        echo "Not enough storage space for installation"
      else echo "Sufficient storage space for installation found."
      fi ;;
    [MmKk]* )
      echo "Not enough storage space for installation."
      exec $SHELL ;;
    * )
    echo "Not enough storage space for installation."
    exec $SHELL ;;
  esac

echo " "

exec $SHELL是退出脚本的最佳方法还是有其他方法?
退出脚本而不保留失败环境的替代方法

oipij1gg

oipij1gg1#

################### System Information Tests ##############################

if [ "$SYSTEMOS" = "Linux" ]; then

  export HOME=`cd;pwd`
  export Storage_Space_Size=$(df -h --output=avail ${HOME} | awk 'NR==2 {print $1}' | tr -cd '[:digit:]')
  export Storage_Space_Units=$(df -h --output=avail ${HOME} | awk 'NR==2 {print $1}' | tr -cd '[:alpha:]')
  export Storage_Space_Required="350"
  echo "-------------------------------------------------- "
  echo " "
  echo " Testing for Storage Space for installation"
  echo " "

  case $Storage_Space_Units in
      [Pp]* )

        echo " "
        echo "Sufficient storage space for installation found"
        echo "-------------------------------------------------- " ;;
      [Tt]* )
        echo " "
        echo "Sufficient storage space for installation found"
        echo "-------------------------------------------------- " ;;
      [Gg]* )
        if [[ ${Storage_Space_Size} -lt ${Storage_Space_Required} ]]; then
          echo " "
          echo "Not enough storage space for installation"
          echo "-------------------------------------------------- "
          exit
        else
          echo " "
          echo "Sufficient storage space for installation found."
          echo "-------------------------------------------------- "
        fi ;;
      [MmKk]* )
        echo " "
        echo "Not enough storage space for installation."
        echo "-------------------------------------------------- "
        exit ;;
      * )
      echo " "
      echo "Not enough storage space for installation."
      echo "-------------------------------------------------- "
      exit ;;
    esac

  echo " "
fi


if [ "$SYSTEMOS" = "MacOS" ]; then
  while true; do
    read -r -p "Do you have minimum of 350GB of FREE storage space available for this instllation (Y/N) " yn
    case $yn in
      [Yy]* )
      echo "-------------------------------------------------- "
      echo " "
      echo "Installation will move forward"
      break
        ;;
      [Nn]* )
      echo " "
      echo "Not enough storage space available. Exiting script."
      break
        ;;
      * ) echo "Please answer yes or no.";;
    esac
  done
fi

相关问题