スクリプトの推定 - 変数とともにファイル名を送信する

スクリプトの推定 - 変数とともにファイル名を送信する

SFTPサーバーから最新のファイルをコピーするスクリプトを作成したいと思います。そのために名前を取り、ファイルに入れるスクリプトを作成しました。

別のスクリプトを作成して名前の付いた最新のファイルをダウンロードしようとしていますが、ファイルの内容を変数に渡してからファイルをダウンロードするのが困難です。

#!/bin/bash

set name "testnamefromfile"

expect << 'EOS'
 spawn sftp -P 2222 [email protected]:/var/log/datarecord
 expect "[email protected]'s password:"
 send "admin\n"
 expect "sftp>"
 send "get $name\r"
 expect "sftp>"
 send "bye\n"
EOS

これは私のスクリプトです。ファイルの内容を$name変数に割り当てる方法は?ファイルをダウンロードするには?

このスクリプトを実行すると、次のエラーが発生します。

sftp> can't read "name": no such variable
    while executing
"send "get $name\r""

ベストアンサー1

あなたのスクリプトに

   set name "testnamefromfile"

ただし、これは変数を$1に設定nameし、変数を$2に設定しますtestnamefromfile

それではそこにいます。

expect << 'EOS'

引用したように、'EOS'「heredoc」では変数置換は発生しません。

以下はうまく機能します(テストされていない、パスワードを送信するときに\ nを\ rに変更します)。

#!/bin/sh

name="testnamefromfile"

expect << EOS
 spawn sftp -P 2222 [email protected]:/var/log/datarecord
 expect "[email protected]'s password:"
 send "admin\r"
 expect "sftp>"
 send "get $name\r"
 expect "sftp>"
 send "bye\n"
EOS

おすすめ記事