linux - 在 SSH 中使用本地和远程变量

标签 linux shell ssh

我编写了一个 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'

最佳答案

我不会直接回答(即,不会根据您的具体需求和操作),而是给出一个通用的可能性以及如何使用本地和远程变量:

您的主脚本应该在本地创建一个“特定脚本”。 然后将其复制过来并远程运行(如果需要,可以添加其他参数)

主脚本的通用示例:

#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. 

然后你运行“本地主脚本”并让它在本地创建 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"

这个(奇怪的...)示例将创建一个本地脚本,其中包含:

#!/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

希望对了解如何使用本地和远程变量有所帮助...

关于linux - 在 SSH 中使用本地和远程变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23762986/

相关文章:

c - ssize_t 在 Linux 中定义在哪里?

linux - 如何存储 ls 的值grep 测试 |变量中的 wc -l

linux - 为 tar 文件创建新脚本返回错误

linux - 该怎么做而不是 awk 中的正则表达式 exp

bash - 通过 ssh 在几个机器上并行执行命令(使用 bash)

linux - 服务器之间的 SSH 登录仍然要求输入密码,为什么?

Python 3.5 setup.py egg_info 失败,错误代码为 1 [Linux]

linux - 匹配两个文件中的字符串,并将第一个文件中匹配字符串的行追加到第二个文件的行尾

c++ - 任意类型上的 GDB 条件断点,例如 C++ std::string 相等性

php - 有没有办法在 PHP 中验证 SSH 公钥?