Command passed as shell script argument

I want to pass a command to a shell script. This command is a grep command. At runtime, I get the following errors, please help:

myscript.sh "egrep 'ERROR|FATAL' \*20100428\*.log | grep -v aString" 

myscript.sh is a simple script:

 #!/bin/ksh cd log $1 

errors:

 egrep: can't open | egrep: can't open grep egrep: can't open -v egrep: can't open aString 

The error is that egrap sees |, grep, -v and aString as arguments.

+4
source share
2 answers

try the following:

 eval $1 
+3
source

You can call sh -c $1 to call the first argument as commands in the new shell so that the special characters of the shell are extended.

+2
source

Source: https://habr.com/ru/post/1308284/


All Articles