【问题标题】:use variable in command bash file in mac [closed]在mac的命令bash文件中使用变量[关闭]
【发布时间】:2021-01-13 07:17:05
【问题描述】:

我为 mac OS 中的 5 秒显示通知创建了一个 bash 脚本。

#!/bin/bash

arr[0]="0"
arr[1]="1"
arr[2]="2"
arr[3]="3"
arr[4]="4"
arr[5]="5"
arr[6]="6"

while true; do
 
    rand=$[$RANDOM % ${#arr[@]}]
    text=${arr[$rand]}
    
    osascript -e 'display notification '"$text"' with title "hello"'

    sleep 5
done

但显示带有标题 hello 和描述 $text 的通知。 如何在这个命令中使用 $text 变量?

【问题讨论】:

  • 当你用osascript 包围你的osascript 行时你看到了什么?祝你好运。
  • @RasoulMiri : 如果不涉及 zsh,为什么要标记 zsh

标签: bash macos sh zsh oh-my-zsh


【解决方案1】:

"$text" 的值作为参数传递给静态脚本。

osascript -e 'on run argv' \
          -e 'display notification (item 1 of argv) with title "hello"' \
          -e 'end run' "$text"

osascript -e 'on run argv
display notification (item 1 of argv) with title "hello"
end run' "$text"

顺便说一句,$[...] 是一种非常古老且非常过时的算术扩展形式。请改用$((...))

【讨论】:

    猜你喜欢
    • 2023-04-08
    • 2012-03-02
    • 2014-06-27
    • 2019-06-22
    • 1970-01-01
    • 2015-06-23
    • 2019-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多