简体   繁体   English

使用awk的输出来运行命令

[英]Using output of awk to run command

I am brand new to shell scripting and cannot seem to figure out this seemingly simple task. 我是shell脚本的新手,似乎无法弄清楚这个看似简单的任务。 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. 我有一个大约250行的文本文件(ciphers.txt),我想使用每行的第一列作为命令中的参数。 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. 当我一次做一个时它工作正常,但我真的不想运行相同的命令250次以上。 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? awk的输出是否在-cipher后附加?

使用awk中的system

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). 这在awk中被称为$ 1而不是$ 0(这将是整行)。 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). 要实际运行该命令,您可以使用system()或管道(后者只有在命令可以从stdin读取时才有意义,我认为在您的情况下不会打开openssl)。 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. 如果需要捕获stdout,则必须通过getline管道命令。

Andreas 安德烈亚斯

PS: Posting the actual error you got, would have helped. PS:发布你得到的实际错误,会有所帮助。

The xargs command is specifically for that use case. xargs命令专门针对该用例。

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 . 这个版本对于示例案例来说有点长,因为awk已经被用来解析$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. 但是,当你已经有一个要使用的东西列表并且没有运行可以执行子shell的东西时, xargs会派上用场。 For example, awk could be used below to execute the mv but xargs is a lot simpler. 例如,下面可以使用awk来执行mvxargs更简单。

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. awk的等价物需要从date命令的结果中创建一个变量,将其传递给awk ,然后构造并执行该命令。

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. xargs命令还可以将多个参数累积到一行上,如果输入有多列,或者在输入文件中将单个记录拆分为重复组,这将非常有用。

For more on all the ways to use it, have a look at "xargs" All-IN-One Tutorial Guide over at UNIX Mantra. 有关使用它的所有方法的更多信息,请查看UNIX Mantra上的“xargs”All-IN-One教程指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM