Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Thu 17 Apr 2014, 14:26
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Having a problem writing script string to script file.
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [12 Posts]  
Author Message
sunburnt


Joined: 08 Jun 2005
Posts: 4979
Location: Arizona, U.S.A.

PostPosted: Sat 26 May 2012, 15:49    Post subject:  Having a problem writing script string to script file.  

I need my app. to write a script file, but the shell always runs. it.
I need $SQ_EXT, $MENU, $pkgN evaluated, all other variables as literals.
Code:
echo "#!/bin/sh
MntPt=/SqApp/mnt/$pkgN
SqF=\`basename $MntPt\`.$SQ_EXT

if [ \"$1\" = '-m' ];then
   echo $MENU
elif [ \"$1\" = '-x' ];then
   umount $MntPt
   [ $? -gt 0 ]&& echo -e \" \n### ERROR:  Failed SqApp unmount:  $SqF\n\"
else
   [ ! -f $pkgN.$SQ_EXT ]&&
      echo -e \" \n### ERROR:  No SqApp. file:  $SqF\n\" && exit
   [ \"\`df |grep $MntPt\`\" ]||
      mount -o loop /SqApp/bin/$pkgN.$SQ_EXT $MntPt
   [ $? -gt 0 ]&&
      echo -e \" \n### ERROR:  Failed mounting SqApp. file:  $SqF\n\" && exit
   cd $MntPt
   /$pkgN.run &
fi
' > $mntPT/SqApp/bin/$pkgN
Back to top
View user's profile Send private message 
PANZERKOPF

Joined: 16 Dec 2009
Posts: 279
Location: Earth

PostPosted: Sat 26 May 2012, 18:23    Post subject: Re: Having a problem writing script string to script file.  

sunburnt wrote:
I need my app. to write a script file, but the shell always runs. it.
I need $SQ_EXT, $MENU, $pkgN evaluated, all other variables as literals.

Maybe "one string - one echo" is better solution?
echo '#!/bin/sh' > $mntPT/SqApp/bin/$pkgN
echo "MntPt=/SqApp/mnt/${pkgN}" >> $mntPT/SqApp/bin/$pkgN
echo "SqF=\`basename $MntPt\`.${SQ_EXT}" >> $mntPT/SqApp/bin/$pkgN
...........................
...........................

_________________
SUUM CUIQUE.
Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 4979
Location: Arizona, U.S.A.

PostPosted: Sun 27 May 2012, 00:12    Post subject:  

Thanks PANZERKOPF; That`s how I`ve done it in the past.
I`d rather not preform multiple writes ( or reads ) to the H.D. or any media.

I`m seeing the {} are helping...
Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 4979
Location: Arizona, U.S.A.

PostPosted: Sun 27 May 2012, 03:26    Post subject:  

Found a working solution, I seem to recall it was quite a struggle before.
If the evaluated variables are at the beginning or end of the string, it`s good.
Code:
exePF=$mntPT/SqApp/bin/$pkgN            # make exec file "mnt/umnt"
echo -e '#!/bin/sh\n\nMntPt=/SqApp/mnt/'"$pkgN\nSqF=$pkgN.$SQ_EXT\n" > $exePF
echo 'if [ "$1" = -m ];then echo '$MENU >> $exePF
echo 'elif [ "$1" = -x ];then umount $MntPt
  [ $? -gt 0 ]&& echo -e "\n### ERROR:  Failed SqApp unmount:  $SqF\n"
else
  [ ! -f $SqF ]&& echo -e "\n### ERROR:  No SqApp file:  $SqF\n" && exit
  if [ ! "`df |grep $MntPt`" ];then
    mount -o loop /SqApp/bin/$SqF $MntPt
    [ $? -gt 0 ]&& echo -e "\n### ERROR:  Failed SqApp mount:  $SqF\n" && exit
  fi
  cd $MntPt
  '"$pkgN.run &
fi" >> $exePF
chmod a=rx $exePF
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4134

PostPosted: Sun 27 May 2012, 16:06    Post subject:  

there is a little trick that you can do

set up an executable script called tmp.sh (or whatever) with just this:

Code:
#!/bin/sh
. /tmp/tmpscript
#add rm -f /tmp/tmpscript here if you want a 1 time run


then all you need to do is echo/printf your commands >/tmp/tmpscript

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 4979
Location: Arizona, U.S.A.

PostPosted: Sun 27 May 2012, 17:25    Post subject:  

Hi again technosaurus; Forgive my slowness but I`m puzzled.

tmp.sh sources tmpscript that the code is being written to.
But I don`t see how this solves Bash evaluating the code instead of writing it?

Should be able to intersperse literal and evaluate code in a string.
Like:
Code:
echo '`basename $File`'

$File should be evaluated because it won`t be available in the final script.
But the command `basename ` needs to be literally written to the script.
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4134

PostPosted: Sun 27 May 2012, 18:58    Post subject:  

You may want some double quotes too then, in case there are spaces, tabs or return characters in the filename
Code:
echo '`basename "'$File'"`' >>/tmp/tmpscript
BTW Yes, it is possible to have return characters in a filename.
_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
jamesbond

Joined: 26 Feb 2007
Posts: 1875
Location: The Blue Marble

PostPosted: Sun 27 May 2012, 21:24    Post subject:  

I usually just do this:

Code:
#!/bin/sh
#this is the generating script.

PARAM=aaa
echo generating script now

cat > /tmp/the-script.sh << EOF
#!/bin/sh
# this is the generated script
echo generated script now running
echo parameter passed to me is \$1
echo variables set during creation is $PARAM
EOF

echo done, executing generated script now
chmod +x /tmp/the-script.sh
/tmp/the-script.sh bbb

The only quoting you need to do is $, everything else is unquoted.

EDIT: Terry - the example I gave you was wrong. It is corrected and expanded now. You need to quote $ if you want you use the as variables, otherwise they are expanded during creation.

_________________
Fatdog64, Slacko and Puppeee user. Puppy user since 2.13

Last edited by jamesbond on Mon 28 May 2012, 03:52; edited 1 time in total
Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 4979
Location: Arizona, U.S.A.

PostPosted: Mon 28 May 2012, 01:57    Post subject:  

technosaurus; So even basename needs the "" to preserve the formatting...
You never know if it`s like an echo or assignment like.

jamesbond; I`ll try escaping the $, I didn`t think of using that here.
Back to top
View user's profile Send private message 
jamesbond

Joined: 26 Feb 2007
Posts: 1875
Location: The Blue Marble

PostPosted: Mon 28 May 2012, 03:53    Post subject:  

Corrected example above.
_________________
Fatdog64, Slacko and Puppeee user. Puppy user since 2.13
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4134

PostPosted: Mon 28 May 2012, 12:53    Post subject:  

you always have to assume users do dumb crap like naming binaries in sentence syntax with full descriptions (very common for mp3 files, but it is still possible for others) ... for instance, this is an acceptable file name

/usr/bin/Abiword - word processor
supports docx,pdf,svg,abw,odf,odt,wps,...


if you put echo 'text $1 here' >file, the file literally gets text $1 here, so you would have to pass that parameter to it.
but echo 'text '$1' here' >file would put... text the value of first parameter here (note that "the value of first parameter" would become 5 different words here, thus the need for 'text "'$1'" here' to make it one word)

single quotes don't evaluate variables inside, but double qoutes do
in fact everything in single quotes are just evaluated as characters (including double quotes)

just play with it a bit, it will make more sense.

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 4979
Location: Arizona, U.S.A.

PostPosted: Mon 28 May 2012, 14:41    Post subject:  

Thanks jamesbond; I`ve seen this type of string redirecting before.
Code:
sh-4.1# A=/123/azx
sh-4.1#
sh-4.1# echo qwe\$A
qwe$A
sh-4.1#
sh-4.1# echo qwe\`basename $A\`
qwe`basename /123/azx`
sh-4.1#
sh-4.1# echo qwe\`basename \$A\`
qwe`basename $A`
sh-4.1#
sh-4.1# echo qwe\$(basename $A)
sh: syntax error near unexpected token `('
sh-4.1#
sh-4.1# echo qwe$\(basename $A\)
qwe$(basename /123/azx)

As you can see... It seems to work wonderfully for simple usage.
It doesn`t work as \$(...) , only $\(...\) just like \`...\`

technosaurus; Yeah, that`s what I was trying to do with my long string.
'piece it '"$together"' with quotes and '`commands`
For this use, menu item names will have spaces, but convert to _ for files.

Note: I`m having better luck compiling thanks to all your help. More Qs...
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [12 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0675s ][ Queries: 11 (0.0046s) ][ GZIP on ]