eval a string doesn't work

2018-07-27 21:48:21

I try to write a script which will return the pids of jobs from some keywords:

#!/bin/bash

grepcmds=()

for pattern in "$@"

do

grepcmds+=("| grep -e \"$pattern\" ")

done

echo "jobs -l ${grepcmds[@]} | projection -d \" \" - 2"

eval "jobs -l ${grepcmds[@]} | projection -d \" \" - 2"

exit

where projection is another script which select the second field to output.

But the script doesn't output anything,

$ myscript evince pdf

jobs -l | grep -e "evince" | grep -e "pdf" | projection -d " " - 2

although run the command directly will:

$ jobs -l | grep -e "evince" | grep -e "pdf" | projection -d " " - 2

14792

How can I solve the above problem?

Thanks.