Page 1 of 1

Having a problem writing script string to script file.

Posted: Sat 26 May 2012, 19:49
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

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

Posted: Sat 26 May 2012, 22:23
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
...........................
...........................

Posted: Sun 27 May 2012, 04:12
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...

Posted: Sun 27 May 2012, 07:26
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

Posted: Sun 27 May 2012, 20:06
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

Posted: Sun 27 May 2012, 21:25
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.

Posted: Sun 27 May 2012, 22:58
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.

Posted: Mon 28 May 2012, 01:24
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.

Posted: Mon 28 May 2012, 05:57
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.

Posted: Mon 28 May 2012, 07:53
by jamesbond
Corrected example above.

Posted: Mon 28 May 2012, 16:53
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.

Posted: Mon 28 May 2012, 18:41
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...