[Sugar-devel] Quoting in shell scripts (was: Re: [sugar-devel] Recent fiddlings with Print Support)

Vamsi Krishna Davuluri vamsi.davuluri at gmail.com
Sat May 16 08:50:56 EDT 2009


Hello! I finally figured what was going wrong with my script.


Jonas, I have tried implementing your suggestions. using mktemp, XXXXX, and
also a -m 077. The code doesnt work for some reason that way. lpr gets me a
failed job. Also that code is basically ported from this guide,
http://www.ibm.com/developerworks/linux/library/l-dvi-filter.html

Jonas, Silbe, notice this

[Vamsi at localhost Desktop]$ $(abiword --to=ps /home/Vamsi/lololo.odt); if [
$? -ne 0 ];  then echo 'hello'; else echo  'what'; fi
hello

[Vamsi at localhost Desktop]$ $(abiword --to=doc /home/Vamsi/lololo.odt); if [
$? -ne 0 ];  then echo 'hello'; else echo  'what'; fi
what

[Vamsi at localhost Desktop]$ $(set -e abiword --to=doc
/home/Vamsi/lololo.odt); if [ $? -ne 0 ];  then echo 'hello'; else echo
'what'; fi
what

[Vamsi at localhost Desktop]$ $(set -e abiword --to=ps /home/Vamsi/lololo.odt);
if [ $? -ne 0 ];  then echo 'hello'; else echo  'what'; fi
what

[Vamsi at localhost Desktop]$ $(abiword --to=ps /home/Vamsi/lololo.odt); if [
$? -ne 0 ];  then echo 'hello'; else echo  'what'; fi
hello

[Vamsi at localhost Desktop]$ $(abiword --to=ps /home/Vamsi/lololo.odt); if [
$? -ne 0 ];  then echo 'hello'; else echo  'what'; fi
hello

Set -e basically yields me the same case, that is even on an error it
returns me a 0. But without set -e , it works great.


Moving on,

Its like this,

The lp user of lp group ( cups script executor) messes up with permissions
when executing commands in a subshell, that's really messed up. my rationale
is, in a subshell the lp user has no longer domain. Also, CUPS doesnt write
to our /tmp folder, instead has its own folder defined somewhere.
So what I made is,

sandbox1="${TMPDIR1:-/tmp}/tempcups.$$$$"
(umask 077 && mkdir "$sandbox1") || exit 1

I made another temp folder for the subshells to execute their commands


And, andres, it initially does try to convert to doc.


The algorithm is pretty much like this,

1) We try to write to some new path besides the same folder, if it works, we
are running 2.6.6 and plus, if not we are on 2.6.6--
    - We cannot check for ~somepath.ps here as 2.6.8 doesnt have that
capability, so that would be inviting a hole as we would be skipping for
2.6.8 as well!

# $fn happens to be supplied variable
$(abiword --to=$sandbox1/temp123.doc $fn)

# if this doesnt work, do the following, as 2.6.6- could only do operations
in the same folder,
# we are copying ;)
if [ "$?" -ne 0 ];
then
#our dummy file
fn1="$sandbox/temp123.odt"
cp "$fn" "$fn1"


# Call abiword quietly, securely
abiword --to="ps" "`echo "$fn1" | sed -e 's/odt$/doc/' `"
fn2="`echo "$fn1" | sed -e 's/odt$/ps/' `"


2) Now we enter the case what if we are in fact on 2.6.6+, well now come two
paths again, 1) are we 2.6.8, or 2) anything other than 2.6.8
   (again 2.6.8 cant write to ps directly, we need an intermediate doc
conversion for this effect)

# Call abiword quietly, securely
#check if our version doesn't require an intermediate conversion, if it
does, do it

$(abiword --to="$sandbox1/temp123.ps" "$fn")
if [ "$?" -ne 0 ];

then

abiword --to="$sandbox/temp123.doc" "$fn"
abiword --to="$sandbox/temp123.ps" "$sandbox/temp123.doc"

else

#abiword --to="$sandbox/temp123.ps" "$fn"

fn2="$sandbox1/temp123.ps"

Andres, script works great for ALLL cases :D



#!/bin/bash
# CUPS filter to process ODT files using abiword



# $6 happens to be the path to file passed as argument for debugging
purposes i am use $1
fn="$6"

#for our subshell convenience
sandbox1="${TMPDIR1:-/tmp}/tempcups.$$$$"
(umask 077 && mkdir "$sandbox1") || exit 1


# we are creating a dummy folder, which can take different file types.
sandbox="${TMPDIR:-/tmp}/t6cups-odftops.$$$$"
(umask 077 && mkdir "$sandbox") || exit 1


#The condition which checks whether our abiword is 2.6.6+ or 2.6.6-

$(abiword --to="$sandbox1/temp123.doc" "$fn")

if [ "$?" -ne 0 ];
then

#our dummy file
fn1="$sandbox/temp123.odt"
cp "$fn" "$fn1"

# Call abiword quietly, securely
abiword --to="ps" "`echo "$fn1" | sed -e 's/odt$/doc/' `"
fn2="`echo "$fn1" | sed -e 's/odt$/ps/' `"

else

# Call abiword quietly, securely
#check if our version doesn't require an intermediate conversion, if it
does, do it

$(abiword --to="$sandbox1/temp123.ps" "$fn")
if [ "$?" -ne 0 ];

then

abiword --to="$sandbox/temp123.doc" "$fn"
abiword --to="$sandbox/temp123.ps" "$sandbox/temp123.doc"

else

#abiword --to="$sandbox/temp123.ps" "$fn"

fn2="$sandbox1/temp123.ps"

fi

fi

cat "$fn2"

#remove the sandbox folder, for debugging purposes check by commenting the
following line and see what is in the /tmp/ folder
#rm -rf $sandbox
#rm -rf $sandbox1
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.sugarlabs.org/archive/sugar-devel/attachments/20090516/88c3e914/attachment-0001.htm 


More information about the Sugar-devel mailing list