bash - 使用 read() 或 run() 运行 bash 命令时,我应该如何转义 '&&' 和其他特殊字符?

标签 bash julia

我的代码:

df = read(`df -h|grep /dev/sda1 && df -h|grep pCloud`, String)
当我运行它时,我从 Julia 收到以下消息:
Warning: special characters "#{}()[]<>|&*?~;" should now be quoted in commands
 │   caller = #shell_parse#333(::String, ::Function, ::String, ::Bool) at shell.jl:100
 └ @ Base ./shell.jl:100
df: invalid option -- '|'
Try 'df --help' for more information.
ERROR: LoadError: failed
process: Process(`df '-h|grep' /dev/sda1 '&&' df '-h|grep' pCloud`, ProcessExited(1)) [1]
我发现有人有类似的problem ,
但他们似乎已经解决了这个问题而没有逃避。

最佳答案

Julia 命令不在 shell 中运行,因此无法使用类似的 shell 功能。如果你想从一个命令到另一个命令,你应该使用 pipeline函数,如果您想测试命令或管道的成功或失败,请使用 success 运行它功能。在这种情况下,您可以这样做:

success(pipeline(`df -h`, `grep /dev/sda1`)) &&
success(pipeline(`df -h`, `grep pCloud `))

当然,在这种情况下,您可以调用 df -h一次做
df = read(`df -h`, String)
contains(df, "/dev/sda1") && contains(df, "pCloud")

关于bash - 使用 read() 或 run() 运行 bash 命令时,我应该如何转义 '&&' 和其他特殊字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62312599/

相关文章:

Julia 中长度有限的排列

multithreading - 多线程 Julia 显示枚举迭代器错误

php - 从 PHP exec() 函数读取 git push 的输出

linux - Git 初始推送错误文件描述符错误

linux - Bash 转义字符

julia - 如何平衡计数映射表中的数据集

php - 从 Windows 中在 PHP 中创建 UNIX 安全测试

windows - 在 Windows 上更改 Git 提示符

julia - replace() 对大字符串失败

julia - 通过自动转换为 Modia 对大规模 Modelica 模型进行有效模拟