1

The folowing code work great but when the folder path contain "," and spaces make error

dir data/ > folder_file.txt

   IFS=$'\n'
for file in "`cat folder_file.txt`"
do
printf 'File found: %s\n' "$file" 
ls  "data/$file/" #-----------> "," and "space" brook this task

done

any idea ? to escape special character

it work now any other advice's to make it better

 IFS=$'\n'
 a=0


 for file in out/*; do

 ls "$file" > html_file.txt


 for file2 in `cat html_file.txt`; do

 echo $file
 mv "$file""/""$file2" "$file""/""page_"$a
 let a=$a+1

  done
  a=0
  done
filip
  • 41
  • 6
  • ls "some, thing/" works for me... what exact error are you getting? – Flash Thunder Dec 31 '16 at 00:49
  • ls "A, b, and à /" ls: impossible d'accéder à A, b, and à /: Aucun fichier ou dossier de ce type – filip Dec 31 '16 at 00:53
  • 1
    Don't take it personally, but you're using several _antipatterns._ And sooner or later your script was meant to break. I don't know where you learned these (broken) techniques, but now you have 2 problems: 1. unlearn them; 2. learn the proper ones. Good luck! – gniourf_gniourf Dec 31 '16 at 00:54
  • Possible duplicate of [Handle special characters in bash for...in loop](http://stackoverflow.com/questions/1563856/handle-special-characters-in-bash-for-in-loop) – Benjamin W. Dec 31 '16 at 00:56
  • @filip: gniourf is right, and your script is probably in different encoding than a shell. – Flash Thunder Dec 31 '16 at 00:56

1 Answers1

1

This is how you loop on the content of a directory:

#!/bin/bash

shopt -s nullglob

for file in data/*; do
    printf 'File found: %s\n' "$file"
    ls "$file"
done

We use the shell options nullglob so that the glob * expands to nothing (and hence the loop is void) in case there are no matches.

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104