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

Jahid
  • 21,542
  • 10
  • 90
  • 108
  • 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}"`? – Etan Reisner Jul 08 '15 at 13:31
  • I wasn't fast enough in editing. It are all $iso in the plain text of my script. – Max Hopfgartner Jul 08 '15 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? – Palu Macil Jul 08 '15 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. – Etan Reisner Jul 08 '15 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? – Etan Reisner Jul 08 '15 at 13:38
  • The error within here is that the variable isn't seperated by new line and im questioned by echo if I would like to use file1.iso and file2.iso so processing the file won't work too. Bash output: Use RMCP01_copy.iso RMCP 01.iso? [Y/N]? Last file name was for testing white space workaround. Also im fairly new to this site and it's editing. Now you can read the post again ;). – Max Hopfgartner Jul 08 '15 at 13:40
  • @EtanReisner The last few lines of find are it's matches of *.iso files so it's output is limited to just that. – Max Hopfgartner Jul 08 '15 at 13:48
  • The [tag:processing] tag should only be used for questions about the Processing language. – Kevin Workman Jul 08 '15 at 13:50
  • The find will only output `.iso` files because of its arguments. The loop will also only find `.iso` files. `find` is only searching the current directory and so those should always find exactly the same files. I do not see what the point here is? If you want the list of all `.iso` files in the same directory just do `isos=(*.iso)` and be done with it. Then loop over then with `for iso in "${isos[@]}"; do` and there's no need to mess with `IFS`, no need to use `find`, no games and all filenames are safe. – Etan Reisner Jul 08 '15 at 13:53
  • The problem with your echo is that you are quoting the expansion of `$AX` so it doesn't get word-split for the loop. That being said this entire approach, as I just said, is very confused. – Etan Reisner Jul 08 '15 at 13:54
  • Does this also solve the white space problem? I now fixed the find command not to show all including of the dir before showing matches by removing the "." parameter, theres no need to pipe. It must not split words but lines. – Max Hopfgartner Jul 08 '15 at 13:56
  • What whitespace problem? Using the loop/array directly has no whitespace problems with filenames. Using `find` *introduces* whitespace problems. – Etan Reisner Jul 08 '15 at 14:23

3 Answers3

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'
Jahid
  • 21,542
  • 10
  • 90
  • 108
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.

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
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  
Marki555
  • 6,434
  • 3
  • 37
  • 59