在Mac中的命令bash文件中使用变量

问题描述

我创建了一个bash脚本,用于在Mac OS中显示通知5秒。

#!/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变量?

解决方法

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

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...