M HYPE SPLASH
// general

how to pass string comand to shell pipe to execute it (output of awk)

By Michael Henderson

For example this works:

echo 1 // output: 1

This doesn't work:

'echo 1' | "/bin/sh" // output: echo 1: command not found

Why string because i have output of awk and i need to pass the string to execute some command like this:

awk '{ print 'chmod 755 "$0"' | "/bin/sh"}' file.txt

I need to use string like that because i guess it let me put the parameter where i want in the command.

So i first wanted try it execute manually from bash to see if it works before i put it in the awk print command.

3

1 Answer

You are passing a string to the shell - however the string you are passing is 1

To pass echo 1 you need something like

echo 'echo 1' | /bin/sh

or

printf '%s' 'echo 1' | /bin/sh

(outside of awk, you don't need to quote /bin/sh).

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy