具有长命令的Bash脚本作为连接字符串 [英] Bash script with long command as a concatenated string

查看:55
本文介绍了具有长命令的Bash脚本作为连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是bash脚本示例:

Here is a sample bash script:

#!/bin/bash

array[0]="google.com"
array[1]="yahoo.com"
array[2]="bing.com"

pasteCommand="/usr/bin/paste -d'|'"

for val in "${array[@]}"; do 
    pasteCommand="${pasteCommand} <(echo \$(/usr/bin/dig -t A +short $val)) "
done

output=`$pasteCommand`

echo "$output"

以某种方式显示错误:

/usr/bin/paste: invalid option -- 't'
Try '/usr/bin/paste --help' for more information.

我该如何修复它以使其正常工作?

How can I fix it so that it works fine?

//

预期的输出是从3个dig执行中以|分隔的字符串获得结果.特点.主要是我以这种方式使用粘贴,因为它允许并行运行3个dig命令,并且我可以使用定界符分隔输出,这样我就可以轻松解析它,并且仍然知道dig输出到哪个域(例如google.com以获得第一个结果)).

Expected output is to get result from the 3 dig executions in a string delimited with | character. Mainly I am using paste that way because it allows to run the 3 dig commands in parallel and I can separate output using a delimiter so then I can easily parse it and still know the dig output to which domain (e.g google.com for first result) is assigned.

推荐答案

当您在问题中指定输入和期望的输出,然后指定您的尝试以及为什么不起作用时,问一个问题会更容易.

It's easier to ask a question when you specify input and desired output in your question, then specify your try and why doesn't it work.

我想要的是 https://i.postimg.cc/13dsXvg7/required.png

$ array=("google.com" "yahoo.com" "bing.com")
$ printf "%s\n" "${array[@]}" | xargs -n1 sh -c '/usr/bin/dig -t A +short "$1" | paste -sd" "' _ | paste -sd '|'
172.217.16.14|72.30.35.9 98.138.219.231 98.137.246.7 98.137.246.8 72.30.35.10 98.138.219.232|204.79.197.200 13.107.21.200

这篇关于具有长命令的Bash脚本作为连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆