我的代码:
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/