2014-05-20 3 views
1

원격 호스트에 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' 
+0

는'$ fileSizeStored'은 아직 정의되지 않습니다 루프 내부의 끝. 또한 기본 디버깅을 직접 해 보셨습니까? 어느 것이 문제의 원인인지 확인하기 위해 라인을 주석/시작하라. –

+1

당신은 틀린 것들을 정확히 벗어났습니다. 당신의 heredoc은'\ $ remote'와'$ local'을 가져야합니다. 여기서'remote'는 리모트 변수의 변수입니다. –

+1

하지만 일관성을 유지해야합니다. 즉,'filelist'를 이스케이프 처리하거나 이스케이프 처리하지 않은 상태로 둘 수 있습니다. –

답변

0

작동합니다

마스터 스크립트는 로컬에서 "특정 스크립트"를 만들어야합니다. 그리고 그것을 통해 복사 마스터 스크립트의

일반적인 예 (필요한 경우 additionnal 인수) 원격으로 실행

#local Master script: This script creates a local script, 
#      and then copy it to remotehost and start it 

#Some local variables will be defined here. 
#They can be used below, and will be replaced by their value locally 
localvar1="...." 
localvar2="...." 

#now we create the script 
cat > /tmp/localscript_to_be_copied_to_remote.sh <<EOF 
#remote_script 

for i in ..... ; do 
    something ; 
    somethingelse 
done 
...... 
..... 
EOF 

#in the above, each time you used "$localvar1" or "$localvar2", the script 
# /tmp/localscript_to_be_copied_to_remote.sh will instead have their values, 
# as the local shell will replace them on the fly during the cat > ... <<EOF . 
# if you want to have some remotevariable "as is" (and not as their local value) in the script, 
# write them as "\$remotevariable" there, instead of "$remotevariable", so the local shell 
# won't interpret them during the 'cat', and the script will receive "$remotevariable" 
# as is, instead of its local value. 

#then you copy the script: 
scp -p /tmp/localscript_to_be_copied_to_remote.sh [email protected]:/some/dir/name.sh 

#and you run it: 
# UNCOMMENT the line below ONLY when /tmp/localscript_to_be_copied_to_remote.sh is correct! 
# ssh [email protected] "/some/dir/name.sh" #+ maybe some parameters as well 

#end of local Master script. 

당신은 다음 "로컬 마스터 스크립트"를 실행하고 로컬 tmp 파일을 만들 수 있습니다 (원격 호스트에서 이와 같아야하는지 확인할 수 있음) 원격으로 복사 한 다음 실행하십시오. 마스터 스크립트의

구체적인 예 :

#!/bin/bash 
local1="/tmp /var /usr /home" # this will be the default name of the dirs (on the remote host) 
           # that the script will print the size of (+ any additionnal parameters) 

cat > /tmp/printsizes.bash <<EOF 
#!/bin/bash 
for dir in $local1 "\[email protected]" ; do 
    du -ks "\$dir" 
done 
EOF 

scp -p /tmp/printsizes.bash [email protected]:/tmp/print_dir_sizes.bash 

ssh [email protected] "/tmp/print_dir_sizes.bash /etc /root" 

이 (이상한 ...) 예를 들어 LOCAL 스크립트를 생성합니다 :

#!/bin/bash 
for dir in /tmp /var /usr /home "[email protected]" ; do 
    du -ks "$dir" 
done 

를 그리고 그것을 실행합니다 :

ssh [email protected] "/tmp/print_dir_sizes.bash /etc /root" 

그래서 원격으로 수행합니다 :

당신은 단지 근처를 정의하기 때문에 나는 그것이 로컬 및 원격 변수를 사용하는 방법에 도움이되기를 바랍니다
for dir in /tmp /var /usr /home /etc /root ; do 
    du -ks "$dir" 
done 

... while 루프 주위 첫 번째 이동

관련 문제