在Linux中使用shell脚本迭代for循环时,如何转义文件路径中的空格?

时间:2018-04-19 20:44:46

标签: linux shell

我正在Linux中编写一个shell脚本,它接受文件路径并迭代路径中的所有文件。找到文件时,它会执行一些逻辑。如果找到文件夹,则该函数调用自身将该文件夹作为参数传递。除非文件夹路径中有空格,否则它很有效。

# I have tried the following with no success:
#   for f in "$ROOT_FOLDER_PATH" do
#   for f in "${ROOT_FOLDER_PATH}" do
#   for [f in "$ROOT_FOLDER_PATH"] do
#   for [[f in "$ROOT_FOLDER_PATH"]] do


# Most of the logic was removed to give a bare minimum example.
function extension_fixer(){
  ROOT_FOLDER_PATH="${1}"
  echo "ROOT FOLDER PATH: $ROOT_FOLDER_PATH/*"
  for f in $ROOT_FOLDER_PATH/*; do
    echo "FOLDER: $f"
    if [[ -d "$f" ]]; then
      # If the file is a folder, recursively call the extension_fixer function on that folder.
      extension_fixer "${f}"
    elif [[ -f "$f" ]]; then
      echo "Do something..."
    else
      echo "$f is not valid"
      exit 1
    fi
  done
}

extension_fixer "/home/www/accounts/210"

1 个答案:

答案 0 :(得分:1)

中缺少双引号
for f in "$ROOT_FOLDER_PATH"/*; do
#        ~                 ~