shell命令在SFTP脚本中不起作用

时间:2014-08-19 05:45:54

标签: shell sftp

我是编程的新手..我有这个sftp脚本,它sftp到其他服务器检查一些条件&然后得到文件。

#! /usr/bin/bash

while read line
do
#echo $line
if [[ $line =~ ^#  ]];
then
#echo $line;
continue;

else
serverIP=`echo $line|cut -d',' -f1`
userID=`echo $line|cut -d',' -f2`
fi
done < sftp.conf

cd /root

sshpass -p red32hat sftp $userID@$serverIP <<EOF
for (; ;) 
do
cd /root/perl
#foreach my $file (<$inputdir/*seq>) {
for $file in *seq 
do
abc1=`find $file -mmin +1`;
if [[ $abc1 eq "" ]] 
then
echo "Skipping file as of now as file is not completed \n";
continue;
fi
mget $file
done
sleep 30
done
bye
EOF

但是每次sftp步骤都会抛出错误,说“无效命令”..我猜一般shell命令在sftp中不起作用...请帮助...下面是错误信息...

eankuls@L9AHR43:~/Idea_Expan$ bash -x sftp_idea.sh
+ read line
+ [[ #bsackjsabckjdsbcds =~ ^# ]]
+ continue
+ read line
+ [[ rinacac-test,root =~ ^# ]]
++ echo rinacac-test,root
++ cut -d, -f1
+ serverIP=rinacac-test
++ echo rinacac-test,root
++ cut -d, -f2
+ userID=root
+ read line
+ [[ #geet =~ ^# ]]
+ continue
+ read line
+ sshpass -p red32hat sftp root@rinacac-test

    ------------------------------------------------------------------------------------
    These computer resources, specifically Internet access and E-mail, are
    provided for authorized users only. 

    IF YOU ARE NOT AN AUTHORIZED USER, PLEASE EXIT IMMEDIATELY.
    ------------------------------------------------------------------------------------
Connected to rinacac-test.
sftp> for ((;;)) 
Invalid command.
sftp> do
Invalid command.
sftp> cd /root/perl
sftp> #foreach my $file (<$inputdir/*seq>) {
sftp> for file in *seq 
Invalid command.
sftp> do
Invalid command.
sftp> abc1=`find $file -mmin +1`;
Invalid command.
sftp> if [[ $abc1 == "" ]]
Invalid command.
sftp> then
Invalid command.
sftp> echo "Skipping file as of now as file is not completed \n";
Invalid command.
sftp> continue;
Invalid command.
sftp> fi
Invalid command.
sftp> mget $file
Couldn't stat remote file: No such file or directory
File "/root/perl/$file" not found.
sftp> done
Invalid command.
sftp> sleep 30
Invalid command.
sftp> done
Invalid command.
sftp> bye

1 个答案:

答案 0 :(得分:0)

在EOF中添加引号以防止在此文档中进行扩展:

sshpass -p red32hat sftp "$userID@$serverIP" <<'EOF'

将变量放在引号周围以防止分词和路径名扩展。

同样for $file in *seq应为for file in *seq

for (; ;)也应为for ((;;))

eq中的

[[ $abc1 eq "" ]]不是有效的运算符。也许您的意思是===