Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将当前路径的目录名(带空格)作为参数传递给bash脚本?_Bash_Perl - Fatal编程技术网

如何将当前路径的目录名(带空格)作为参数传递给bash脚本?

如何将当前路径的目录名(带空格)作为参数传递给bash脚本?,bash,perl,Bash,Perl,我可以在ubuntubash和windows/cygwinbash上重现这个问题。 问题发生在文件名和目录名中有空格的情况下。 下面是我的脚本pass-dir-names-with-spaces.sh: for dir in "$@" do echo dir = $dir done 这非常有效: ./pass-dir-names-with-spaces.sh * dir = pass-file-names-with-spaces.sh dir = this is a directory w

我可以在ubuntubash和windows/cygwinbash上重现这个问题。 问题发生在文件名和目录名中有空格的情况下。 下面是我的脚本pass-dir-names-with-spaces.sh:

for dir in "$@"
do
  echo dir = $dir
done
这非常有效:

./pass-dir-names-with-spaces.sh *
dir = pass-file-names-with-spaces.sh
dir = this is a directory with spaces in it
dir = this is a file name with spaces.txt
dir = this is another file name with spaces.txt
我在每一行上都会得到完整的文件名,即使其中有空格。有时这是完全可以接受的,因为我需要文件名和目录名。但是,有时我想用perl实现一个过滤器来删除文件名

backtick/perl方法不起作用!这是我第一次尝试逃离太空

./pass-dir-names-with-spaces.sh `perl -e ' my @m  = (); my $c = shift @ARGV; opendir $h, $c or die "cannot opendir $c"; @a= map { push @m, $_ if  -d $_ } grep(!/^\.\.$/,readdir $h); closedir $h; foreach (@m){s/ /\\\\ /g; print " $_ " } '  .`
dir = .
dir = this\
dir = is\
dir = a\
dir = directory\
dir = with\
dir = spaces\
dir = in\
dir = it
我试着用引号代替:

./pass-dir-names-with-spaces.sh `perl -e ' my @m  = (); my $c = shift @ARGV; opendir $h, $c or die "cannot opendir $c"; @a= map { push @m, $_ if  -d $_ } grep(!/^\.\.$/,readdir $h); closedir $h; foreach (@m){ print " \"$_\" " } '  .`
dir = "."
dir = "this
dir = is
dir = a
dir = directory
dir = with
dir = spaces
dir = in
dir = it"

看来bash忽略了我的引用!看来,$@的行为不正常!如何筛选出文件名

谢谢


Siegfried

传递目录名的方式不正确

您需要像以下简化版的perl脚本那样重新构造调用:

readarray -t -d '' dirs < <(perl -e 'print "$_\x00"')
./pass-dir-names-with-spaces.sh "${dirs[@]}"

这是你传递目录名的方式不对

您需要像以下简化版的perl脚本那样重新构造调用:

readarray -t -d '' dirs < <(perl -e 'print "$_\x00"')
./pass-dir-names-with-spaces.sh "${dirs[@]}"
set-:清空参数数组。 对于d in*;do:将每个目录项作为变量d迭代。 [-d$d]&&set-$@$d:如果条目d是目录,则将其添加到参数数组中。 ./pass-dir-names-with-spaces.sh$@:最后将参数数组传递给脚本或命令。 设置- 对于d in*;做 [-d$d]&&set-$@$d 完成 ./pass-dir-names-with-spaces.sh$@ 现在,为了反映,让我们使用find:

找到-mindepth 1-maxdepth 1-type d-exec./pass-dir-names-with-spaces.sh{}+ 或者如果find对于-exec命令{}+语法来说太旧:

找到-mindepth 1-maxdepth 1-type d-printf“%f\0”| xargs-0./pass-dir-names-with-spaces.sh set-:清空参数数组。 对于d in*;do:将每个目录项作为变量d迭代。 [-d$d]&&set-$@$d:如果条目d是目录,则将其添加到参数数组中。 ./pass-dir-names-with-spaces.sh$@:最后将参数数组传递给脚本或命令。 设置- 对于d in*;做 [-d$d]&&set-$@$d 完成 ./pass-dir-names-with-spaces.sh$@ 现在,为了反映,让我们使用find:

找到-mindepth 1-maxdepth 1-type d-exec./pass-dir-names-with-spaces.sh{}+ 或者如果find对于-exec命令{}+语法来说太旧:

找到-mindepth 1-maxdepth 1-type d-printf“%f\0”| xargs-0./pass-dir-names-with-spaces.sh 你知道查找类型d吗?祝你好运。你知道查找d型吗?祝你好运