【问题标题】:Should this be done with bash script or automator or applescript?这应该用 bash 脚本、automator 或 applescript 来完成吗?
【发布时间】:2013-09-30 07:30:00
【问题描述】:

我有一个 bash 命令,其中包含一个文件变量,该文件更新特定硬件的固件并给它一个序列号。

#!/bin/bash
fpath=$(dirname "$0")

ee_image=mlr-2000119.bin

sudo nvram tbt-options=4

sudo /usr/sbin/bless -mount / -firmware "$fpath/ThorUtilDevice.efi" -payload "$fpath/$ee_image" -options "-o -ej 1 -blast efi-apple-payload0-data" 

sudo reboot now

我想通过 automator 或 applescript 创建一个文件,该文件将创建相同的文件,但会自动将 ee_image bin 文件名加一。这样最终用户不必总是在文本编辑中打开命令文件,手动进行更改然后保存然后执行文件..

这方面的任何帮助都是上帝派来的。

【问题讨论】:

    标签: bash applescript automator


    【解决方案1】:

    脚本中的最后一行 sudo reboot now 会使任何类型的循环变得毫无意义。

    但是,如果你坚持,可以使用循环:

    #!/bin/bash
    fpath=$(dirname "$0")
    
    for i in {2000119..3000119}; do
      ee_image=mlr-${i}.bin
      sudo nvram tbt-options=4
      sudo /usr/sbin/bless -mount / -firmware "$fpath/ThorUtilDevice.efi" -payload "$fpath/$ee_image" -options "-o -ej 1 -blast efi-apple-payload0-data" 
      sudo reboot now
    done
    

    这将循环通过mlr-2000119.binmlr-3000119。您还可以考虑将参数传递给脚本,在这种情况下,您可以使用带有 ee_image 行的原始脚本

      ee_image=mlr-$1.bin
    

    并调用bash /path/to/your/script.sh 2000119

    【讨论】:

      【解决方案2】:

      @devnull 写道:

      脚本中的最后一行 sudo reboot now 会使任何类型的循环变得毫无意义。

      我相信reboot 命令和其他命令一样。它应该被回显到一个文件中,而不是运行来为最终用户生成脚本。

      我认为创建脚本的脚本是一个好主意。

      这类似于有多少网站工作。服务器上的脚本可以回显 HTML、CSS 和 JavaScript 代码以供 Web 浏览器使用。

      这是一个例子:

      #!/bin/bash
      
      # set the path to the dir
      dir=$(dirname $0)"/"
      
      # set the path to the file that keeps track of the serial numbers
      snFile="$dir""sn.txt"
      
      # set the file name of the file to be generated
      fileName="serialize"
      
      # read the last serial number used
      if [ -e "$snFile" ];
      then
          read lastSN < "$snFile"
      else
          lastSN="0"
      fi
      
      # increment the serial number
      let "nextSN = $lastSN + 1"
      echo "$nextSN" > "$snFile"
      
      # create a path variable for the file being created.
      generatedPath="$dir$fileName$nextSN.sh"
      
      # generate the script
      echo "#!/bin/bash" > "$generatedPath"
      echo 'fpath=$(dirname "$0")' >> "$generatedPath"
      echo '' >> "$generatedPath"
      echo "ee_image=mlr-$nextSN.bin" >> "$generatedPath"
      echo '' >> "$generatedPath"
      echo 'sudo nvram tbt-options=4' >> "$generatedPath"
      echo '' >> "$generatedPath"
      echo 'sudo /usr/sbin/bless -mount / -firmware \"$fpath/ThorUtilDevice.efi\" -payload \"$fpath/$ee_image\" -options \"-o -ej 1 -blast efi-apple-payload0-data\" \' >> "$generatedPath"
      echo '' >> "$generatedPath"
      echo 'sudo reboot now' >> "$generatedPath"
      
      # give the user some feedback
      echo "generatedPath: $generatedPath"
      

      如果让您的最终用户运行 bash 脚本就足够了,那么我认为您几乎完成了。

      如果您想拥有更好的用户界面并拥有一个供最终用户运行的 Mac 应用程序,请给我发送电子邮件,我可以为您提供帮助。

      kaydell@learnbymac.com

      【讨论】:

        猜你喜欢
        • 2013-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-06
        • 2014-03-25
        • 2020-12-11
        • 2014-09-29
        相关资源
        最近更新 更多