<?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: Parameter checking in scripts in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976517#M99347</link>
    <description>Pete,&lt;BR /&gt;you could use the length of $1:&lt;BR /&gt;a=`expr length "$1"`&lt;BR /&gt;or &lt;BR /&gt;if [ -z "$1" ]</description>
    <pubDate>Tue, 02 May 2006 05:16:27 GMT</pubDate>
    <dc:creator>Peter Godron</dc:creator>
    <dc:date>2006-05-02T05:16:27Z</dc:date>
    <item>
      <title>Parameter checking in scripts</title>
      <link>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976516#M99346</link>
      <description>Hi,&lt;BR /&gt;I need to write a script which performes some action on files which are 4 weeks old (no parameters specified), unless another timeframe is specified (in parameter $1).&lt;BR /&gt;I can't figure out how to check if $1 is specified or not.&lt;BR /&gt;Any help appreciated.&lt;BR /&gt;Pete.</description>
      <pubDate>Tue, 02 May 2006 05:08:44 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976516#M99346</guid>
      <dc:creator>PJJ</dc:creator>
      <dc:date>2006-05-02T05:08:44Z</dc:date>
    </item>
    <item>
      <title>Re: Parameter checking in scripts</title>
      <link>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976517#M99347</link>
      <description>Pete,&lt;BR /&gt;you could use the length of $1:&lt;BR /&gt;a=`expr length "$1"`&lt;BR /&gt;or &lt;BR /&gt;if [ -z "$1" ]</description>
      <pubDate>Tue, 02 May 2006 05:16:27 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976517#M99347</guid>
      <dc:creator>Peter Godron</dc:creator>
      <dc:date>2006-05-02T05:16:27Z</dc:date>
    </item>
    <item>
      <title>Re: Parameter checking in scripts</title>
      <link>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976518#M99348</link>
      <description>Hi Peter&lt;BR /&gt;&lt;BR /&gt;see:&lt;BR /&gt;man ksh(1) Section "Parameter Substitution."&lt;BR /&gt; &lt;BR /&gt;&amp;lt;...&amp;gt;&lt;BR /&gt;           ${parameter:=word}  If parameter is not set or is null, set it to&lt;BR /&gt;                               word; then substitute the value of the&lt;BR /&gt;                               parameter.  Positional parameters cannot be&lt;BR /&gt;                               assigned in this way.&lt;BR /&gt;&amp;lt;...&amp;gt;&lt;BR /&gt;my be what you're searching for.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;rgds&lt;BR /&gt;HGH&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 02 May 2006 05:19:25 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976518#M99348</guid>
      <dc:creator>Hemmetter</dc:creator>
      <dc:date>2006-05-02T05:19:25Z</dc:date>
    </item>
    <item>
      <title>Re: Parameter checking in scripts</title>
      <link>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976519#M99349</link>
      <description>Hi Pete:&lt;BR /&gt;&lt;BR /&gt;One way is simply to check the number of arguments passed to the script:&lt;BR /&gt;&lt;BR /&gt;# cat .mysh&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;if [ $# -eq 0 ]; then&lt;BR /&gt;    echo "nothing passed"&lt;BR /&gt;else&lt;BR /&gt;    echo "I passed $@"&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;# ./mysh 123&lt;BR /&gt;I passed 123&lt;BR /&gt;&lt;BR /&gt;# ./mysh&lt;BR /&gt;nothing passed&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;</description>
      <pubDate>Tue, 02 May 2006 05:20:12 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976519#M99349</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-05-02T05:20:12Z</dc:date>
    </item>
    <item>
      <title>Re: Parameter checking in scripts</title>
      <link>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976520#M99350</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;set your script variable conditionally:&lt;BR /&gt;&lt;BR /&gt;...&lt;BR /&gt;timespec=${1:-28}&lt;BR /&gt;...&lt;BR /&gt;find .. -mtime +$timespec ...&lt;BR /&gt;&lt;BR /&gt;mfG Peter</description>
      <pubDate>Tue, 02 May 2006 05:56:16 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976520#M99350</guid>
      <dc:creator>Peter Nikitka</dc:creator>
      <dc:date>2006-05-02T05:56:16Z</dc:date>
    </item>
    <item>
      <title>Re: Parameter checking in scripts</title>
      <link>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976521#M99351</link>
      <description>Thanks all for the quick replies.&lt;BR /&gt;James's solution seems elegant. But when I run this script with parameters it gives correct output. Running a 2nd time without parameters it gives the same output as the 1st run.&lt;BR /&gt;Somehow it seems to conserve the parameters???</description>
      <pubDate>Tue, 02 May 2006 06:14:42 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976521#M99351</guid>
      <dc:creator>PJJ</dc:creator>
      <dc:date>2006-05-02T06:14:42Z</dc:date>
    </item>
    <item>
      <title>Re: Parameter checking in scripts</title>
      <link>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976522#M99352</link>
      <description>Sorry, I shpuld have supplied output:&lt;BR /&gt;$ . script.sh a b c&lt;BR /&gt;I passed a b c&lt;BR /&gt;$ . script.sh&lt;BR /&gt;I passed a b c&lt;BR /&gt;</description>
      <pubDate>Tue, 02 May 2006 06:17:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976522#M99352</guid>
      <dc:creator>PJJ</dc:creator>
      <dc:date>2006-05-02T06:17:38Z</dc:date>
    </item>
    <item>
      <title>Re: Parameter checking in scripts</title>
      <link>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976523#M99353</link>
      <description>Hi (again) Pete:&lt;BR /&gt;&lt;BR /&gt;Well, that's quite impossible for the script to retain state between executions.  I suspect that your terminal settings are to blame.  I suspect that you ran the script with arguments; recalled the command line; and "backspaced" over the arguments.  If the backspace truly didn't erase, then you would see the behavior you did.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Tue, 02 May 2006 06:35:28 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976523#M99353</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-05-02T06:35:28Z</dc:date>
    </item>
    <item>
      <title>Re: Parameter checking in scripts</title>
      <link>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976524#M99354</link>
      <description>Hi James,&lt;BR /&gt;&lt;BR /&gt;I didn't recall... Is it the way I call the script?&lt;BR /&gt;$ ./script.sh&lt;BR /&gt;nothing passed&lt;BR /&gt;$ ./script.sh a b c&lt;BR /&gt;I passed a b c&lt;BR /&gt;$ ./script.sh&lt;BR /&gt;nothing passed&lt;BR /&gt;$ . script.sh&lt;BR /&gt;I passed a b c&lt;BR /&gt;$ . script.sh d e f&lt;BR /&gt;I passed d e f&lt;BR /&gt;$ . script.sh&lt;BR /&gt;I passed d e f&lt;BR /&gt;&lt;BR /&gt;This last run not recalled!</description>
      <pubDate>Tue, 02 May 2006 06:41:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976524#M99354</guid>
      <dc:creator>PJJ</dc:creator>
      <dc:date>2006-05-02T06:41:38Z</dc:date>
    </item>
    <item>
      <title>Re: Parameter checking in scripts</title>
      <link>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976525#M99355</link>
      <description>Peter, &lt;BR /&gt;&lt;BR /&gt;Yes your are 'sourcing' the script instead of simply running it.&lt;BR /&gt;Is that deliberate? (why?)&lt;BR /&gt;What shell are you in while executing this?&lt;BR /&gt;Did you use the exact script as provided, or did you already augment it?&lt;BR /&gt;This does not reproduce for me.&lt;BR /&gt;&lt;BR /&gt;From the Posix shell man page: &lt;BR /&gt;"The dot (.) special command, as in . file, reads the entire file before any commands are executed. Therefore, alias and unalias commands in the file will not apply to any functions defined in the file."&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Groetjes,&lt;BR /&gt;Hein.&lt;BR /&gt;</description>
      <pubDate>Tue, 02 May 2006 06:59:40 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976525#M99355</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2006-05-02T06:59:40Z</dc:date>
    </item>
    <item>
      <title>Re: Parameter checking in scripts</title>
      <link>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976526#M99356</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;your error is using&lt;BR /&gt;. myscript&lt;BR /&gt;&lt;BR /&gt;This is executed directly in the running shell.&lt;BR /&gt;Check this:&lt;BR /&gt;. myscript a b c&lt;BR /&gt;&lt;BR /&gt;echo $1&lt;BR /&gt;a&lt;BR /&gt;&lt;BR /&gt;You have to call the script 'in normal way':&lt;BR /&gt;&lt;BR /&gt;myscript a b c&lt;BR /&gt;&lt;BR /&gt;without leading '. '.&lt;BR /&gt;&lt;BR /&gt;mfG Peter</description>
      <pubDate>Tue, 02 May 2006 07:00:49 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976526#M99356</guid>
      <dc:creator>Peter Nikitka</dc:creator>
      <dc:date>2006-05-02T07:00:49Z</dc:date>
    </item>
    <item>
      <title>Re: Parameter checking in scripts</title>
      <link>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976527#M99357</link>
      <description>Hi Pete:&lt;BR /&gt;&lt;BR /&gt;Both Hein and Peter N. are correct.  A dot character followed by a space followed by the script name means *read* or 'source' the file.&lt;BR /&gt;&lt;BR /&gt;This is used to bring environmental variables into your current environment (shell).  For instance, do this:&lt;BR /&gt;&lt;BR /&gt;# cat /tmp/env&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;export VAR1=jrf&lt;BR /&gt;export VAR2=hein&lt;BR /&gt;export VAR3=peter&lt;BR /&gt;&lt;BR /&gt;Now, compare:&lt;BR /&gt;&lt;BR /&gt;# /tmp/env&lt;BR /&gt;echo "${VAR1}, ${VAR2} ${VAR3}"&lt;BR /&gt;sh: VAR1: Parameter not set.&lt;BR /&gt;&lt;BR /&gt;...to:&lt;BR /&gt;&lt;BR /&gt;# . /tmp/env&lt;BR /&gt;echo "${VAR1}, ${VAR2} ${VAR3}"&lt;BR /&gt;jrf, hein peter&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Tue, 02 May 2006 07:10:25 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976527#M99357</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-05-02T07:10:25Z</dc:date>
    </item>
    <item>
      <title>Re: Parameter checking in scripts</title>
      <link>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976528#M99358</link>
      <description>As James points out, 'sourcing' a script means that the current shell remembers any variables that are previously set. So to allow for maximum flexibility, you should always test for the quantity of parameters passed to the script and ignore anything in the variables if the quantity is not correct (or in your case, preset the default values):&lt;BR /&gt; &lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;set -u&lt;BR /&gt; &lt;BR /&gt;[ $# -eq 1 ] &amp;amp;&amp;amp; VAR1=$1 || VAR1=4&lt;BR /&gt; &lt;BR /&gt;echo "VAR1 is $VAR1"&lt;BR /&gt; &lt;BR /&gt;In this case, the test is explicitly for one parameter so anything else (ie, no parameters, two parameters, etc) will leave VAR1=4, otherwise, VAR1 is assigned the first parameter on the command line. As with all interactive scripts, you should assume that monkeys are typing on the keyboard and the value for VAR1 should be tested for validity (ie, not a number, number too large or small, etc)</description>
      <pubDate>Tue, 02 May 2006 08:19:19 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976528#M99358</guid>
      <dc:creator>Bill Hassell</dc:creator>
      <dc:date>2006-05-02T08:19:19Z</dc:date>
    </item>
    <item>
      <title>Re: Parameter checking in scripts</title>
      <link>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976529#M99359</link>
      <description>I'm lost now, seeing no diff in your explanation.&lt;BR /&gt;Using /usr/bin/sh :&lt;BR /&gt;$ more /tmp/env&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;echo "${VAR1}, ${VAR2} ${VAR3}"&lt;BR /&gt;&lt;BR /&gt;$ export VAR1=jrf&lt;BR /&gt;$ export VAR2=hein&lt;BR /&gt;$ export VAR3=peter&lt;BR /&gt;&lt;BR /&gt;$ /tmp/env&lt;BR /&gt;jrf, hein peter&lt;BR /&gt;$ . /tmp/env&lt;BR /&gt;jrf, hein peter&lt;BR /&gt;</description>
      <pubDate>Tue, 02 May 2006 08:35:55 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976529#M99359</guid>
      <dc:creator>PJJ</dc:creator>
      <dc:date>2006-05-02T08:35:55Z</dc:date>
    </item>
    <item>
      <title>Re: Parameter checking in scripts</title>
      <link>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976530#M99360</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;JRF's solutions was (sorry) a bit misleading, because the variables VARi should NOT exported in the calling shell.&lt;BR /&gt;&lt;BR /&gt;Let us try my way:&lt;BR /&gt;cat myscript&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;v1=${1:-jrf}&lt;BR /&gt;v2=${2:-hein}&lt;BR /&gt;v3=${3:-nik}&lt;BR /&gt;echo v1=$v1 v2=$v2 v3=$v3&lt;BR /&gt;&lt;BR /&gt;Now try:&lt;BR /&gt;myscript&lt;BR /&gt;v1=jrf v2=hein v3=nik&lt;BR /&gt;&lt;BR /&gt;myscript set your params&lt;BR /&gt;v1=set v2=your v3=params&lt;BR /&gt;&lt;BR /&gt;. myscript set your params&lt;BR /&gt;v1=set v2=your v3=params&lt;BR /&gt;&lt;BR /&gt;. myscript&lt;BR /&gt;v1=set v2=your v3=params&lt;BR /&gt;&lt;BR /&gt;myscript&lt;BR /&gt;v1=jrf v2=hein v3=nik&lt;BR /&gt;&lt;BR /&gt;set try this&lt;BR /&gt;. myscript&lt;BR /&gt;v1=try v2=this v3=nik&lt;BR /&gt;&lt;BR /&gt;But you always get your desired result via&lt;BR /&gt;myscript Jong&lt;BR /&gt;v1=Jong v2=hein v3=nik&lt;BR /&gt;&lt;BR /&gt;mfG Peter&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 02 May 2006 08:52:23 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976530#M99360</guid>
      <dc:creator>Peter Nikitka</dc:creator>
      <dc:date>2006-05-02T08:52:23Z</dc:date>
    </item>
    <item>
      <title>Re: Parameter checking in scripts</title>
      <link>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976531#M99361</link>
      <description>Hi (again):&lt;BR /&gt;&lt;BR /&gt;THe 'echo' is run after either running the script or sourcing it.  It wasn't part of the script I meant you to run.  Eliminating the superflous (yes, misleading) 'export', what I showed was:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;VAR1=jrf&lt;BR /&gt;VAR2=hein&lt;BR /&gt;VAR3=peter&lt;BR /&gt;exit &lt;BR /&gt;&lt;BR /&gt;Now, compare:&lt;BR /&gt;&lt;BR /&gt;# /tmp/env #...run the script&lt;BR /&gt;# echo "${VAR1}, ${VAR2} ${VAR3}"&lt;BR /&gt;sh: VAR1: Parameter not set.&lt;BR /&gt;# set #...to see the environmental variables&lt;BR /&gt;...to:&lt;BR /&gt;&lt;BR /&gt;# . /tmp/env #...source the script&lt;BR /&gt;# echo "${VAR1}, ${VAR2} ${VAR3}"&lt;BR /&gt;jrf, hein peter&lt;BR /&gt;# set #...now shows VAR1, VAR2 and VAR3 too!&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Tue, 02 May 2006 09:02:33 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976531#M99361</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-05-02T09:02:33Z</dc:date>
    </item>
    <item>
      <title>Re: Parameter checking in scripts</title>
      <link>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976532#M99362</link>
      <description>With all this help the 'global-' or 'localness' is clear now.&lt;BR /&gt;&lt;BR /&gt;And for the parameterquestion this is elegant: v1=${1:-jrf}&lt;BR /&gt;&lt;BR /&gt;Thanks all!&lt;BR /&gt;Pete</description>
      <pubDate>Wed, 03 May 2006 01:46:20 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976532#M99362</guid>
      <dc:creator>PJJ</dc:creator>
      <dc:date>2006-05-03T01:46:20Z</dc:date>
    </item>
    <item>
      <title>Re: Parameter checking in scripts</title>
      <link>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976533#M99363</link>
      <description>Closed, thank to all.</description>
      <pubDate>Wed, 03 May 2006 01:50:25 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/parameter-checking-in-scripts/m-p/4976533#M99363</guid>
      <dc:creator>PJJ</dc:creator>
      <dc:date>2006-05-03T01:50:25Z</dc:date>
    </item>
  </channel>
</rss>

