带有进程替换的 shell 脚本中的语法错误 [英] Syntax error in shell script with process substitution

查看:39
本文介绍了带有进程替换的 shell 脚本中的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 shell 脚本,我用它来备份我的系统.有一行:

I have this shell script which I use to back up my system. There is a line:

tar -Pzcpf /backups/backup.tar.gz --directory=/ --exclude=proc --exclude=sys --exclude=dev/pts --exclude=backups --exclude=var/log / 2> >(grep -v 'socket ignored' >&2)

如您所见,我一直在尝试使用 这篇博文.

As you can see, I have been trying to filter out the annoying, useless "socket ignored" error by tar, using this blog post.

我在执行时从 shell 得到的是:

What I get from shell upon execution is:

/bin/sysback:第 45 行:意外标记附近的语法错误 >'/bin/sysback:第 45 行:tar -Pzcpf/backups/backup --directory=/--exclude=proc --exclude=sys --exclude=dev/pts --exclude=backups --exclude=var/log/2> >(grep -v '忽略套接字'>&2)'

/bin/sysback: line 45: syntax error near unexpected token >' /bin/sysback: line 45:tar -Pzcpf /backups/backup --directory=/ --exclude=proc --exclude=sys --exclude=dev/pts --exclude=backups --exclude=var/log / 2> >(grep -v 'socket ignored' >&2)'

推荐答案

您使用的语法是基本 shell 语法的 bash 扩展,因此您必须小心使用 bash 运行您的脚本.(Ksh 也有 >(…) 进程替换,但在重定向后不支持它.Zsh 会很好.)

The syntax you've used is a bash extension to the basic shell syntax, so you must take care to run your script with bash. (Ksh also has >(…) process substitution but doesn't support it after a redirection. Zsh would be fine.)

鉴于您收到的错误消息,您正在 bash 中运行此脚本,但在其 POSIX 兼容模式下,而不是在完整的 bash 模式下.注意使用显式 #!/bin/bash 行调用脚本.#!/bin/sh 不会,即使 /bin/sh 是 bash 的符号链接,因为如果在名称下调用 bash,则 bash 在 POSIX 模式下运行sh.如果您使用 bash 功能,请始终按名称调用 bash.

Given the error message you're getting, you are running this script in bash, but in its POSIX compatibility mode, not in full bash mode. Take care to invoke your script with an explicit #!/bin/bash line. #!/bin/sh won't do, even if /bin/sh is a symbolic link to bash, because bash runs in POSIX mode if it's invoked under the name sh. Always invoke bash by name if you use bash features.

还要注意不要设置环境变量POSIXLY_CORRECT,或者如果你想使用bash特性,在命令行上传递--posix选项.

Also take care not to set the environment variable POSIXLY_CORRECT or to pass the --posix option on the command line if you want to use bash features.

或者,不要使用这种特定于 bash 的语法;使用可移植的结构,例如 Stephane Rouberol 提出的结构.

Alternatively, don't use this bash-specific syntax; use a portable construct such as the one proposed by Stephane Rouberol.

这篇关于带有进程替换的 shell 脚本中的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆