Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error in shell script with process substitution

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.

What I get from shell upon execution is:

/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)'

like image 743
Milad Naseri Avatar asked Aug 25 '12 08:08

Milad Naseri


People also ask

What does $$ mean in shell script?

The $? variable holds the exit status of a command, a function, or of the script itself. $$ process ID variable. The $$ variable holds the process ID [4] of the script in which it appears.

What does bad substitution mean bash?

It's a syntax error that occurs when you execute your Bash script and it can be caused by different reasons. Two common causes for this error are the incorrect use of command substitution and incorrect characters added to the lines of your script (for example extra dollar signs or white spaces).

What is substitution in bash?

Command substitution is a mechanism that is followed by programmers in a bash script. In this mechanism, the output of a command replaces the command itself. Bash operates the expansion by executing a command and then replacing the command substitution with the standard output of the command.


1 Answers

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.)

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.

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.

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

like image 111
Gilles 'SO- stop being evil' Avatar answered Oct 02 '22 19:10

Gilles 'SO- stop being evil'