Bash 验证和条件语句

标签 bash file installation version verification

我正在为 bash 中的程序编写安装脚本。安装的第一部分检测程序是否已经安装。这是通过指定目录中是否存在文件来检测的。那部分工作正常。但是,如果旧版本存在,我希望用户能够升级到新版本。我以为我的代码是正确的,但它并不完全有效。/var/www/html/gvsms 中有一个名为 .version 的文件,文件中只包含程序的版本。我想将该号码与安装程序的号码进行比较。不过,我不确定如何在 if 语句中。一个if里面可以有多个if吗?这是我目前所拥有的:

$installerver=1.1
$installedver=${cat /var/www/html/gvsms/.version}
echo "Installation beginning...."
echo " "
echo " "
echo -n "Checking to see if GVoice SMS Notification System was previously installed..."
if [ -a /var/www/html/gvsms/index.php ]; then 
   echo "Yes."
   echo ""
   echo "I have detected a previous installation of GVoice SMS Notification System."
   if [ $installerver == $installedver ]; then
       echo "The GVoice SMS Notification System is currently installed and is the same version as this installer. If you were trying to upgrade, please check the installer you downloaded and try again."
       exit
   fi
   elif [ $installedver == "0.5" || $installedver == "1.0"]; then
   while [[ "$yn" != "Yes" && "$yn" != "Y" && "$yn" != "y" && "$yn" != "yes" ]]; do
      echo "Would you like to upgrade? Yes or no."
      read -r  upgrade
      echo "$upgrade upgrade?"
      echo "Is this correct? (Yes or No)"
      read yn
   done
   echo "Upgrade proceeding..."
   fi
echo "No."
echo "I have not detected a previous installation of GVoice SMS Notification System."
read -p "To proceed with installation at your own risk, press Enter. Otherwise Ctrl-C."

现在程序出错了:

./test.x: line 1: =1.1: command not found
./test.x: line 2: ${cat /var/www/html/gvsms/.version}: bad substitution

程序的工作方式是,如果检测到文件,安装的版本与安装程序不同(或低于),并且用户同意升级,升级命令应该运行。如果用户拒绝升级,则退出脚本。

如果程序已安装并且与安装程序的版本相同,则显示错误信息并退出。

如果程序尚未安装,则跳过重新确认安装并继续正常安装。

这可能吗? 谢谢!

最佳答案

  1. 将 shebang 行添加到脚本的顶部:

    #!/bin/bash
    
  2. 变量赋值是在没有美元符号的情况下完成的:

    installerver=1.1
    
  3. 子 shell 命令的变量赋值使用圆括号而不是大括号:

    installedver=$(cat /var/www/html/gvsms/.version)
    
  4. 你的 if block 中有一个错误的 fi

  5. 你缺少一个 else block
  6. 您可能应该在抓取文件之前测试该文件是否存在。
  7. 你应该双引号所有的变量
  8. 条件括号 [ … ][[ … ]]
  9. 中需要空格

这将使您的代码运行:

#!/bin/bash

installerver="1.1"
echo "Installation beginning...."
echo " "
echo " "
echo -n "Checking to see if GVoice SMS Notification System was previously installed..."
if [ -e "/var/www/html/gvsms/index.php" -a -r "/var/www/html/gvsms/.version" ]; then
   echo "Yes."
   echo ""
   echo "I have detected a previous installation of GVoice SMS Notification System."
   if [ "$(cat /var/www/html/gvsms/.version)" == "$installedver" ]; then
       echo "The GVoice SMS Notification System is currently installed and is the same version as this installer. If you were trying to upgrade, please check the installer you downloaded and try again."
       exit
   elif [ "$installedver" == "0.5" || "$installedver" == "1.0" ]; then
       while [[ "$yn" != "Yes" && "$yn" != "Y" && "$yn" != "y" && "$yn" != "yes" ]]; do
          echo "Would you like to upgrade? Yes or no."
          read -r  upgrade
          echo "$upgrade upgrade?"
          echo "Is this correct? (Yes or No)"
          read yn
       done
       echo "Upgrade proceeding..."
   fi
else
    echo "No."
    echo "I have not detected a previous installation of GVoice SMS Notification System."
    read -p "To proceed with installation at your own risk, press Enter. Otherwise Ctrl-C."
fi

关于Bash 验证和条件语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6103624/

相关文章:

bash - 如何使用ffmpeg有序地合并目录中的所有视频?

installation - 如何实现WiX安装程序升级?

mysql - bash中返回变量的问题

bash - rsync --include 选项不排除其他文件

linux - 是否可以在 Linux 中可靠地嵌套 find -exec 命令?

c++ - 一次输入一行到c++中的字符串数组

bash - 如何递归遍历目录树并仅查找文件?

c++ - FAT,检索文件时优化性能

linux - 设置 ddd 以调试 bash 脚本时出错

Android 通过包名获取安装位置