My first zsh experience

So, I learned something tonight. I sat down to update my desktop PCs with the latest and greatest OpenOffice 2.0.4. I have this part scripted easily with beyondexec on the ‘Doze hosts, but there’s a big annoying but. Everytime you update OpenOffice you get that damn annoying registration wizard shit. Easily fixed I thought! I’ll just run a quick script under bash to copy the new file over into the users profiles on the Samba server. Hmm, damn backticks were messing with me.

PROFILES=’/my/path/to/my/profiles/’
REGFILE=’/Application Data/OpenOffice.org2/user/registry/data/org/openoffice/’
FILE=Setup.xcu
NEWFILE=’/my/path/to/my/new/Setup.xcu’
PATHWAY=`/usr/bin/find “$PROFILES”*”$REGFILE” -name “$FILE”`

Now, the problem with calling this is that Bash assumes a new line for the Application Data part. Since there’s white space, you get a new line after that. I thought I could fix that by using this in a script:

for file in $(/usr/bin/find “$PROFILES”*”$REGFILE” -name “$FILE); do
echo $file
done

That doesn’t work either, because you get same as with backticks:

/my/path/to/my/profiles/username1/Application
Data/OpenOffice.org2/user/registry/data/org/openoffice/Setup.xcu
/my/path/to/my/profiles/username2/Application
Data/OpenOffice.org2/user/registry/data/org/openoffice/Setup.xcu

For each username, I’d get double the lines so this didn’t work for allowing me to copy the new file over to the old file. I’m no brainiac scripting wizard that’s for sure, so there’s probably even some easier way to do this, but I’m learning. Anyways, after some Googling, I ran across a mailing list post about whitespace in scripts.

Both Bash and ZSH can handle this:

“$(ls -1)” which is the similar to `ls -l`

But the double quotes will cause the output to be combined into one single argument. Remember the output from the backtick command will create a new line for each piece of whitespace it sees. Now after I did some more reading on the post, I found a solution for zsh to be easier. What I came up with:

for file in ${(f)”$(/usr/bin/find “$PROFILES”*”$REGPATH” -name “$FILE”)”}; do
cp $NEWFILE $file
done

Since I’m not a regular expression guru, I don’t understand a lot of things. I do understand what’s going on here, but none the less, zsh saved me a lot of dicking around tonight because it split the output on the newlines as expected and I got my updates done. More importantly, my users won’t be calling in the morning because they aren’t able to click through a 4 click registration wizard…

If there’s an even simpler solution using Bash, then please by all means, show me if I’m being retarded. :)

Posted in Geek Stuff.

2 Responses to “My first zsh experience”

  1. Niels Huylebroeck Says:

    Try in bash *setting* the variables using double quotes. (Unless you did and wordpress screwed it)

    PROFILES=”/my/path/to/my/profiles/”
    REGFILE=”/Application Data/OpenOffice.org2/user/registry/data/org/openoffice/”
    FILE=Setup.xcu
    NEWFILE=”/my/path/to/my/new/Setup.xcu”
    PATHWAY=`/usr/bin/find “$PROFILES”*”$REGFILE” -name “$FILE”`

  2. maxwell Says:

    Niels,

    The reason I was using the single quotes for my variables was because I thought the single quote would preserve the white space better in the REGFILE variable. Or the single quote would keep the string together not allowing any substitution in the variable. The script does work with using single quotes. I’m terrible at regular expressions, but I’m learning. Thanks!

Leave a Reply