Using output of awk to run command

Viewed 86052

I am brand new to shell scripting and cannot seem to figure out this seemingly simple task. I have a text file (ciphers.txt) with about 250 lines, and I would like to use the first column of each line as an argument in a command. Any help would be greatly appreciated!

the command is:

openssl s_client -connect host:port -cipher argument

It works fine when I do one at a time but I do not really want to run the same command 250+ times. Here is my script so far:

awk '{command = "openssl s_client -connect localhost:4433 -cipher > results.txt"
print $0 | command}' ciphers.txt

I keep getting an error so I am pretty sure I have a syntax error somewhere. Is the output of awk being appended after -cipher?

3 Answers

Use system from within awk:

awk '{ system("openssl s_client -connect host:port -cipher " $1) }' ciphers.txt

there are quite a few things wrong with your command. For one you want to use the first column. That's referred to as $1 in awk and not $0 (which would be the whole line). Second, you forgot a semicolon at the end of your definition of command.

To actually run the command you can either use system() or a pipe (the latter only makes sense if the command can read from stdin, which openssl in your case won't, I think). The easiest would be something like

awk '{cmd="openssl s_client -connect host:port -cipher" $1; system(cmd)}' results.txt

Note, that this will only return the exit status. If you need to capture stdout, you will have to pipe the command through getline.

Andreas

PS: Posting the actual error you got, would have helped.

Related