0

我正在尝试转义路径名中的空格/Volumes/NO NAME。我需要通过bash脚本访问此目录中存在的文件。我使用过sedprintf命令,但似乎都没有奏效。我到目前为止的代码:

while read line
do
  if [[ -d ${line} ]]; then
    echo "${line} is a directory. Skipping this.";
  elif [[ -f ${line} ]]; then
    spacedOutLine=`echo ${line} | sed -e 's/ /\\ /g'`;
    #spacedOutLine=$(printf %q "$line");
    echo "line = ${line} and spacedOutLine = ${spacedOutLine}";
  else
    echo "Some other type of file. Unrecognized.";
  fi
done < ${2}

这似乎都没有奏效。对于像这样的输入:/Volumes/NO NAME/hello.txt,输出是:

/Volumes/NO: No such file or directory
NAME/hello.txt: No such file or directory  

我在 Mac 上并bash通过终端使用。我还浏览了很多关于 SO 的其他帖子,这些帖子对我没有帮助。

4

1 回答 1

1

调用${line}变量时使用双引号。

"${line}"
于 2013-10-15T07:41:24.990 回答