问题
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?
回答1:
Use system from within awk:
awk '{ system("openssl s_client -connect host:port -cipher " $1) }' ciphers.txt
回答2:
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.
回答3:
The xargs command is specifically for that use case.
awk '{print $0}' <ciphers.txt | xargs -I{} openssl s_client -connect host:port -cipher {} >>results.txt
This version is a bit longer for the example case because awk was already being used to parse out $0. However, xargs comes in handy when you already have a list of things to use and are not running something that can execute a subshell. For example, awk could be used below to execute the mv but xargs is a lot simpler.
ls -1 *.txt | xargs -I{} mv "{}" "{}.$(date '+%y%m%d')"
The above command renames each text file in the current directory to a date-stamped backup. The equivalent in awk requires making a variable out of the results of the date command, passing that into awk, and then constructing and executing the command.
The xargs command can also accumulate multiple parameters onto a single line which is helpful if the input has multiple columns, or when a single record is split into recurring groups in the input file.
For more on all the ways to use it, have a look at "xargs" All-IN-One Tutorial Guide over at UNIX Mantra.
来源:https://stackoverflow.com/questions/20389799/using-output-of-awk-to-run-command