exercise in shell commands

Task at hand: db records of a web application are synced to an external application, syncing of additional fields was requested. The script is updated, and existing records should sync to push the new fields. * The syncing takes place through directory/file based queues ([fsq](https://github.com/axialmarket/fsq)). * There is a program that writes the appropriate file to the queue given a record type and id. * The queue directory is writable only to the application's user, `appuser`. Getting a list of the records to update is easy:
mysql -udbuser db -ss -e 'select id from this_table where necessary_conditions = true;'
Which can be passed to the queueing program.
mysql -udbuser db -ss -e 'select id from this_table where necessary_conditions = true;' | \
    xargs -I{} sync -t RECORD_TYPE {}
Import errors. `sync` is python, and needs some path set to import some modules.
export PYTHONPATH="$PYTHONPATH:/path/to/lib";
mysql -udbuser db -ss -e 'select id from this_table where necessary_conditions = true;' | \
    xargs -I{} sync -t RECORD_TYPE {}
`sync` runs, but I see directory permission errors, my user can't write to the queue directory.
export PYTHONPATH="$PYTHONPATH:/path/to/lib"
mysql -udbuser db -ss -e 'select id from this_table where necessary_conditions = true;' | \
    xargs -I{} sudo -u appuser sync -t RECORD_TYPE {}
Import errors again, `sudo` doesn't bring `PYTHONPATH` with it. There's an `-E` option to preserve environment.
export PYTHONPATH="$PYTHONPATH:/path/to/lib"
mysql -udbuser db -ss -e 'select id from this_table where necessary_conditions = true;' | \
    xargs -I{} sudo -E -u appuser sync -t RECORD_TYPE {}
Same import error. Which part isn't working?
SOMEVAR="this"
echo $SOMEVAR
> this
sudo echo $SOMEVAR
> this
See, 'this' is there!
printenv SOMEVAR
>
But not there.
export SOMEVAR
printenv SOMEVAR
> this
Now it's there. But not here.
sudo printenv SOMEVAR
>
`-E` only works when `setenv` is enabled globally or for the command. Before this, I hadn't paused to notice the difference. Final script:
mysql -udbuser db -ss -e 'select id from this_table where necessary_conditions = true;' | \
    xargs -I{} sudo -u appuser bash -c 'export PYTHONPATH="$PYTHONPATH:/path/to/lib"; sync -t RECORD_TYPE {};'

posted: 08/23/2013