ファイル名文字列を配列に分割

ファイル名文字列を配列に分割

特定のディレクトリ(/ myfiles)にあるすべてのファイルのリストを取得し、sftpを介して別のサーバーに転送しようとしています。ファイルの数と名前もさまざまです。ファイル形式は次のとおりです。 CODE CONVERSION TABLE_C_PRIOR RELATIONSHIP TYPES CODE CONVERSION TABLE_E_TRANSACTION TYPES

私はそれらをすべて配列に入れてから、sftp putコマンドを使用してwhileループ(数に応じて)を実行しようとしています。ただし、この文字列を配列にインポートすることはできません。

これが私が持っているものです:

export directory=`find -name *Table_\*`
IFS="./"
read -a filearry <<< "${directory}"

テスト中は ${filearry[0]} だけが入力されます。 echo ${filearry[0]} の出力は次のとおりです。

Code Translation Table_c_Primary Relationship Type
Code Translation Table_e_Transaction Type
Code Translation Table_f_Appeal Code
Code Translation Table_g_Campaign Codes
Code Translation Table_h_Designation Code
Code Translation Table_i_Designation Purpose
Code Translation Table_j_Address Types
Code Translation Table_k_Degree of Graduation
Code Translation Table_l_Relationship Type
Code Translation Table_m_Activity Role
Code Translation Table_n_Activity Status Club
Code Translation Table_o_Participation Category
Code Translation Table_p_Activity Status Organization
Code Translation Table_q_Restriciton Code
Code Translation Table_c_Primary Relationship Type

編集:これはcronによって開始された自動化スクリプトでなければなりません。必ずしもsftpを使用する必要はありませんが、何を使用してもセキュリティを維持する必要があります。

結局、名前に関係なく、ディレクトリ内のすべてのファイルをアップロードするようにスクリプトを簡素化しました。私は私のオフィスの複数の人にテキストメールで送信されるログファイルに出力を送信します。このメールの形式が正しくありません。すべてのファイルが同じ行に表示され、読みにくいです。この問題を修正できますか?

if [ "$(ls -A $DIR)" ]; 
then 
    printf "=====================================================\n"
    printf " $DIR contains files.\n"
    printf "=====================================================\n"
    sftp [email protected]:/upload <<EOF
    put -r /myfolder/*
    quit
EOF
    printf "=====================================================\n"
    printf "Done transfering files.\n"
    printf "=====================================================\n"
else 
    printf "No files to upload in $DIR"
fi

mailx -s 'Document Feed' [email protected] < /var/log/docfeed.log

ベストアンサー1

次のようなファイル名の配列が得られます。

filenames=( *Table_* )

scp以下を使用してすべてのファイルをコピーできないとします。

scp *Table_* user@host:dir/

次のバッチスクリプトを生成できますsftp

printf 'put "%s"\n' *Table_* | sftp user@host:/dir

ターゲットの名前を変更するには、たとえば、すべてのスペースをアンダースコアに変更します(パターン置換を使用${parameter//pattern/string})。

for name in *Table_*; do
    printf 'put "%s" "%s"\n' "$name" "${name// /_}"
done | sftp user@host:/dir

別の明確な解決策は、関連ファイルのアーカイブを作成し、そのアーカイブを別のホストに転送することです。

tar -cf archive.tar *Table_*

echo 'put archive.tar' | sftp user@host:/dir

おすすめ記事