0

I have a simple bash script that looks like this, which adds two "printer" processes that print words in a loop to an array and executes them, storing their process IDs. I am trying to user process substitution to filter out a piece of text that gets printed, but I keep getting the error grep: <(./usr/bin/printscript2.sh): No such file or directory

The scripts for the printer processes are similar but printscript1 just has some different words, so I'll add the code for printscript2 below the parent process.

#!/bin/bash

# Setup array for the commands we wish to execute
procs_arr=()
procs_arr+=("/usr/bin/printscript1.sh")
procs_arr+=("grep -v FILTERMEPLEASEENDTHISMISERY <(/usr/bin/printscript2.sh)")

num_procs=${#procs_arr[@]}
echo "num_procs = $num_procs"


# Run the commands and store pids to an array
pids=()
for (( i=0; i<"$num_procs"; i++ )); do
    echo "cmd = ${procs_arr[$i]}"
    ${procs_arr[$i]} &
    pids+=("$!")
    echo "    pid = ${pids[$i]}"
done

while true; do
  sleep 1000
done

exit 0

!/bin/bash

sleep 3
while true; do
  sleep 5
  echo "parrot"
  sleep 5
  echo "FILTERMEPLEASEENDTHISMISERY"
  sleep 5
  echo "socks"
done

I executed the same command using process substitution locally in the shell (i.e. ran "grep -v FILTERMEPLEASEENDTHISMISERY <(/usr/bin/printscript2.sh)" in bash directly) and it worked as intended, meaning the string was filtered out from the console prints. I'm not sure if there's some special formatting I'm missing here. I also tried to use escape characters so putting a backslash '' in front of '<', '(', and ')' and got the same error grep: \<\(/usr/bin/printscript2.sh\): No such file or directory. Is this a function that is only supposed to be supported when directly done in a bash shell, or am I typing something incorrectly?

  • Code does not belong in variables. See [BashFAQ #50](https://mywiki.wooledge.org/BashFAQ/050). Arrays are the appropriate tool for storing command lines, but `<()` is not a command-line element, it's syntax; use a function instead. – Charles Duffy Aug 24 '23 at 20:40
  • Bash syntax is not processed after expanding variables. You would have to use `eval "${procs_arr[$i]}"` to do what you want. – Barmar Aug 24 '23 at 20:41
  • (Also, the process substitution _creates an extra process_, so you use `eval` to force it to work, your nprocs count is too low... and of course you have [all the risks of `eval`](https://mywiki.wooledge.org/BashFAQ/048)). – Charles Duffy Aug 24 '23 at 20:42
  • 1
    ...one approach to consider: Define a function with `myproc() { grep -v FILTERMEPLEASEENDTHISMISERY <(/usr/bin/printscript2.sh); }`, and then use `procs_arr+=( "myproc" )` – Charles Duffy Aug 24 '23 at 20:43
  • ...actually, that you're attempting to run `${procs_arr[$i]}` makes BashFAQ #50 **exactly** on-point. – Charles Duffy Aug 24 '23 at 20:46

0 Answers0