bash - 在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"
顺便说一句,$[...]
它是一种非常古老且非常过时的算术扩展形式。改为使用$((...))
。
推荐阅读
- javascript - 使用它来保存函数中的变量?
- c# - 调用函数列表。如何获取它们的返回值并使用可能是已处置闭包的一部分的参数?
- java - 为什么 system.out.println() 让我的 Eclipse 没有响应
- c++ - 我试图想出一个程序来将用户输入(字符串)存储在一个向量中,并按长度降序输出它们
- r - lm() 的响应优化
- python - 如何使用 Django Modelseirallizer 更新实例的粒子属性?
- java - 是否有引入 Java 数据类的预期日期?
- mysql - 如果另一个表B在mysql中有行,则选择表A中的所有行
- python - 在 Spyder 中使用 OpenPyxl 和 sqlite3 将数据从 Excel 文件导入数据库
- python - np.logical_or 与 reduce 返回不同的结果