击:检查是否存在远程目录使用FTP [英] Bash: Check if remote directory exists using FTP
问题描述
我在写一个bash脚本来从Linux服务器发送文件到远程Windows FTP服务器。
我想,如果该文件将存储在文件夹中存在使用FTP尝试创建它之前检查。
请注意,我不能使用SSH,也不SCP,我不能在Linux服务器上安装新的脚本。此外,对于性能问题,我想如果检查和创建文件夹,仅使用一个FTP连接完成preFER。
下面是发送文件功能:
SENDFILE(){
FTP -n $ FTP_HOST<&LT ;! >> $ {} LOCAL_LOG
报价的$ {} FTP_USER
报价PASS $ {} FTP_PASS
二进制
$(ftp_mkdir_loop$ FTP_PATH)
把$ {FILE_PATH} $ {FTP_PATH} / $ {FILENAME}
再见
!
}
这就是 ftp_mkdir_loop
如下:
ftp_mkdir_loop(){
本地研发
当地一
R =$ @
而[![$ R=$ A]];做
A = $ {红%% / *}
回声MKDIR $了
回声CD $了
R = $ {R·* /}
DONE
}
的 ftp_mkdir_loop
功能在 $ FTP_PATH
创建的所有文件夹帮助(因为我不能做的mkdir -p $ FTP_PATH
)。
总体我的剧本作品,但不是干净;这就是我得到在我的日志文件中的脚本执行后(是的, $ FTP_PATH
由5个现有的目录):
(目录名)时,该文件已存在无法创建文件。
当文件已存在无法创建文件。
当文件已存在无法创建文件。
当文件已存在无法创建文件。
当文件已存在无法创建文件。
要解决这个问题,请执行以下操作:
-
要确保你只使用一个FTP连接,您可以创建一个shell脚本的输出输入(FTP命令)
例如
$猫a.sh
CD /家庭/ test1的
的mkdir /家庭/ TEST1 / TEST2$ ./a.sh | FTP $ Your_login_and_server> /你/日志2 - ;&放大器; 1 -
要允许FTP测试是否存在一个目录,您使用DIR命令有一个选项可以写入文件的事实。
#...继续a.sh
#在一个循环中,$ CURRENT_DIR是下一个子目录办理入住或创建
回声DIR $ CURRENT_DIR $ local_output_file
要创建睡眠5#保持时间的文件
如果(!$ -s local_output_file)
然后
回声MKDIR $ CURRENT_DIR
万一请注意,
- 的
测试不一定是正确的 - 我没有acccess现在FTP和不知道是什么运行DIR的精确的输出对不存在的目录将是 - 冷是空文件,可能是一个特定的错误。如果错误,你可以grep在$ local_output_file错误文本 -
现在,敷步骤#2成一个圈在你的个人子目录a.sh
I'm writing a bash script to send files from a linux server to a remote Windows FTP server. I would like to check using FTP if the folder where the file will be stored exists before attempting to create it. Please note that I cannot use SSH nor SCP and I cannot install new scripts on the linux server. Also, for performance issues, I would prefer if checking and creating the folders is done using only one FTP connection.
Here's the function to send the file:
sendFile() {
ftp -n $FTP_HOST <<! >> ${LOCAL_LOG}
quote USER ${FTP_USER}
quote PASS ${FTP_PASS}
binary
$(ftp_mkdir_loop "$FTP_PATH")
put ${FILE_PATH} ${FTP_PATH}/${FILENAME}
bye
!
}
And here's what ftp_mkdir_loop
looks like:
ftp_mkdir_loop() {
local r
local a
r="$@"
while [[ "$r" != "$a" ]]; do
a=${r%%/*}
echo "mkdir $a"
echo "cd $a"
r=${r#*/}
done
}
The ftp_mkdir_loop
function helps in creating all the folders in $FTP_PATH
(Since I cannot do mkdir -p $FTP_PATH
through FTP).
Overall my script works but is not "clean"; this is what I'm getting in my log file after the execution of the script (yes, $FTP_PATH
is composed of 5 existing directories):
(directory-name) Cannot create a file when that file already exists.
Cannot create a file when that file already exists.
Cannot create a file when that file already exists.
Cannot create a file when that file already exists.
Cannot create a file when that file already exists.
To solve this, do as follows:
To ensure that you only use one FTP connection, you create the input (FTP commands) as an output of a shell script
E.g.
$ cat a.sh cd /home/test1 mkdir /home/test1/test2 $ ./a.sh | ftp $Your_login_and_server > /your/log 2>&1
To allow the FTP to test if a directory exists, you use the fact that "DIR" command has an option to write to file
# ...continuing a.sh # In a loop, $CURRENT_DIR is the next subdirectory to check-or-create echo "DIR $CURRENT_DIR $local_output_file" sleep 5 # to leave time for the file to be created if (! -s $local_output_file) then echo "mkdir $CURRENT_DIR" endif
Please note that
"-s"
test is not necessarily correct - I don't have acccess to ftp now and don't know what the exact output of running DIR on non-existing directory will be - cold be empty file, could be a specific error. If error, you can grep the error text in $local_output_fileNow, wrap the step #2 into a loop over your individual subdirectories in a.sh
这篇关于击:检查是否存在远程目录使用FTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!