![파일 이름 문자열을 배열로 분할](https://linux55.com/image/119380/%ED%8C%8C%EC%9D%BC%20%EC%9D%B4%EB%A6%84%20%EB%AC%B8%EC%9E%90%EC%97%B4%EC%9D%84%20%EB%B0%B0%EC%97%B4%EB%A1%9C%20%EB%B6%84%ED%95%A0.png)
특정 디렉터리(/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
답변2
나는 여기서 머리카락을 나누고 있다고 생각하지만 fwiw, 내가 원래하고 싶었던 것을 성취하기 위해 다음을 사용할 수 있습니다.
shopt -s nullglob
documentarray=(*Translation*)
for i in "${documentarray[@]}"
do
#sftp stuff here
done