Bash IFS ('\n'): Problems with file name detection through find command and for loop

南笙酒味 提交于 2020-01-05 03:33:12

问题


#!/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

Is the command substitution done right? The variable does not work with the IFS \n in the for loop, I don't get why this occurs.

for loop is supposed to process filenames with blank space by processing the output of find line by line (thats why I use the IFS \n).


回答1:


The variable does not work with the IFS \n in the for loop, I don't get why this occurs.

IFS='\n' doesn't set IFS to newline, it sets IFS to a literal string \n. If you want to set IFS to newline, use:

IFS=$'\n'



回答2:


I don't see a need for find or the first loop here at all.

Does this do what you want?

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

I also removed the useless n) case as the default case handles that just fine.




回答3:


I fixed it by now. I have thrown away the quotation of the variable in the for loop, fixed the declaration of IFS in the beginning and removed the unnecessary piping.
This should be a good solution to the white space problem
Thanks, now I can insert this into my working script. Why did I kept the quotes?

#!/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  


来源:https://stackoverflow.com/questions/31294077/bash-ifs-n-problems-with-file-name-detection-through-find-command-and-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!