Bash 验证和条件语句

发布于 2024-11-09 11:02:46 字数 1794 浏览 4 评论 0原文

我正在为 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

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

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

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

这可能吗? 谢谢!

I am in the process of writing an install script for a program in bash. The first part of the install detects whether the program is already installed or not. This is detected by the presence of a file in a specified directory. That part works fine. However, I want the user to be able to upgrade to the new version if the old version exists. I thought I had the code right, but it doesn't quite work. There is a file in /var/www/html/gvsms called .version that simply has the version of the program in the file. I want to compare that number to the number of the installer. I'm not sure how, though, inside an if statement. Can there be multiple Ifs inside an If? This is what I have so far:

$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."

Right now the program errors out with:

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

The way the program should work is that if the file is detected, installed version is not (or less than) the same as the installer, and the user says yes to upgrade, the upgrade commands should run. If the user says no to upgrade, then exit the script.

If the program is installed and is the same version as the installer, display error message and exit.

If the program is not already installed, skip reconfirm installation and proceed with normal install.

Is this possible?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手心的海 2024-11-16 11:02:46
  1. 将 shebang 行添加到脚本顶部:

    <前><代码>#!/bin/bash

  2. 变量赋值无需美元符号即可完成:

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

    installedver=$(cat /var/www/html/gvsms/.version)
    
  4. if 块中存在错误的 fi

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

这将使您的代码运行:

#!/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
  1. Add the shebang line to the top of the script:

    #!/bin/bash
    
  2. Variable assignment is done without the dollar sign:

    installerver=1.1
    
  3. Variable assignment from a subshell command uses parenthesis not braces:

    installedver=$(cat /var/www/html/gvsms/.version)
    
  4. There's a bad fi in your if block

  5. You're missing an else block
  6. You should probably test if the file exists before cat'ing it.
  7. You should double-quote all your variables
  8. You need spaces inside conditional brackets [ … ] and [[ … ]]

This will get your code running:

#!/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
寻梦旅人 2024-11-16 11:02:46

您的作业有语法错误。分配给变量时,不要输入美元符号 $。对于命令替换,请使用括号 $(...)

installerver=1.1
installedver=$(cat /var/www/html/gvsms/.version)

大括号用于简单的变量替换,如 echo "You buy $qty ${item}s."

Your assignments have syntax errors. When assigning to a variable you do not put the dollar sign $. And for command substitution use parentheses $(...).

installerver=1.1
installedver=$(cat /var/www/html/gvsms/.version)

Curly braces is for simple variable substitution, as in echo "You bought $qty ${item}s.".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文