bash - bash字符串变量扩展中的空格字符未读取并在case语句中执行命令
问题描述
我正在使用switchaudio-osx构建 Alfred 工作流程,因此我可以将系统音频输入/输出更改为特定源。编写 shell 脚本很新,我想到了这个:
#!/bin/bash
#Command path
cmd="/usr/local/Cellar/switchaudio-osx/1.0.0/SwitchAudioSource"
#Available sources
source1="Built-in Output"
source2="USB Audio CODEC "
source3="Built-in Microphone"
#Switch output source to
systemOut="$cmd -t output -s $source1"
usbOut="$cmd -t output -s $source2"
#Switch system sound source to
systemSys="$cmd -t system -s $source1"
usbSys="$cmd -t system -s $source2"
#Switch input source to
usbIn="$cmd -t input -s $source2"
systemIn="$cmd -t input -s $source3"
case "{query}" in
"systemOut")
$systemOut
$systemSys
;;
"usbOut")
$usbOut
$usbSys
;;
"systemIn")
$systemIn
;;
"usbIn")
$usbIn
;;
*)
echo "No source detected"
;;
esac
运行后,脚本返回错误信息:
Could not find an audio device named "Built-in" of type output. Nothing was changed.
Could not find an audio device named "Built-in" of type system. Nothing was changed.
所以不知何故$source1
没有完整阅读。通过搜索,我了解了 IFS 设置和使用{}
字符串变量,所以我尝试了这些。但是错误消息仍然存在。
当我更改$source1="Built-in Output"
为 时"'Built-in Output'"
,错误消息返回:
Could not find an audio device named "'Built-in" of type output. Nothing was changed.
当我将 case 语句命令更改$systemOut
为${systemOut}
or"$systemOut"
时,两者都返回错误消息:
/usr/local/Cellar/switchaudio-osx/1.0.0/SwitchAudioSource -t output -s 'Built-in Output': No such file or directory
/bin/bash: line 25: /usr/local/Cellar/switchaudio-osx/1.0.0/SwitchAudioSource -t system -s 'Built-in Output': No such file or directory
这个错误信息是什么意思?看起来包含 case 语句中命令的字符串变量没有以正确的方式执行。
我还尝试使用$()
并``
包含具有我的命令的字符串变量,但得到相同类型的错误消息。
在case语句或bash脚本中嵌入shell命令的语法是什么?它不是一个以命令为值的字符串吗?超级新手问题。对不起。
解决方案
推荐阅读
- google-cloud-functions - 存储中的连接问题触发 GCF
- ios - 当接收者应用程序使用 XMPP iOS 处于终止模式时,如何获取消息传递回执?
- javascript - 加载时如何将js文件注入html页面?
- reactjs - DRF & React:如何通过单击“使用 google 登录”将社交应用程序令牌添加到数据库
- grafana - 在 Grafana 上将表格转换为图形或时间序列
- html - Vuetify v-data-table 具有固定的页眉和页脚以及可滚动的正文
- react-native - 无法读取 react-native-image-picker 上未定义的属性“showimagepicker”
- java - 根据查找表添加列表对象中不存在的值
- angular - PrimeNg GlobalFilter :在第一列中找到匹配项后停止搜索(不在后续列中查找值)
- java - 在 PathParam 中使用 PathSegment 测试 POST 请求时断言失败