Using output of awk to run command

前端 未结 3 1692
终归单人心
终归单人心 2021-02-05 02:39

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 fi

相关标签:
3条回答
  • 2021-02-05 03:18

    Use system from within awk:

    awk '{ system("openssl s_client -connect host:port -cipher " $1) }' ciphers.txt
    
    0 讨论(0)
  • 2021-02-05 03:24

    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.

    0 讨论(0)
  • 2021-02-05 03:35

    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.

    0 讨论(0)
提交回复
热议问题