bash 上传到 sftp 并移动文件。绕来绕去

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28192983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:17:03  来源:igfitidea点击:

bash upload to sftp and moving files. looping around

bashloopssftp

提问by sander0s

Im somewhat beginner in bash scripting. I have here part of script that uploades files to sftp server. Then it shoud move files to other directory. Else echo info about no files in source_dir. Problem is that its not moving files and is looping back to sftp login after files are already uploaded.

我是 bash 脚本的初学者。我在这里有将文件上传到 sftp 服务器的脚本的一部分。然后它应该将文件移动到其他目录。否则回显有关 source_dir 中没有文件的信息。问题是它没有移动文件,而是在文件上传后循环回到 sftp 登录。

cd /dest_dir/
    for file5 in *.test
            do
                if test -f $file5
                then
                    echo 'some text.'
                    ((
                      echo "
    cd /dest_dir/
    lcd /source_dir/
    mput *.test
    exit
                        "
                    ) | sftp "user"@"host")

                    sleep 3 
                    mv /source_dir/$file5 /source_dir/test/
                else
                    echo -e 'some text'
                fi

回答by tripleee

There is normally no reason to check that the files you are looping over actually exist. If a file could disappear between the forand the test, it could just as well disappear between the testand the echoinstead, in which case you end up referring to a file which no longer exists anyway.

通常没有理由检查您正在循环的文件是否确实存在。如果一个文件可能在 thefor和 the之间消失test,它也可能在 thetest和 the之间消失echo,在这种情况下,您最终会引用一个无论如何都不再存在的文件。

#!/bin/bash
shopt -s nullglob   # bash-only feature
cd /dest_dir/
(#echo "cd /dest_dir/"  # no use, you already did above
echo "lcd /source_dir/"
for file in *.test; do
    echo put "$file"
done
echo "exit" ) | sftp "user"@"host"
mv *.test /source_dir/test/

This opens a single scpconnection for all the files. It has the distinct disadvantage that if one of the files fails for some reason, the script doesn't catch that.

scp将为所有文件打开一个连接。它有一个明显的缺点,如果其中一个文件由于某种原因失败,脚本就不会捕捉到它。

Apart from the shopt -s nullglobthis should be portable to Bourne shell in general.

除了shopt -s nullglob这应该可以移植到 Bourne shell。

回答by sander0s

thank you for tips. I made it: with loging:

谢谢你的提示。我做到了:通过登录:

SFTP_HOST="host"
SFTP_LOGIN="USER"
cd /path/
mylog=/path/sftp_session_$(date "+%Y_%m_%d").log
echo "$(date "+%Y-%m-%d %H:%M:%S") - text" >> $mylog
let licz5=`ls|wc -l`
let jest_plik=0
for file5 in *.test
do
    if test -f /patch_source1/$file5
    then
        let jest_plik=1
        echo "$(date "+%H:%M:%S") - transfer pliku "$file5" na serwer "$SFTP_HOST >> $mylog
    fi

    if test -f /source_patch2/$file6
    then
        echo "$(date "+%H:%M:%S") - transfer pliku "$file6" na serwer "$SFTP_HOST >> $mylog
    fi

    if test -f /source_patch3/$file7
    then
        echo "$(date "+%H:%M:%S") - transfer pliku "$file7" na serwer "$SFTP_HOST >> $mylog
    fi

    if test -f /source_patch4/$file8
    then
        echo "$(date "+%H:%M:%S") - transfer pliku "$file8" na serwer "$SFTP_HOST >> $mylog
    fi
done
if test $jest_plik -eq 1
then
echo 'text.'
(echo 'cd /dest_dir/
lcd /source_p_1/
mput *.test
lcd /source_p_2/
mput *.test
lcd /source_p_3/
mput *.test
lcd /source_p_4/
mput *.test
exit') | sftp $SFTP_LOGIN@$SFTP_HOST
echo "$(date "+%H:%M:%S") - send ok." >> $mylog
else
echo -e 'text'
fi