gpt4 book ai didi

linux - bash中带空格的回调脚本

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:01:12 24 4
gpt4 key购买 nike

我写了一个小脚本来检查一些东西,如果测试成功,我希望从脚本中执行一个命令。我不想对命令进行硬编码,而是像回调脚本一样将其作为参数提供给它。

我测试的命令是 /usr/bin/xmessage -buttons "button a","button b"some text to test。在终端中独立运行它可以正常工作,最后一个文本不需要引号。

脚本如下所示:

#!/bin/bash
echo "$1"
$1

但是当运行 /path/to/script.bash '/usr/bin/xmessage -buttons "button a","button b"some text to test' 它看起来像 this ,尽管 echo 看起来是正确的。

当使用 "$1" 而不是 $1 时,它会提示找不到文件。任何人都知道如何修复空格的行为吗?

最佳答案

我建议你这样写脚本:

#!/bin/bash
echo ${1+"$@"}
${1+"$@"}

然后这样调用它:

/path/to/script.bash /usr/bin/xmessage -buttons "button a","button b" some text to test

关于linux - bash中带空格的回调脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9365874/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com