【问题标题】:Check version of OS then issue a command if the correct version检查操作系统版本,如果版本正确则发出命令
【发布时间】:2012-04-12 10:11:52
【问题描述】:

我正在为 Mac OS X Lion 10.7 编写一个 bash 脚本,我想知道如何在 bash 中检查操作系统的版本,如果版本是 10.7.1,那么它会执行一个命令并继续脚本并为不同的版本做同样的事情让我们说 10.7.3 然后它执行不同的命令然后用于 10.7.1 的命令?

【问题讨论】:

    标签: macos bash operating-system osx-lion sh


    【解决方案1】:

    您需要在 OS X 上使用 sw_vers 命令。它会打印一些人类可读的字符串,包括 10.X.X 系统版本 (sw_vers -productVersion)。也可以使用uname查看内核版本;如果您的脚本曾经移植到其他 Unix 变体,uname 将在那里工作。

    【讨论】:

      【解决方案2】:

      OS_Version(完整…示例 10.7.3)

      system_profiler SPSoftwareDataType | grep "System Version" | awk '{print $6}'
      

      sw_vers -productVersion
      

      操作系统(简短……示例 10.7)

      system_profiler SPSoftwareDataType | grep "System Version" | awk '{print $6}' | sed "s:.[[:digit:]]*.$::g"
      

      OS_Version=$(OS (short… example 10.7) | sed "s:.[[:digit:]]*)
      

      bash:

      #!/bin/bash
      
      # Use one of the examples given above to create the OS_Version variable
      
      if [[ ${OS_Version} == 10.7.3 ]]; then
         echo "Operating System is a match... will continue on."
      else
         echo "Operating System is NOT a match... will NOT continue."
      fi
      

      【讨论】:

      • 感谢您的修复和提示。
      • 我不得不使用 5 美元而不是 6 美元。
      • sw_vers -productVersion 可以说是第一个答案。从经验和阅读 cmets 来看,它避免了担心 $5$6 的区别,而且它更具可扩展性
      • 你会如何检查if [[ ${OS_Version} >= 10.7.3 ]];
      【解决方案3】:

      如果您只需要检查主要操作系统版本,请记住与它对应的 Darwin 版本,并且在 bash 中设置了一个易于强制转换为数值可比较整数的 shell 变量。

      if [[ ${OSTYPE:6} -ge 13 ]]; then
          echo "At least 10.9, so feeling fine.";
      else 
          echo "Time to put the old cat to sleep.";
      fi
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-21
        • 2012-02-05
        • 1970-01-01
        • 1970-01-01
        • 2019-09-02
        • 2014-08-07
        相关资源
        最近更新 更多