belim
belim

Reputation: 13

Escaping Whitespace in simple bash script

There are a few mentions of escaping white spaces in previous questions but none which I could, or could work out how to, apply to my situation so I would just like to apologise if I am double posting. Also this is I think incredibly simple but I am really new to bash, well programming in general actually...

Anyway, I am trying to create the following little script:

 #!/bin/bash
    for i in $( zfs list -o name | grep data/ ) ; do
    echo $i
    done

But one of the output lines contains a white space and so is being printed on two separate lines. I need to work out how to have it processed correctly so I can continue with the script and use it to shorten another script I made:

    #!/bin/bash
zfs snapshot data/backups@initial
zfs snapshot data/bin@initial
zfs snapshot data/downloads@initial
zfs snapshot data/movies@initial
zfs snapshot data/music@initial
zfs snapshot data/pictures@initial
zfs snapshot data/stand\ up@initial
zfs snapshot data/thumbs@initial
zfs snapshot data/tv@initial
zfs snapshot data/vids@initial
echo "Snapshots Created"
echo ""
echo "Copying Backups"
zfs send data/backups@initial   | pv -tpreb -s 48M  | ssh 192.168.61.100 zfs recv -dF data
echo "Backups Copied"
echo ""
echo "Copying Bin"
zfs send data/bin@initial   | pv -tpreb -s 46K  | ssh 192.168.61.100 zfs recv -dF data
echo "Bin Copied"
echo ""
echo "Copying Downloads"
zfs send data/downloads@initial | pv -tpreb -s 31M  | ssh 192.168.61.100 zfs recv -dF data
echo "Downloads Copied"
echo ""
echo "Copying Movies"
zfs send data/movies@initial    | pv -tpreb -s 490496M  | ssh 192.168.61.100 zfs recv -dF data
echo "Movies Copied"
echo ""
echo "Copying Music"
zfs send data/music@initial | pv -tpreb -s 66560M   | ssh 192.168.61.100 zfs recv -dF data
echo "Music Copied"
echo ""
echo "Copying Pictures"
zfs send data/pictures@initial  | pv -tpreb -s 6114M    | ssh 192.168.61.100 zfs recv -dF data
echo "Pictures Copied"
echo ""
echo "Copying Stand Up"
zfs send data/stand\ up@initial | pv -tpreb -s 13.312M  | ssh 192.168.61.100 zfs recv -dF data
echo "Stand Up Copied"
echo ""
echo "Copying Thumbs"
zfs send data/thumbs@initial    | pv -tpreb -s 45K  | ssh 192.168.61.100 zfs recv -dF data
echo "Copied Thumbs"
echo ""
echo "Copying TV"
zfs send data/tv@initial    | pv -tpreb -s 787456M  | ssh 192.168.61.100 zfs recv -dF data
echo "Copied TV"
echo ""
echo "Copying Vids"
zfs send data/vids@initial  | pv -tpreb -s 11k  | ssh 192.168.61.100 zfs recv -dF data
echo "Vids Copied"

I am going to be cutting that into a much shorter script but I need the initial bit first. I am hoping I should be able to figure the rest out myself

OK I have slightly changed it to a while statement so that I am able to process the further ZFS commands against it otherwise when I tried to process it I could not process each output line against the comment. It was coming out as one long command which obviously doesnt work.

Anyway so I have:

zfs list -o name | grep data/ | while read i;

But again I am left with the whitespace problem... How would I get round that one?

Upvotes: 1

Views: 839

Answers (1)

sorpigal
sorpigal

Reputation: 26086

Always quote the expansion when you need whitespace to be preserved

for i in "$( zfs list -o name | grep data/ )" ; do
    echo "$i"

Upvotes: 1

Related Questions