我正在使用Bash FTP命令来ftp文件,但我有一个问题,我尝试创建一个超过2个文件夹深度的目录。如果我使用两个文件夹深,但是如果我深入三个文件夹,那么它会失败。例如:
mkdir foo/bar - this works
mkdir foo/bar/baz - this fails
我也试过这个:
mkdir -p foo/bar/baz - which didn't work, it ended up creating a '-p' directory
我尝试运行的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
答案 0 :(得分:11)
mkdir foo
cd foo
mkdir bar
cd bar
mkdir baz
cd baz
如果您仍然需要原始构造,也可以这样做:
#!/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
答案 1 :(得分:0)
尝试使用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