有人可以就此提出建议吗?
我想从运行的 mysql 查询中获取输出文件:
$code $IP
123456 192.168.26.176
10051 192.168.20.80
234567 192.168.26.178
并在命令中运行它:
rsync -rvp *.$code.extension root@$IP:/path/of/dest
我正在尝试这个:
while read -r line ; do echo
"$SOURCE_TRANSMIT_DIR"*."$code".class.json
"$DEST_HOST"@"$IP":"$DEST_TRANSMIT_DIR" ; done
我得到的输出是这样的:
/opt/file/outgoing/*.123456
10051
234567.class.json [email protected]
192.168.20.80
192.168.26.178:/opt/file/incoming/
我希望它在单独的 rsync 命令中像这样读取:
rsync -rvp *.123456.extension [email protected]:/path/of/dest
rsync -rvp *.234567.extension [email protected]:/path/of/dest
rsync -rvp *.345678.extension [email protected]:/path/of/dest
希望这可以更好地解释,对于糟糕的解释感到抱歉。
答案1
我看不到你的 mysql 查询的结果,但你可以执行它并用 awk 解析以打印你想要的内容(请参阅 mysql 选项以避免打印元组和标题 -raw 或类似的内容)
mysql -printingoptions "your query" |awk '{print "rsync -rvp *."$1".extension root@"$2":/path/of/dest"}'
您可以将其通过管道传输到 sh 或 bash,然后(命令 |sh)来执行 rsync :)
对我来说似乎是最简单的方法。
答案2
如果您通过以下命令运行 mysql-query 的输出,它将为您提供所需的输出。您可以使用此输出进行评估,例如将其传输到文件中并将该文件作为 shell 脚本执行。
mysql | grep -v '\$' | while read line; do echo "${line}" | sed 's#\(^[0-9]*\)[\ ]*\([0-9\.]*\)#rsync -rvp *.\1.extension root@\2:/path/of/dest#g'; done
以下是该命令的详细信息:
mysql # Your command with output (this is of course longer than this)
grep -v '\$' # Exclude the header
while read line; do # Start a while-loop
echo "${line}" # Echo the contents of the current line that we've read to the next command
# Regular expression to filter the number and the IP and add these to the command, they are held by the variables \1 \2.
sed 's#\(^[0-9]*\)[\ ]*\([0-9\.]*\)#rsync -rvp *.\1.extension root@\2:/path/of/dest#g'
done; # Close the while loop
正则表达式的内容解释如下:
(^[0-9]*\) # From the beginning (^), zero or more (*) numerical [0-9] character
[\ ]* # Zero or more (*) spaces. These are not in brackets, so they won't be matched into a variable.
([0-9\.]* # Zero or more (*) numerical characters or points [0-9\.]