원격 호스트에 SSH 쉘 스크립트를 작성하고 일부 처리를 수행했습니다. 원격으로 실행되는 코드는 특성 파일에서 읽은 로컬 변수를 사용해야합니다. 내 코드는 아래와 같습니다. 아래 코드는 제대로 실행되지 않습니다. 그 오류를주는 그SSH에서 로컬 및 원격 변수 사용
-printf: unknown primary or command.
도와주세요.
참고 : datadir, username 및 ftphostname은 등록 정보 파일에 정의되어 있습니다.
. config.properties
ssh [email protected]$ftphostname << EOF
filelist=;
filelist=($(find "$datadir" -type f -printf "%[email protected] %p\n"| sort -n | head -5 | cut -f2- -d" "));
filecount=\${#filelist[@]};
while [ \${#filelist[@]} -gt 0 ]; do
checkCount=;
filesSize=$(wc -c \${filelist[@]}|tail -n 1 | cut -d " " -f1) ;
if [ "\$filesSize" == "\$fileSizeStored" ]; then
fileSizeStored=0;
printf "\n*********** \$(date) ************* " >> /home/chisan/logs/joblogs.log;
echo "Moved below files" >> /home/joblogs.log;
for i in "\${filelist[@]}"
do
# echo "file is \$i"
checkCount=0;
mv \$i /home/outputdirectory/;
if [ $? -eq 0 ]; then
echo "File Moved to the server: \$i" >> /home/joblogs.log;
else
echo "Error: Failed to move file: \$i" >> /home/joblogs.log;
fi
done
filelist=($(find "$datadir" -type f -printf '%[email protected] %p\n' | sort -n | head -5 | cut -f2- -d" "));
else
((checkCount+=1));
sleep 4;
fileSizeStored=\$filesSize;
fi
done
EOF
하지만이 사람은 내가 (즉,하지의 특정 요구와 행동으로)하지만, 일반적인 가능성을 제공하는 방법과 로컬 및 원격 변수를 사용하는 직접 대답하지 않습니다
#ssh to remote system and sort the files and fetch the files which are copied first(based on modification time)
ssh -o StrictHostKeyChecking=no [email protected] 'filelist=($(find /home/data -type f - printf "%[email protected] %p\n" | sort -n | head -5 | cut -f2- -d" "));
# filelist array variable holds the file names which have the oldest modification date.
#check the directory until it has atleast one file.
while [ ${#filelist[@]} -gt 0 ]; do
filesSize=$(wc -c "${filelist[@]}"|tail -n 1 | cut -d " " -f1) ;
#filesSize contains the total size of the files that are in the filelist array.
if [ -e "$HOME/.storeFilesSize" ]; then
fileSizeStored=$(cat "$HOME/.storeFilesSize");
if [ "$filesSize" == "$fileSizeStored" ]; then
echo "Moved below files" >> /home/joblogs.log;
for i in "${filelist[@]}"
do
mv "$i" /home/dmpdata1 &>/dev/null;
if [ $? -eq 0 ]; then
echo "File Moved to the server: $i" >>/home/joblogs.log;
else
echo "Error: Failed to move file: $i" >>/home/joblogs.log;
fi
done
filelist=($(find /home/data -type f -printf "%[email protected] %p\n" | sort -n | head -5 | cut -f2- -d" "));
else
sleep 4;
echo "$filesSize" > "$HOME/.storeFilesSize";
fi
else
echo "creating new file";
echo "$filesSize" > "$HOME/.storeFilesSize";
fi
done'
는'$ fileSizeStored'은 아직 정의되지 않습니다 루프 내부의 끝. 또한 기본 디버깅을 직접 해 보셨습니까? 어느 것이 문제의 원인인지 확인하기 위해 라인을 주석/시작하라. –
당신은 틀린 것들을 정확히 벗어났습니다. 당신의 heredoc은'\ $ remote'와'$ local'을 가져야합니다. 여기서'remote'는 리모트 변수의 변수입니다. –
하지만 일관성을 유지해야합니다. 즉,'filelist'를 이스케이프 처리하거나 이스케이프 처리하지 않은 상태로 둘 수 있습니다. –