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

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).

12
  • I don't understand the question here. Can you explain more? Why are you looping over .iso files but then using find to get the .wbfs files? Would it not be easier to just loop over both and count the iso files and use an array for the wbfs files and then just use array offset indexing to get the count you want? case $image in *.iso) count+=1;; *.wbfs) files+=("$image");; esac and then "${files[@]:$count}"? Commented Jul 8, 2015 at 13:31
  • I wasn't fast enough in editing. It are all $iso in the plain text of my script. Commented Jul 8, 2015 at 13:33
  • The question body outside your code should have some additional explanation of what the problem is. Is there an exception, or is the output / result simply not what you expected? If not, why? Commented Jul 8, 2015 at 13:33
  • What about this code isn't working? If the code isn't accurate to what you have locally or are trying to do then edit it and fix it. And please, as Palu Macil said more clearly, give us more description about what you are trying to do and what is (and isn't) happening with this code. Commented Jul 8, 2015 at 13:37
  • What is the purpose of that find command? You are getting a count of every iso file from the loop and then using find and limiting its output to exactly that same count? Commented Jul 8, 2015 at 13:38

3 Answers 3

2

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'
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that was the problem besides the quotation.
1

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.

Comments

1

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  

1 Comment

This can also fail if you by mistake happen to have newline in some filename

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.