<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: pass function as an argument in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/pass-function-as-an-argument/m-p/3762609#M101845</link>
    <description>I still want to learn more...&lt;BR /&gt;&lt;BR /&gt;typeset -i ISTAT=0&lt;BR /&gt;#define a variable ISTATE right?&lt;BR /&gt;func=${1}&lt;BR /&gt;#what does it mean?&lt;BR /&gt;&lt;BR /&gt;shift&lt;BR /&gt;${func}&lt;BR /&gt;#func is now variable right?&lt;BR /&gt;ISTAT=${?}&lt;BR /&gt;return ${ISTAT}&lt;BR /&gt;} # f5&lt;BR /&gt;</description>
    <pubDate>Thu, 30 Mar 2006 18:15:12 GMT</pubDate>
    <dc:creator>Gemini_2</dc:creator>
    <dc:date>2006-03-30T18:15:12Z</dc:date>
    <item>
      <title>pass function as an argument</title>
      <link>https://community.hpe.com/t5/operating-system-linux/pass-function-as-an-argument/m-p/3762604#M101840</link>
      <description>I have 4 functions, I want to know if I can consolidate to 3 by passing the funtion as an argument.&lt;BR /&gt;&lt;BR /&gt;function f1 {&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;function f2 {&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;function f3 {&lt;BR /&gt;for i in $files&lt;BR /&gt;do&lt;BR /&gt;  f1&lt;BR /&gt;done&lt;BR /&gt;}   &lt;BR /&gt;function f4 {&lt;BR /&gt;for i in $files&lt;BR /&gt;do&lt;BR /&gt;  f2&lt;BR /&gt;done&lt;BR /&gt;}   &lt;BR /&gt;&lt;BR /&gt;as you can tell, f3 and f4 are just the loop, the only difference is the function...&lt;BR /&gt;&lt;BR /&gt;I want to consolidate f3+4 to f5&lt;BR /&gt;&lt;BR /&gt;function f5 {&lt;BR /&gt;function = $1&lt;BR /&gt;for i in $files&lt;BR /&gt;do&lt;BR /&gt;  function&lt;BR /&gt;done&lt;BR /&gt;}   &lt;BR /&gt;&lt;BR /&gt;in my main program, I can then do&lt;BR /&gt;&lt;BR /&gt;f5 f1&lt;BR /&gt;f5 f2&lt;BR /&gt;&lt;BR /&gt;is that possible?</description>
      <pubDate>Thu, 30 Mar 2006 17:27:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/pass-function-as-an-argument/m-p/3762604#M101840</guid>
      <dc:creator>Gemini_2</dc:creator>
      <dc:date>2006-03-30T17:27:54Z</dc:date>
    </item>
    <item>
      <title>Re: pass function as an argument</title>
      <link>https://community.hpe.com/t5/operating-system-linux/pass-function-as-an-argument/m-p/3762605#M101841</link>
      <description>You can pass the consolidated function some sort of a flag that dictates what the function should do after looking at its value; something like...&lt;BR /&gt;&lt;BR /&gt;function f5 {&lt;BR /&gt;flag=$1&lt;BR /&gt;for i in $files&lt;BR /&gt;do&lt;BR /&gt;if [ $flag = "f1" ]; then&lt;BR /&gt;&lt;COMMANDS&gt;&lt;BR /&gt;elif [ $flag = "f2"]; then&lt;BR /&gt;&lt;COMMANDS&gt;&lt;BR /&gt;fi&lt;BR /&gt;done&lt;BR /&gt;} &lt;BR /&gt;&lt;BR /&gt;f5 arg&lt;BR /&gt;&lt;BR /&gt;hope it helps!&lt;/COMMANDS&gt;&lt;/COMMANDS&gt;</description>
      <pubDate>Thu, 30 Mar 2006 17:44:52 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/pass-function-as-an-argument/m-p/3762605#M101841</guid>
      <dc:creator>Sandman!</dc:creator>
      <dc:date>2006-03-30T17:44:52Z</dc:date>
    </item>
    <item>
      <title>Re: pass function as an argument</title>
      <link>https://community.hpe.com/t5/operating-system-linux/pass-function-as-an-argument/m-p/3762606#M101842</link>
      <description>Yes although your syntax isn't quite rigorous:&lt;BR /&gt;I'll do a simplifed version:&lt;BR /&gt;&lt;BR /&gt;function f1 {&lt;BR /&gt;  echo "Function f1"&lt;BR /&gt;  return 1&lt;BR /&gt;} # f1&lt;BR /&gt;&lt;BR /&gt;function f2 {&lt;BR /&gt;  echo "Function f2"&lt;BR /&gt;  return 2&lt;BR /&gt;} # f2&lt;BR /&gt;&lt;BR /&gt;function f5 {&lt;BR /&gt;  typeset -i ISTAT=0&lt;BR /&gt;  func=${1}&lt;BR /&gt;  shift&lt;BR /&gt;  ${func}&lt;BR /&gt;  ISTAT=${?}&lt;BR /&gt;  return ${ISTAT}&lt;BR /&gt;} # f5&lt;BR /&gt;&lt;BR /&gt;f5 f1&lt;BR /&gt;f5 f2&lt;BR /&gt;</description>
      <pubDate>Thu, 30 Mar 2006 17:46:13 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/pass-function-as-an-argument/m-p/3762606#M101842</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2006-03-30T17:46:13Z</dc:date>
    </item>
    <item>
      <title>Re: pass function as an argument</title>
      <link>https://community.hpe.com/t5/operating-system-linux/pass-function-as-an-argument/m-p/3762607#M101843</link>
      <description>yes, that is my alternative. I guess that I just want to know how flexible shell is..</description>
      <pubDate>Thu, 30 Mar 2006 17:48:36 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/pass-function-as-an-argument/m-p/3762607#M101843</guid>
      <dc:creator>Gemini_2</dc:creator>
      <dc:date>2006-03-30T17:48:36Z</dc:date>
    </item>
    <item>
      <title>Re: pass function as an argument</title>
      <link>https://community.hpe.com/t5/operating-system-linux/pass-function-as-an-argument/m-p/3762608#M101844</link>
      <description>that is not straightforward...&lt;BR /&gt;&lt;BR /&gt;but, thank you...I am glad that it can be done!&lt;BR /&gt;</description>
      <pubDate>Thu, 30 Mar 2006 18:02:29 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/pass-function-as-an-argument/m-p/3762608#M101844</guid>
      <dc:creator>Gemini_2</dc:creator>
      <dc:date>2006-03-30T18:02:29Z</dc:date>
    </item>
    <item>
      <title>Re: pass function as an argument</title>
      <link>https://community.hpe.com/t5/operating-system-linux/pass-function-as-an-argument/m-p/3762609#M101845</link>
      <description>I still want to learn more...&lt;BR /&gt;&lt;BR /&gt;typeset -i ISTAT=0&lt;BR /&gt;#define a variable ISTATE right?&lt;BR /&gt;func=${1}&lt;BR /&gt;#what does it mean?&lt;BR /&gt;&lt;BR /&gt;shift&lt;BR /&gt;${func}&lt;BR /&gt;#func is now variable right?&lt;BR /&gt;ISTAT=${?}&lt;BR /&gt;return ${ISTAT}&lt;BR /&gt;} # f5&lt;BR /&gt;</description>
      <pubDate>Thu, 30 Mar 2006 18:15:12 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/pass-function-as-an-argument/m-p/3762609#M101845</guid>
      <dc:creator>Gemini_2</dc:creator>
      <dc:date>2006-03-30T18:15:12Z</dc:date>
    </item>
    <item>
      <title>Re: pass function as an argument</title>
      <link>https://community.hpe.com/t5/operating-system-linux/pass-function-as-an-argument/m-p/3762610#M101846</link>
      <description>More than anything, it means that I am a disciplined shell programmer. That's why you will always see my variables enclosed in {}'s.&lt;BR /&gt;&lt;BR /&gt;typeset -i ISTAT=0 #declare ISTAT to be an integer (-i) variable and local to this function so that if there is another ISTAT elsewhere it does not interfere with this ISTAT&lt;BR /&gt;&lt;BR /&gt;func=${1} #&lt;BR /&gt;and better still:&lt;BR /&gt;typeset func=${1} &lt;BR /&gt;#what does it mean? &lt;BR /&gt;&lt;BR /&gt;It means that I taking the 1st parameter and turning it into a local variable eventgough it's the name of a function.&lt;BR /&gt;&lt;BR /&gt;shift # clean up the parameter stack&lt;BR /&gt;${func}&lt;BR /&gt;#func is now variable right? Yes so that &lt;BR /&gt;${func} is now "f1" for example.&lt;BR /&gt;ISTAT=${?} # get the exit status of the last command and store it in ISTAT&lt;BR /&gt;return ${ISTAT} # returns a valid status to the calling routine&lt;BR /&gt;} # f5&lt;BR /&gt;&lt;BR /&gt;Much of this (especially typeset) is documented in man sh-posix.</description>
      <pubDate>Thu, 30 Mar 2006 18:25:04 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/pass-function-as-an-argument/m-p/3762610#M101846</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2006-03-30T18:25:04Z</dc:date>
    </item>
  </channel>
</rss>

