我编写了一个shell脚本,它通过SSH连接到远程主机并进行一些处理。远程执行的代码必须使用从属性文件中读取的局部变量。我的代码如下。以下代码未正确执行。它给出了错误
-printf: unknown primary or command.
请帮助我。
注意:datadir,username和ftphostname在属性文件中定义。
. config.properties
ssh $username@$ftphostname << EOF
filelist=;
filelist=($(find "$datadir" -type f -printf "%T@ %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 '%T@ %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 user@server 'filelist=($(find /home/data -type f - printf "%T@ %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 "%T@ %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 :(得分:0)
我不会直接回答(即,不是根据您的具体需求和行动),而是提供一般的可能性以及如何使用本地和远程变量:
您的主脚本应在本地创建一个特定的脚本&#34;。 然后将其复制并远程运行(如果需要,可以使用附加参数)
主脚本的通用示例:
#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 user@remotehost:/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 user@remotehost "/some/dir/name.sh" #+ maybe some parameters as well
#end of local Master script.
然后你运行&#34;本地主脚本&#34;并让它在本地创建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 "\$@" ; do
du -ks "\$dir"
done
EOF
scp -p /tmp/printsizes.bash user@remotehost:/tmp/print_dir_sizes.bash
ssh user@remotehost "/tmp/print_dir_sizes.bash /etc /root"
这个(奇怪的......)示例将创建一个包含以下内容的LOCAL脚本:
#!/bin/bash
for dir in /tmp /var /usr /home "$@" ; do
du -ks "$dir"
done
并将执行:
ssh user@remotehost "/tmp/print_dir_sizes.bash /etc /root"
所以它会远程执行:
for dir in /tmp /var /usr /home /etc /root ; do
du -ks "$dir"
done
我希望看看如何使用本地和远程变量...