You are not logged in.

#1 2012-07-30 05:52:39

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,595
Website

Possible to use /usr/bin/watch to call a function [SOLVED]

I want to have a script both define functions and have the ability to run an external program calling one of them.  This is the simplified construct:

#!/bin/bash
foo() {
  echo "this is foo"
}

bar() {
  echo "this is bar"
}

case "$1" in
  one)
    foo
    ;;
  two)
    export bar
    /usr/bin/watch bar
    ;;
  *)
    echo $0 "{one|two}"
    exit 0
    ;;
esac

So if I invoke /path/to/script one the foo function runs and it exits as expected.  I want to ability to run the bar function as if it were a stand-alone program via `/usr/bin/watch bar` but this doesn't work as expected.

$ /path/to/script two
Every 2.0s: bar                                                                         Mon Jul 30 01:52:15 2012

sh: bar: command not found

How can I accomplish it?

Last edited by graysky (2012-07-30 07:16:56)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#2 2012-07-30 06:18:48

halhen
Member
From: Gothenburg, Sweden
Registered: 2009-04-08
Posts: 56
Website

Re: Possible to use /usr/bin/watch to call a function [SOLVED]

export -f bar

statnot - status text manager and notification-daemon for dwm, wmii and the like
shic - SHellscript Irc Client

Offline

#3 2012-07-30 06:29:34

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,595
Website

Re: Possible to use /usr/bin/watch to call a function [SOLVED]

Thanks halhen!

...now why didn't the man page for export show me that?  I do see it here but not from a `man export` hmm


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#4 2012-07-30 06:38:54

halhen
Member
From: Gothenburg, Sweden
Registered: 2009-04-08
Posts: 56
Website

Re: Possible to use /usr/bin/watch to call a function [SOLVED]

You're very welcome.

'-f' seems to not be POSIX standard, which is what export(1p) documents.


statnot - status text manager and notification-daemon for dwm, wmii and the like
shic - SHellscript Irc Client

Offline

#5 2012-07-30 09:15:16

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: Possible to use /usr/bin/watch to call a function [SOLVED]

To expand slightly on that, export(1p) comes from the man-page project, but the export command you're actually using is a bash built-in, and is therefore documented in the bash man page.

Offline

Board footer

Powered by FluxBB