-1

私のコードは次のとおりです。

nb_lignes=`wc -l $1 | cut -d " " -f1`
for i in $(seq $(($nb_lignes - 1)) )
do
machine=`head $1 -n $i | tail -1`
machine1=`head $1 -n $nb_lignes | tail -1`
ssh root@$machine -x " scp /home/file.txt root@$machine1:/home && rm -r /home/file.txt"
done

$ machine1は変数または文字列として使用されますか?文字列の場合、引用符を追加して変更するにはどうすればよいですか?

4

3 に答える 3

2

$machinehead $1 -n $i | tail -1結果に拡大し、結果に$machine1拡大しhead $1 -n $nb_lignes | tail -1ます。

あなたは自分でそれを理解することができます。

ところで、ssh root@…</ p>

于 2012-10-22T13:55:30.100 に答える
1

$machine1machine1二重引用符を使用しているため、変数の値を指定するために展開されます"。一重引用符を使用した場合'、それは拡張されませんでした。

考えられる混乱の1つは、他のテキスト内に変数を埋め込む場合です。この場合、末尾の文字は:root@$machine1:/home)であり、Bash変数名の有効な文字ではないため、問題ありません。一部のシェル(csh)はそれを好まなかったでしょう。よくわからない場合は{ }、たとえば次のように変数名を区切ることができます。

root@${machine1}:/home
于 2012-10-22T14:06:18.177 に答える
0

書き直された答え

ssh rootそこでの虐待に関係なく...(私curlはこの目的のために使用することを好みますが、あなたはあなた自身を書かなければなりませんcollectFiles.php;)

わかりました。これの目的は、ホストのリストの最後のdestination行を、リストの残りの部分からファイルを送信する場所として取得することです。あなたは出来る:

最初のPosixシェル:

でテスト済み

tac $1  | (
    srcFile=/home/file.txt i=1
    read destHost
    while read collectHost ;do
        destFile=`printf "root@%s:/home/fileHost_%-12s_%03d.txt" \
            $destHost $collectHost $i`
        i=$((i+1))
        echo ssh $collectHost -x "curl -H 'Filename: $destFile' \
            --data-binary '@$srcFile http:/$destHost/collectFiles.php && \
                rm $srcFile"
        done
)

echoドロップされます)

変数を操作すると、コマンドを作成するための優れた方法が得られます。完全に使用可能なサンプルがあります

#!/bin/bash

mapfile flist <$1

dstCmdFmt="curl -H 'Filename: %s' --data-binary '@%s' http://%s/%s && rm %s"
dstRcvPhp=collectfiles.php
srcFile=/home/file.txt

for ((i=0;i<${#flist[@]}-1;i++));do
    printf -v destFile "fileHost_%-12s_%03d.txt" ${flist[i]} $[1+i]
    printf -v cmd "$dstCmdFmt" \
        ${destFile// /_} $srcFile ${flist[@]:${#flist[@]}-1} $dstRcvPhp $srcFile
    echo ssh ${flist[i]} -x "$cmd"
done

ファイルで試すには:

cat <<eof > testfile
machineA
OtherMachine
AnotherHost
DestinationHost
eof

./script.sh testfile
ssh machineA -x curl -H 'Filename: fileHost_machineA_____001.txt' --data-binary '@/home/file.txt' http://DestinationHost/collectfiles.php && rm /home/file.txt
ssh OtherMachine -x curl -H 'Filename: fileHost_OtherMachine_002.txt' --data-binary '@/home/file.txt' http://DestinationHost/collectfiles.php && rm /home/file.txt
ssh AnotherHost -x curl -H 'Filename: fileHost_AnotherHost__003.txt' --data-binary '@/home/file.txt' http://DestinationHost/collectfiles.php && rm /home/file.txt

古い答え

それ以外の...

nb_lignes=`wc -l <$1`
machine1=`sed -ne ${nb_lignes}p <$1`
for i in `seq $(($nb_lignes - 1))` ;do
    machine=`sed -ne ${i}p <$1`
    ssh  root@$machine -x " scp /home/file.txt root@$machine1:/home && rm -r /home/file.txt"
  done

だが...

それぞれから、同じ宛先ディレクトリ内の同じ一意のファイルmachineに異なるfile.txt(ただし同じ名前で)送信machineする場合は、毎回前に送信したファイルを上書きします。

于 2012-10-24T23:49:13.050 に答える