pipe in command

Hello,

I try to concatenate a command to execute. Sadly it throws an error.

#!/bin/bash
cd /
cmd="find -name *.txt | awk '{ printf "FILE: "$1; system("less "$1);}' | egrep 'FILE:|$1'"
echo "1."
$($cmd)
echo "2."
$("$cmd")
echo "3."
`$cmd`
echo "4."
`"$cmd"`

1.&3. 'find: paths must precede expression: |'
2.&4. 'command not found'

nothing of this works right now. I need some help please...

EDIT: I've found a solution using 'eval'. Why do I need to execute another command to use piping in shell script?

You don't. It is not needed or desirable to put the command into an Environment Variable (like $cmd in your script).
Your whole script should be just:

#!/bin/bash
cd /
find . -type f -name *.txt | awk '{ printf "FILE: "$1; system("less "$1);}' | egrep 'FILE:|$1'

Note that the first parameter in "find" should be the name of a directory or "." (current directory) and in your case you should confine the search to files (-type f).
There are probable bugs in the command line. The "egrep" in particular.
Not clear what the script is meant to do but searching down from root is not usually a good idea because you will find directories and files which you cannot read.

1 Like

I started programming with C and was searching for defined functions in header files (*.h) on this system here.
I also now got another help related using parameter '-exec grep -i word' with 'find' to search for content in files.

1 Like