Bash removes tilde and wildcards but doesn't run through

(EDIT in solution with sed)

I have a list of file names and directories, including tilde and wildcards. For instance:.

~/docs/file.*    
~/my docs/*.txt
...

I read the lines and passed them to the command (e.g. rsync):

while read ROW
do
   rsync $ROW /my/dest/
done < list.txt

The problem is handling with spaces in file names. If I put $ ROW in double quotes like this

rsync "$ROW" /my/dest/

Of course, bash does not get out of wildcards or tildes. But if I don't use quotation marks, space breaks the string.

One possible solution is to change the IFS (caution: the script is more complicated than the one I reported). Another solution (thanks for fixing Patrick Echterbruch) is the preliminary spaces. However, the following code does not work for me:

while read ROW
do
    export NEWROW=$(echo $ROW | sed -e 's/ /\\ /g') 
    echo "Value: $NEWROW"
    ls -1d $NEWROW
done < list.txt

, ls. "~/a b c/test.txt" , :

Value: ~/saver/a\ b\ c/*.txt
ls: impossibile accedere a ~/saver/a\: Nessun file o directory
ls: impossibile accedere a b\: Nessun file o directory
ls: impossibile accedere a c/*.txt: Nessun file o directory

, NEWROW ls , , , .

? .

+5
2

, sed script, , - "" "" .

:

ROW=$(echo $ROW | sed -e "s/ /\\\\ /g")

():

ROW=$(echo $ROW | sed -e 's/ /\\ /g')

, .

: (, ~) bash, . bash .

rsync $NEWROW ... - rsync $NEWROW .

, , , :

ROW="~/a b c"
$NEWROW=$(echo $ROW | sed -e 's/ /\\ /g')
bash -c "ls -ld $f"

, bash (.. ~/a\ b\ c , ). "" bash , , .

, .

: sed (, NEWROW=$(echo $ROW | sed -e 's/ /\\ /g') try:

eval ls -ld $NEWROW

  eval rsync $NEWROW/other/path

.

//EDIT: g sed script

//EDIT:

//EDIT: eval

+1

, rsync ,

while read ROW
do
  echo "rsync '$ROW' /my/dest/"
done < list.txt > rsync.txt

sh rsync.txt
0

All Articles