Reputation: 324
I'm very sorry if someone already have ask this question or something near it, but I did not found anything close to it, but may be it's because of my weakness in coding.
I have bash this script:
...
...
## Read n line in vmlist
VMTORESTORE=$(sed -n "$yn p" ./vmlist)
## Replace " " between names by "\ "
VMTORESTORE="$(echo "$VMTORESTORE" | sed 's/ /\\ /g' )"
## Test variable
echo $VMTORESTORE
## Run Command
ls -1 /vmfs/volumes/BACKUP_SAN/$VMTORESTORE | sed 's/ /\\ /g'
This is the result:
Save\ Camera
ls: /vmfs/volumes/BACKUP_SAN/Save: No such file or directory
Camera:
I don't understand why the echo gave the good result whereas the command insert a line break between Save\ and Camera.
thanks by advance for your help.
Proc
Upvotes: 0
Views: 48
Reputation: 324
Thanks you all, I did not expect a solution that easy. You were right, the double quotes were needed.
But in my cas this was not enough. The path to the searched folder was:
"/vmfs/volume/BACKUP_SAN/Save Camera/"
but usually in Linux to reach this kind of folder I need to put \ before spaces like that:
"/vmfs/volume/BACKUP_SAN/Save\ Camera/"
That's why I replaced in my script all " " with "\ "
But is seems in a bash script this is not needed so this works like a charm:
...
...
## Read n line in vmlist
VMTORESTORE=$(sed -n "$yn p" ./vmlist)
## Run Command
ls -1 "/vmfs/volumes/BACKUP_SAN/$VMTORESTORE"
I searched the complication whereas it was this simple.
Thank you all for your help.
Upvotes: 0
Reputation: 797
This is happening because \
is an escape character in bash. So shouldnt your path consists of /
rather than \
to define the location save/camera
. Also globbing happens when you are using commands without " "
so use the address in " "
.
Upvotes: 1
Reputation: 3913
it is a good idea to quote the variables as in:
ls -1 "/vmfs/volumes/BACKUP_SAN/$VMTORESTORE"
Take this as a good practice and update the script wherever needed.
Upvotes: 2