3
#!/bin/bash

IFS='\n' 
declare -i count=0

AX=$(find *.iso -maxdepth 1 -type f) # Rather use AX="$(find *.iso -maxdepth 1 -type f"?
# A="${AX%x}" < Could I use this when applying "" to $() in AX? But it should already include newlines like this way. edit: I don't need the trailing newlines fix.

for iso in "$AX"
 do
  echo "Use "$iso"? [Y/N]?" # Outputs ALL files, IFS has no force somehow
  read choiceoffile
  shopt -s nocasematch
  case $choiceoffile in
  y ) echo "Using selected file.";;
  * ) continue;;
  esac
  # some sort of processing
done

命令替换是否正确?该变量不适用于 for 循环中的 IFS \n,我不明白为什么会发生这种情况。

for 循环应该通过逐行处理 find 的输出来处理带有空格的文件名(这就是我使用 IFS \n 的原因)。

4

3 回答 3

2

该变量不适用于 for 循环中的 IFS \n,我不明白为什么会发生这种情况。

IFS='\n'不设置IFS为换行符,它设置IFS为文字字符串\n。如果要将 IFS 设置为换行符,请使用:

IFS=$'\n'
于 2015-07-08T14:09:57.733 回答
1

我根本看不到这里需要find或第一个循环。

这是做你想做的吗?

for iso in *.iso
 do
  echo "Use $iso? [Y/N]?"
  read choiceoffile
  shopt -s nocasematch
  case $choiceoffile in
  y ) echo "Using selected file.";;
  * ) continue;;
  esac
  # some sort of processing
done

我还删除了无用的n)情况作为默认情况处理就好了。

于 2015-07-08T14:25:05.060 回答
1

我现在修好了。我在for循环中丢弃了变量的引用,在开头修复了IFS的声明并删除了不必要的管道。
这应该是解决空白问题的好方法
谢谢,现在我可以将其插入到我的工作脚本中。为什么我保留引号?

#!/bin/bash

IFS=$'\n'   

AX=$(find *.wbfs -maxdepth 1 -type f )  

for wbfs in $AX  
 do  
  echo "Use "$wbfs"? [Y/N]?"  
  read choiceoffile  
  shopt -s nocasematch  
    case $choiceoffile in  
      y ) echo "Using selected file.";;  
      * ) continue;;  
    esac  
   # some sort of processing  
done  
于 2015-07-08T14:27:45.750 回答