bash FTP Shell 脚本 mkdir 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11553430/
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 02:48:36  来源:igfitidea点击:

FTP Shell Script mkdir issue

bashftp

提问by davey1990

I am using the Bash FTP command to ftp files, however i have a problem where i try to create a directory that is more than 2 folders deep. It works if i use two folders deep but if i go to three folders deep then it fails. For example:

我正在使用 Bash FTP 命令来处理 ftp 文件,但是我在尝试创建一个深度超过 2 个文件夹的目录时遇到了问题。如果我使用两个深的文件夹,它会起作用,但如果我使用深的三个文件夹,则它会失败。例如:

mkdir foo/bar - this works
mkdir foo/bar/baz - this fails

I have also tried this:

我也试过这个:

mkdir -p foo/bar/baz - which didn't work, it ended up creating a '-p' directory

The shell script i am trying to run is actually quite simple but as you can see the directory structure is 3 folders deep and it fails to create the required folders:

我试图运行的 shell 脚本实际上非常简单,但是正如您所看到的,目录结构有 3 个文件夹深,并且无法创建所需的文件夹:

#!/bin/bash
DIRECTORY="foo/bar/baz"
FILE="test.pdf"         
HOST="testserver"           
USER="test"         
PASS="test"         

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASS
mkdir $DIRECTORY
cd $DIRECTORY
binary
put $FILE
quit
END_SCRIPT

回答by pizza

mkdir under ftp is implemented by the ftp server, not by calling /bin/mkdir no such options as -p, what you should do is

ftp下的mkdir是由ftp服务器实现的,不是通过调用/bin/mkdir没有-p这样的选项,你应该做的是

mkdir foo
cd foo
mkdir bar
cd bar
mkdir baz
cd baz

If you still want your original construct, you can also do it like this:

如果你仍然想要你的原始结构,你也可以这样做:

#!/bin/bash
foo() {
    local r
    local a
    r="$@"
    while [[ "$r" != "$a" ]] ; do
        a=${r%%/*}
        echo "mkdir $a"
        echo "cd $a"
        r=${r#*/}
    done
}
DIRECTORY="foo/bar/baz"
FILE="test.pdf"         
HOST="testserver"           
USER="test"         
PASS="test"         

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASS
$(foo "$DIRECTORY")
binary
put $FILE
quit
END_SCRIPT

回答by mcsrainbow

Try lftp instead:

尝试 lftp 代替:

[dong@idc1-server1 ~]$ lftp sftp://idc1-server2
lftp idc1-server2:~> ls
drwxr-xr-x    3 dong     dong         4096 Jun 16 09:11 .
drwxr-xr-x   18 root     root         4096 Apr  1 22:25 ..
-rw-------    1 dong     dong          116 Jun 16 09:28 .bash_history
-rw-r--r--    1 dong     dong           18 Oct 16  2013 .bash_logout
-rw-r--r--    1 dong     dong          176 Oct 16  2013 .bash_profile
-rw-r--r--    1 dong     dong          124 Oct 16  2013 .bashrc
drwx------    2 dong     dong         4096 Jul 24  2014 .ssh

lftp idc1-server2:~> mkdir a/b/c/d
mkdir: Access failed: No such file (a/b/c/d)

lftp idc1-server2:~> mkdir -p a/b/c/d
mkdir ok, `a/b/c/d' created