こんにちは、私はいくつかの行を含むfile.txtをbashスクリプト引数に渡してコマンドで実行する方法を見つけようとしました。 whileループを実行する必要があるかどうかわかりませんか?
したがって、テキストファイルにはaboutなどのコンテンツのみが含まれます。
ip_addr1,foo:bar
ip_addr2,foo2:bar2
user@ip_addr3,foo3:bar3
私はbashスクリプトがそのファイルからコンテンツを取得し、bashスクリプトとして使用したいと思います。
ssh ip_addr1 'echo "foo:bar" > /root/text.txt'
ssh ip_addr2 'echo "foo2:bar2" > /root/text.txt'
ssh user@ip_addr3 'echo "foo3:bar3" > /root/text.txt'
したがって、テキストファイルの行数に応じてスクリプトが実行されます。
ベストアンサー1
read
答えが示唆したように、bashコマンドを使用してファイル行を繰り返すことができます。この問題。
while read -r line
do
# $line will be a variable which contains one line of the input file
done < your_file.txt
答えが示すように、read
変数を再利用してIFS
各行の内容を変数に分割できます。IFS
この問題。
while read -r line
do
# $line will be a variable which contains one line of the input file
IFS=, read -r ip_addr data <<< "$line"
# now, $ip_addr stores the stuff to the left of the comma, and $data stores the stuff to the right
done < your_file.txt
ここでは、新しい変数を使用して実行したいコマンドを実行できます。
while read -r line
do
# $line will be a variable which contains one line of the input file
IFS=, read -r ip_addr data <<< "$line"
# now, $ip_addr stores the stuff to the left of the comma, and $data stores the stuff to the right
ssh "$ip_addr" "echo \"${data}\" > /root/text.txt"
done < your_file.txt
変数が必要ない場合は、$line
単一のread
コマンドを使用できます。
while IFS=, read -r ip_addr data
do
# now, $ip_addr stores the stuff to the left of the comma, and $data stores the stuff to the right
ssh "$ip_addr" "echo \"${data}\" > /root/text.txt"
done < your_file.txt