Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does process substitution not work in a shell script?

Tags:

bash

shell

I am trying to comm command in shell script but getting an error:

a.sh: command substitution: line 1: syntax error near unexpected token `('
a.sh: command substitution: line 1: `comm -12 <( sort /home/xyz/a.csv1 | uniq) <( sort /home/abc/tempfile | uniq) | wc -l'

snippet of code-

temp=`comm -12 <( sort /home/xyz/a.csv1 | uniq) <( sort /home/abc/tempfile | uniq) | wc -l`
echo $temp
like image 474
champs Avatar asked Jul 12 '15 19:07

champs


People also ask

How does process substitution work?

In computing, process substitution is a form of inter-process communication that allows the input or output of a command to appear as a file. The command is substituted in-line, where a file name would normally occur, by the command shell.

What is command substitution in a shell Why is it important explain with an example?

Substitution is a functionality by following which we can instruct the shell to substitute the actual value of an expression. Example: In the below program we have firstly created variable num and assigned it with the value 20 and then substituted the value of the num as 100 in the echo command.

How does substitution command work?

Command substitution allows the output of a command to replace the command itself. Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted.

What is shell substitution?

Command substitution allows you to capture the output of any command as an argument to another command. When you place a command line within backquotes ( `` ), the shell first runs the command or commands and then replaces the entire expression, including the backquotes, with the output.


1 Answers

It isn't entirely clear yet, but the chances are very high that you either have an incorrect shebang line at the top of the script:

#!/bin/sh

or you are using sh script.sh instead of bash script.sh while testing it, or you have SHELL=/bin/sh or something similar set in the environment. Your failure is on the process substitution code. When Bash is run as sh (in POSIX mode), then process substitution is not available:

  1. Process substitution is not available.

You need to write:

#!/bin/bash

temp=$(comm -12 <(sort -u /home/xyz/a.csv1) <(sort -u /home/abc/tempfile) | wc -l)
echo $temp

or even simply:

#!/bin/bash

comm -12 <(sort -u /home/xyz/a.csv1) <(sort -u /home/abc/tempfile) | wc -l

which will achieve the same effect as the capture followed by the echo. When testing, use bash -x script.sh or bash script.sh.


Deciphering the indecipherable comment

In an indecipherable comment, the information appears to include:

BASH=/bin/sh
BASHOPTS=cmdhist:extquote:force_fignore:hostcomplete:interactive_comments:progco‌mp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=([0]="0")
BASH_SOURCE=([0]="a.sh")
BASH_VERSINFO=([0]="4" [1]="1" [2]="2" [3]="1" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION='4.1.2(1)-release'
CVS_RSH=ssh
SHELL=/bin/bash
SHELLOPTS=braceexpand:hashall:interactive-comments:posix
SHLVL=2

Note that BASH=/bin/sh and SHELLOPTS=braceexpand:hashall:interactive-comments:posix. Either or both of these might be a major part of the problem.

like image 170
Jonathan Leffler Avatar answered Oct 11 '22 12:10

Jonathan Leffler