Bash验证和条件语句

Bash验证和条件语句,bash,file,installation,version,verification,Bash,File,Installation,Version,Verification,我正在为bash中的程序编写安装脚本。安装的第一部分检测程序是否已安装。这是通过指定目录中存在文件来检测的。那部分很好用。但是,如果旧版本存在,我希望用户能够升级到新版本。我以为我的代码是对的,但不太管用。/var/www/html/gvsms中有一个名为.version的文件,该文件中只包含程序的版本。我想将这个数字与安装程序的数字进行比较。不过,我不确定在if语句中是如何实现的。一个If中是否有多个If?这就是我到目前为止所做的: $installerver=1.1 $installedve

我正在为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
程序的工作方式是,如果检测到文件,安装的版本与安装程序不相同(或低于),并且用户同意升级,则应运行升级命令。如果用户拒绝升级,则退出脚本

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

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

这可能吗?
谢谢

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

大括号用于简单的变量替换,如
echo“youbunded$qty${item}s.”
中所示

  • 将shebang行添加到脚本顶部:

    #!/bin/bash
    
  • 变量赋值不带美元符号:

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

    installedver=$(cat /var/www/html/gvsms/.version)
    
  • if块中有一个坏的
    fi

  • 您缺少一个
    else
  • 在对文件进行分类之前,您可能应该测试该文件是否存在
  • 你应该用双引号引用你所有的变量
  • 在条件括号内需要空格
    […]
    […]]
  • 这将使您的代码运行:

    #!/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
    

    我想你忘了把台词写进剧本了。尝试插入
    #/bin/bash
    在第一行。我确实把它放在那里了,但它在粘贴的问题区域之外。好的,我修复了它,并删除了变量定义处的$。但现在我得到了这个:
    cat:/var/www/html/gvsms/。版本:没有这样的文件或目录安装开始。。。。检查GVoice SMS通知系统之前是否已安装…是。我检测到以前安装的GVoice SMS通知系统/测试x:第13行:[:1.1:一元运算符应为否。我尚未检测到以前安装的GVoice SMS通知系统。若要继续安装,请按Enter键。否则按Ctrl-C键。
    /var/www/html/gvsms/。版本:没有此类文件或目录。
    。您绝对确定该文件存在吗?看起来像是形而上学的证书nty确认这是代码错误或文件命名错误。请检查文件名本身是否没有空格或其他非显示字符,或者您的代码是否以某种方式添加了空格或其他字符。对于文件名,请使用
    ls-l/var/www/html/gvsms/.version*| cat-vet
    查找文件名中的空格等。祝您好运!抱歉。我的不好。它现在确实存在。新建o输出为:
    安装开始…检查GVoice SMS通知系统是否以前安装过…是。我检测到GVoice SMS通知系统以前安装过。否。我没有检测到GVoice SMS通知系统以前安装过。若要继续安装,风险自负,请按Enter。否则Ctrl-C.
    那么这是否能满足您的需要?如果是,请勾选@ascotan。他完全解决了您的问题,因此赢得了这项服务。如果您觉得他和其他人帮助了您,请向他们投票。如果这还没有解决您的问题,那么您需要告诉我们为什么它不正确。祝您好运!不,仍然不起作用。我已经放弃它一段时间了虽然,我无意中发现了它。看起来棒极了!谢谢!但是,它仍然不起作用。
    安装开始…检查GVoice SMS通知系统以前是否安装过…是的。我检测到GVoice SMS通知系统以前安装过../test.x:第15行:[:缺少
    ]'./test.x:第15行::未找到命令'Grr。代码标记与错误混淆。应该缺少']'。@Ross:在右括号前还缺少一个空格。
    #!/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