Having a problem writing script string to script file.

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

Having a problem writing script string to script file.

#1 Post by sunburnt »

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: Select all

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

PANZERKOPF
Posts: 282
Joined: Wed 16 Dec 2009, 21:38
Location: Earth

Re: Having a problem writing script string to script file.

#2 Post by PANZERKOPF »

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.

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#3 Post by sunburnt »

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...

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#4 Post by sunburnt »

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: Select all

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

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#5 Post by technosaurus »

there is a little trick that you can do

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

Code: Select all

#!/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
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#6 Post by sunburnt »

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: Select all

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.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#7 Post by technosaurus »

You may want some double quotes too then, in case there are spaces, tabs or return characters in the filename

Code: Select all

echo '`basename "'$File'"`' >>/tmp/tmpscript
BTW Yes, it is possible to have return characters in a filename.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

jamesbond
Posts: 3433
Joined: Mon 26 Feb 2007, 05:02
Location: The Blue Marble

#8 Post by jamesbond »

I usually just do this:

Code: Select all

#!/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.
Last edited by jamesbond on Mon 28 May 2012, 07:52, edited 1 time in total.
Fatdog64 forum links: [url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Latest version[/url] | [url=https://cutt.ly/ke8sn5H]Contributed packages[/url] | [url=https://cutt.ly/se8scrb]ISO builder[/url]

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#9 Post by sunburnt »

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.

jamesbond
Posts: 3433
Joined: Mon 26 Feb 2007, 05:02
Location: The Blue Marble

#10 Post by jamesbond »

Corrected example above.
Fatdog64 forum links: [url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Latest version[/url] | [url=https://cutt.ly/ke8sn5H]Contributed packages[/url] | [url=https://cutt.ly/se8scrb]ISO builder[/url]

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#11 Post by technosaurus »

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.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#12 Post by sunburnt »

Thanks jamesbond; I`ve seen this type of string redirecting before.

Code: Select all

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...

Post Reply