Variable inside a function? [SOLVED]

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
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

Variable inside a function? [SOLVED]

#1 Post by Argolance »

Bonjour,
How to make the variable "DEF_APP" to work inside this function?
Example:

Code: Select all

#!/bin/sh

export DEF_APP="myapp"

func_defaultapp () {

	Xdialog --title "$(gettext 'Installation') - $(gettext 'Information')" icon /usr/local/lib/X11/pixmaps/question.png --stdout --yesno "$(gettext 'Do you want this application to be set as default?')" 0 0

	case $? in
		0)echo '#!/bin/sh
		
exec $DEF_APP "$@"' > /root/defaultapptest
		;;
		1)Xdialog --title "$(gettext 'Installation') - $(gettext 'Information')" --no-buttons --infobox "$(gettext 'Canceled...')" 3 50
		;;
		255)Xdialog --title "$(gettext 'Installation') - $(gettext 'Information')" --no-buttons --infobox "$(gettext 'Canceled...')" 3 50
		;;
	esac
}
export -f func_defaultapp

func_default
I get:

Code: Select all

#!/bin/sh
		
exec $DEF_APP "$@"
And would like to get:

Code: Select all

#!/bin/sh
		
exec myapp "$@"
Ouch! The variables... :evil: :?
Thank you!

Cordialement.
Last edited by Argolance on Wed 20 Mar 2013, 13:52, edited 1 time in total.

User avatar
SFR
Posts: 1800
Joined: Wed 26 Oct 2011, 21:52

#2 Post by SFR »

Single quotes prevent variables from being expanded:

Code: Select all

# var=123; echo 'something $var blahblah'
something $var blahblah
# var=123; echo 'something '$var' blahblah'
something 123 blahblah
# 
This should work:

Code: Select all

exec '$DEF_APP' "$@"' > /root/defaultapptest 
Greetings!
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#3 Post by Argolance »

Hello, I found the (a) solution:

Code: Select all

#!/bin/bash

export DEF_APP="myapp"

func_defaultapp () {

	Xdialog --title "$(gettext 'Installation') - $(gettext 'Information')" icon /usr/local/lib/X11/pixmaps/question.png --stdout --yesno "$(gettext 'Do you want this application to be set as default?')" 0 0

	case $? in
		0)echo "#!/bin/sh
		
exec $DEF_APP \"\$@\""> /root/defaultapptest
		;;
		1)Xdialog --title "$(gettext 'Installation') - $(gettext 'Information')" --no-buttons --infobox "$(gettext 'Canceled...')" 3 50
		;;
		255)Xdialog --title "$(gettext 'Installation') - $(gettext 'Information')" --no-buttons --infobox "$(gettext 'Canceled...')" 3 50
		;;
	esac
}
export -f func_defaultapp

func_defaultapp
Question: This works but is this right?
Thanks.

Cordialement.
Last edited by Argolance on Wed 20 Mar 2013, 13:52, edited 1 time in total.

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#4 Post by Argolance »

... This works (too) and seems to be more "kosher"! :wink:
Thanks.

Cordialement.

Post Reply