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 24 Jul 2014, 16:20
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Execute a function inside another in command line? [SOLVED]
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
Page 1 of 1 Posts_count  
Author Message
Argolance


Joined: 06 Jan 2008
Posts: 1803
Location: PORT-BRILLET (Mayenne - France)

PostPosted: Fri 01 Nov 2013, 12:32    Post_subject:  Execute a function inside another in command line? [SOLVED]  

Bonjour,
... All is in the title.
Code:
function 1 () {
[...]
    function 2 () {
    [...]
    } End of 2
 } End of 1


EDIT: my example was obviously wrong Confused :
Code:
} End of 2
 } End of 1
instead of:
Code:
{ End of 2
 { End of 1
Thank you!

Cordialement.

_________________


Edited_times_total
Back to top
View user's profile Send_private_message Visit_website 
amigo

Joined: 02 Apr 2007
Posts: 2217

PostPosted: Fri 01 Nov 2013, 14:03    Post_subject:  

Are you asking if that is possible?
Back to top
View user's profile Send_private_message 
sunburnt


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

PostPosted: Fri 01 Nov 2013, 16:30    Post_subject:  

Not that I know of, can only call one from the other.
Code:
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...
Back to top
View user's profile Send_private_message 
Ibidem

Joined: 25 May 2010
Posts: 463
Location: State of Jefferson

PostPosted: Sat 02 Nov 2013, 01:45    Post_subject:  

sunburnt wrote:
Not that I know of, can only call one from the other.
Code:
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:
$ 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.
Back to top
View user's profile Send_private_message 
sunburnt


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

PostPosted: Sat 02 Nov 2013, 13:16    Post_subject:  

Quote:
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.
.
Back to top
View user's profile Send_private_message 
Argolance


Joined: 06 Jan 2008
Posts: 1803
Location: PORT-BRILLET (Mayenne - France)

PostPosted: Sun 03 Nov 2013, 08:25    Post_subject:  

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

Cordialement.

_________________

Back to top
View user's profile Send_private_message Visit_website 
amigo

Joined: 02 Apr 2007
Posts: 2217

PostPosted: Sun 03 Nov 2013, 10:43    Post_subject:  

It's not that the function runs in a subshell. It's that variables inside functions have a local scope.
Back to top
View user's profile Send_private_message 
sunburnt


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

PostPosted: Sun 03 Nov 2013, 15:38    Post_subject:  

That I understand... But obviously a loop does not run in the same shell.
Apparently functions do run in the same shell.
Code:
# func() { a=1 ; } ; func ; echo $a
1
# func() { b=2 ; }
# func
# echo $b
2
Back to top
View user's profile Send_private_message 
Argolance


Joined: 06 Jan 2008
Posts: 1803
Location: PORT-BRILLET (Mayenne - France)

PostPosted: Mon 04 Nov 2013, 06:00    Post_subject:  

Hello,
It is the way I initially should have presented the question:
Code:
# 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:

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


Cordialement.

_________________

Back to top
View user's profile Send_private_message Visit_website 
Keef


Joined: 20 Dec 2007
Posts: 617
Location: Staffordshire

PostPosted: Mon 04 Nov 2013, 08:26    Post_subject:  

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:

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


Or:
Code:
#   func1 () {
> a=1
> func2 () {
> b=2
> echo $b
> }
> }
# func2
2
#
Back to top
View user's profile Send_private_message 
amigo

Joined: 02 Apr 2007
Posts: 2217

PostPosted: Mon 04 Nov 2013, 09:54    Post_subject:  

"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.
Back to top
View user's profile Send_private_message 
Argolance


Joined: 06 Jan 2008
Posts: 1803
Location: PORT-BRILLET (Mayenne - France)

PostPosted: Mon 04 Nov 2013, 11:22    Post_subject:  

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!
Quote:
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:
# 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? Embarassed
Sorry, I am not an expert and my way to present and understand things is probably a little bit silly Confused

Cordialement.

_________________


Edited_time_total
Back to top
View user's profile Send_private_message Visit_website 
Karl Godt


Joined: 20 Jun 2010
Posts: 3953
Location: Kiel,Germany

PostPosted: Wed 06 Nov 2013, 14:04    Post_subject:  

help getopt

Code:
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 ?
Back to top
View user's profile Send_private_message Visit_website 
sunburnt


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

PostPosted: Wed 06 Nov 2013, 15:34    Post_subject:  

To call any script function using cli arguments, put this at the bottom.
Code:
[ "$1" ]&& $@

But you probably already know this...
Back to top
View user's profile Send_private_message 
Display_posts:   Sort by:   
Page 1 of 1 Posts_count  
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Off-Topic Area » Programming
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1123s ][ Queries: 12 (0.0110s) ][ GZIP on ]