Bash:使用 FTP 检查远程目录是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27233277/
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
Bash: Check if remote directory exists using FTP
提问by grim
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.
我正在编写一个 bash 脚本来将文件从 linux 服务器发送到远程 Windows FTP 服务器。在尝试创建文件之前,我想使用 FTP 检查存储文件的文件夹是否存在。请注意,我无法使用 SSH 或 SCP,也无法在 linux 服务器上安装新脚本。此外,对于性能问题,我更喜欢只使用一个 FTP 连接来检查和创建文件夹。
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
样子:
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).
该ftp_mkdir_loop
功能有助于在$FTP_PATH
(因为我无法mkdir -p $FTP_PATH
通过 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):
总的来说,我的脚本有效,但并不“干净”;这是执行脚本后我在日志文件中得到的内容(是的,$FTP_PATH
由 5 个现有目录组成):
(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.
采纳答案by DVK
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 连接,您可以创建输入(FTP 命令)作为 shell 脚本的输出
例如
$ cat a.sh cd /home/test1 mkdir /home/test1/test2 $ ./a.sh | ftp $Your_login_and_server > /your/log 2>&1
要允许 FTP 测试目录是否存在,您可以使用“DIR”命令具有写入文件的选项这一事实
# ...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
请注意,
"-s"
测试不一定正确——我现在无法访问 ftp,也不知道在不存在的目录上运行 DIR 的确切输出是什么——冷是空文件,可能是一个特定的错误。如果出错,您可以在 $local_output_file 中 grep 错误文本现在,将步骤 #2 包装到 a.sh 中各个子目录的循环中
回答by Cyrus
#!/bin/bash
FTP_HOST=prep.ai.mit.edu
FTP_USER=anonymous
[email protected]
DIRECTORY=/foo # /foo does not exist, /pub exists
LOCAL_LOG=/tmp/foo.log
ERROR="Failed to change directory"
ftp -n $FTP_HOST << EOF | tee -a ${LOCAL_LOG} | grep -q "${ERROR}"
quote USER ${FTP_USER}
quote pass ${FTP_PASS}
cd ${DIRECTORY}
EOF
if [[ "${PIPESTATUS[2]}" -eq 1 ]]; then
echo ${DIRECTORY} exists
else
echo ${DIRECTORY} does not exist
fi
Output:
输出:
/foo does not exist
如果您只想抑制 中的消息
${LOCAL_LOG}
${LOCAL_LOG}
:ftp -n $FTP_HOST <<! | grep -v "Cannot create a file" >> ${LOCAL_LOG}