Execute a function inside another in command line? [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:

Execute a function inside another in command line? [SOLVED]

#1 Post by Argolance »

Bonjour,
... All is in the title.

Code: Select all

function 1 () {
[...]
    function 2 () {
    [...]
    } End of 2
 } End of 1
EDIT: my example was obviously wrong :? :

Code: Select all

} End of 2
 } End of 1
instead of:

Code: Select all

{ End of 2
 { End of 1
Thank you!

Cordialement.
Last edited by Argolance on Sun 03 Nov 2013, 12:26, edited 2 times in total.

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#2 Post by amigo »

Are you asking if that is possible?

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

#3 Post by sunburnt »

Not that I know of, can only call one from the other.

Code: Select all

func2() { echo "$1 World" ; }
func1() { echo `func2 Hello` ; }
ret=`func1`
echo $ret
Shell functions and loops are a royal pain.
You can only export into them and echo back out of them.
Now if Bash had an option to run functions and loops in the same shell...

Ibidem
Posts: 549
Joined: Wed 26 May 2010, 03:31
Location: State of Jefferson

#4 Post by Ibidem »

sunburnt wrote:Not that I know of, can only call one from the other.

Code: Select all

func2() { echo "$1 World" ; }
func1() { echo `func2 Hello` ; }
ret=`func1`
echo $ret
Shell functions and loops are a royal pain.
You can only export into them and echo back out of them.
Now if Bash had an option to run functions and loops in the same shell...

Code: Select all

$ while [ -z "$I" ]; do export I="true"; done
$ echo $I
true
$ testfunc() {
export I="$1"
}
$ testfunc garbaage
$ echo $I
garbaage
$ testfunc1() {                 
testfunc2() {
echo "Testing $2"
}
testfunc2 "$1"
}
$ testfunc1 abc def
Testing 
Huh?
Using () instead of {} gives you a subshell. So does any sort of pipe or "&".

I realize that the nested function should have $1/$2 reversed.

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

#5 Post by sunburnt »

Huh?
Using () instead of {} gives you a subshell. So does any sort of pipe or "&".
So how do you get a function or loop to run in the same shell.?
Examples of variables made inside of them that are accessible from the outside.
Without using "echo".? Thanks Ibidem.
.

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

#6 Post by Argolance »

Bonjour,
sunburnt wrote:Shell functions and loops are a royal pain.
Yes indeed! :oops:
Thanks guys!

Cordialement.

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#7 Post by amigo »

It's not that the function runs in a subshell. It's that variables inside functions have a local scope.

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

#8 Post by sunburnt »

That I understand... But obviously a loop does not run in the same shell.
Apparently functions do run in the same shell.

Code: Select all

# func() { a=1 ; } ; func ; echo $a
1
# func() { b=2 ; }
# func
# echo $b
2

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

#9 Post by Argolance »

Hello,
It is the way I initially should have presented the question:

Code: Select all

# func1 () {
> a=1
>     func2 () {
>     b=2
>     }
>  }
# func1
# echo $a
1
# func2
# echo $b
2
How to get b value in command line without executing the whole function but just the required part of it?

Code: Select all

# func1 () {
> a=1
>     func2 () {
>     b=2
>     }
>  }
# func2
bash: func2: command not found
Cordialement.

User avatar
Keef
Posts: 987
Joined: Thu 20 Dec 2007, 22:12
Location: Staffordshire

#10 Post by Keef »

I'm probably misunderstanding the question, but is the problem caused by the fact that the function itself does nothing apart from assign the variable (b=2). Calling the function is not going to produce any output, because it isn't set up to do that. This works though:

Code: Select all

#  func1 () {  a=1; echo $a; func2 () { b=2; echo $b;}; }
#
# func2
2
Or:

Code: Select all

#   func1 () {
> a=1
> func2 () {
> b=2
> echo $b
> }
> }
# func2
2
#

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#11 Post by amigo »

"How to get b value in command line without executing the whole function but just the required part of it?"

Don't nest the functions then.

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

#12 Post by Argolance »

Bonjour,
amigo wrote:Don't nest the functions then.
I want to get values from functions I created inside an existing script I didn't create myself!
I'm probably misunderstanding the question, but is the problem caused by the fact that the function itself does nothing apart from assign the variable (b=2).
No, this was just a simplified example to make you understand what I am expecting for. The original function is much more complicated in reality but it should work by following the same principles?

Code: Select all

# Function tray app
function RUN_NOTIFICATION_TRAY () {
	REFRESH_TEXT="$(gettext 'Refresh updates list')"  ## refresh tray app text
	REFRESH_ICON="/usr/local/lib/X11/mini-icons/gtk-refresh.png"
	LOG_TEXT="$(gettext 'Log/Configuration files')"  ## refresh tray app text
	LOG_ICON="/usr/local/lib/X11/mini-icons/gtk-file.png"
	CLEAR_TEXT="$(gettext 'Clear log files')"  ## clear log files text
	CLEAR_ICON="/usr/local/lib/X11/mini-icons/gtk-clear.png"
	ABOUT_TEXT="$(gettext 'About')"  ## about text
	ABOUT_ICON="/usr/local/lib/X11/mini-icons/gtk-about.png"
	HELP_TEXT="$(gettext 'Help')"  ## help text
	HELP_ICON="/usr/local/lib/X11/mini-icons/gtk-help.png"
	QUIT_TEXT="$(gettext 'Quit')"  ## exit tray app text
	QUIT_ICON="/usr/local/lib/X11/mini-icons/gtk-quit.png"
	PIPE_FIFO=$(mktemp -u $TEMP_DIR/update.XXXXXXXX)
	
	## Function about
	function about(){
export ABOUT="
<window decorated="false" skip_taskbar_hint="true">
<vbox border-width="10">	
	<hbox>	
		<button can-focus="no" relief="2"><input file stock="gtk-close"></input></button>
	</hbox>
	<frame  2POSou $APP_VERSION - $(gettext 'About') >
		<pixmap><input file>/usr/share/pixmaps/2POSou.png</input></pixmap>
		<text wrap="false" use-markup="true"><label>" <b><span size='large'>$(gettext 'Operating System Online Updater')</span></b>

$(eval_gettext '<i>(Argolance, adapted for $OS from POU-0.0.2 by ASRI
Original script by RSH for LazY Puppy - October 2013)</i>

<b>Localization:</b> <i>Argolance</i>

<b>Translations:</b>
')"</label></text>
		<text use-markup="true" wrap="false" xalign="0">
		<input file>$APP_DIR/translators</input>
		</text>
	</frame>
	<frame  $(gettext 'Links') >
<hbox>
		<text><label>" $(gettext 'Report bugs and suggest improvements:') "</label></text>
		<button tooltip-text=" $(eval_gettext 'If you have any questions or suggestions, or if you encountered a bug, please post a message on the $OS support Forum') "><input file>/usr/local/lib/X11/pixmaps/www24.png</input>
			<action>defaultbrowser $FORUM &</action>
			<action>exit:ok</action>
		</button>
</hbox>
<hbox>
		<text><label>" $(eval_gettext '$OS Web Site:') "</label></text>
		<button tooltip-text=" $(eval_gettext 'Stay up to date with the latest news and programs published on the $OS website') "><input file>/usr/share/pixmaps/tooppy_logo.png</input>
			<action>defaultbrowser $WEB_SITE &</action>
			<action>exit:ok</action>
		</button>
</hbox>
	</frame>
</vbox>
</window>
"

gtkdialog4 --program=ABOUT --center
	} # End of function


	## Function default help file if locale help file doesn't exist
	function help(){
		if [[ ! -f $APP_DIR/help_files/2POSou_help_$lng2.html ]]; then
		$BROWSER $APP_DIR/help_files/2POSou_help_en.html
		else
		$BROWSER $APP_DIR/help_files/2POSou_help_$lng2.html
		fi
	exit	
	} # End of function


	## Function - Clear the history
	function CLEAR_HISTORY (){
	Xdialog --title "$(gettext 'Question')" --icon /usr/local/lib/X11/pixmaps/question.png --stdout --yesno "$(gettext 'Do you really want to clear log files?')" 0 100

	case $? in

		0)echo "`date "+%Y-%m-%d %H:%M:%S"`" - $(gettext 'Cleared by user') > $CONF_DIR/installed_updates_log
		  echo "`date "+%Y-%m-%d %H:%M:%S"`" - $(gettext 'Cleared by user') > $CONF_DIR/uninstalled_updates_log
		  echo "`date "+%Y-%m-%d %H:%M:%S"`" - $(gettext 'Cleared by user') > $CONF_DIR/downloaded_updates_log
		  echo "`date "+%Y-%m-%d %H:%M:%S"`" - $(gettext 'Cleared by user') > $CONF_DIR/excluded_updates_log
		  echo "`date "+%Y-%m-%d %H:%M:%S"`" - $(gettext 'Cleared by user') > $CONF_DIR/restored_updates_log
		  REFRESH_TRAY
		  Xdialog --title "$(gettext 'Information')" --icon /usr/local/lib/X11/pixmaps/info.png --left --no-buttons --infobox "$(gettext 'Log files cleared!')" 0 100
		 ;;
		 1)Xdialog --title "$(gettext 'Information')" --icon /usr/local/lib/X11/pixmaps/info.png --left --no-buttons --infobox "$(gettext 'Clear log files cancelled!')" 0 100
		 ;;
		 255)Xdialog --title "$(gettext 'Information')" --icon /usr/local/lib/X11/pixmaps/info.png --left --no-buttons --infobox "$(gettext 'Clear log files cancelled!')" 0 100
		 ;;

	esac
	} # End of function CLEAR_HISTORY
		
	## Function view log file
	function OpenLogFile(){
		$APP_DIR/log.sh &
	} # End of function view log file

	## close / quit menu
	function CLOSE_QUIT () {
		if	[ "`pidof 2POSou_GUI.sh`" ]; then
		kill $(ps ax | grep gtkdialog4 | grep PPOSou_GUI | awk '{print $1}' );
		rm -f $CONF_DIR/run
		fi
		echo "0" > $CONF_DIR/tab
		exec 3<> $PIPE_FIFO
		echo "quit" >&3
		rm -f $PIPE_FIFO
	}
	export -f CLOSE_QUIT
	
	## Refresh tray app
	function REFRESH_TRAY () {
		if	[ "`pidof 2POSou_GUI.sh`" ]; then
		kill $(ps ax | grep gtkdialog4 | grep PPOSou_GUI | awk '{print $1}' );
		echo -e '\c' > $CONF_DIR/run
		else
		rm -f $CONF_DIR/run
		fi
		$APP_DIR/AppRun &
		CLOSE_QUIT
	}
	export -f REFRESH_TRAY
	
	## 1 Create PIPE_FIFO file
	mkfifo $PIPE_FIFO
	export PIPE_FIFO
	
	## 2 Attach a filedescriptor to this PIPE_FIFO
	exec 3<> $PIPE_FIFO

	## 3 Run yad and tell it to read its stdin from the file descriptor
	
GUI=$(yad --notification --auto-kill --listen --no-middle \
--image="$TRAY_ICON" \
--text="$POPUP_TEXT" \
--command="bash -c LEFT_CLICK" <&3) & 

	## 4 generate right click menu
	echo "menu:$REFRESH_TEXT!bash -c REFRESH_TRAY!$REFRESH_ICON||$LOG_TEXT!bash -c OpenLogFile!$LOG_ICON|$CLEAR_TEXT!bash -c CLEAR_HISTORY!$CLEAR_ICON||$HELP_TEXT!bash -c help!$HELP_ICON|$ABOUT_TEXT!bash -c about!$ABOUT_ICON||$QUIT_TEXT!bash -c CLOSE_QUIT!$QUIT_ICON" >&3
 
} # End RUN_NOTIFICATION_TRAY
This is the real one. "help" and "about" functions work normally when called from the notification icon tray, but I would like to be able to call them using an external command too.
Is it clearer? :oops:
Sorry, I am not an expert and my way to present and understand things is probably a little bit silly :?

Cordialement.
Last edited by Argolance on Wed 06 Nov 2013, 21:45, edited 1 time in total.

User avatar
Karl Godt
Posts: 4199
Joined: Sun 20 Jun 2010, 13:52
Location: Kiel,Germany

#13 Post by Karl Godt »

help getopt

Code: Select all

while getopt ahv option ;do
case $option in
a) #about
about
;;
h) #help
[ .. ]
*) #do nothing
:
# do something
#echo choke me
#exit $quirk
;;
esac
done
something like this ?

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

#14 Post by sunburnt »

To call any script function using cli arguments, put this at the bottom.

Code: Select all

[ "$1" ]&& $@
But you probably already know this...

Post Reply