rsyncから出力ファイルと変数を読み取る

rsyncから出力ファイルと変数を読み取る

誰でもこれについてアドバイスできますか?

私が実行した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にパイプしてから(command | sh)rsyncを実行できます。 :)

私にとっては最も簡単な方法のようです。

答え2

次のコマンドでmysql-queryの出力を実行すると、希望の出力が得られます。この出力をファイルに転送し、ファイルをシェルスクリプトとして実行するなどの評価に使用できます。

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\.]

関連情報