AIX

AIX

Connect with fellow AIX users and experts to gain knowledge, share insights, and solve problems.


#Power
#Power
#Operatingsystems
#Servers
 View Only
  • 1.  How to clear $1 when dot-running a script.

    Posted 03/05/08 10:40 PM

    Originally posted by: GregMarino


    Here's my problem: the $1 parameter is getting permanently set when you dot-run a script - and I cannot unset it.

    Here's how to duplicate the problem in a ksh shell running on AIX 5.x:

    1) Create a script called testme and put this simple command in the script:
    echo 1=$1

    Save the script

    2) Make the testme script executable
    chmod a+rx testme

    3) Now dot-run the script:
    . testme

    Output:
    1=

    4)Now dot-run the script with a parameter:
    . testme YOU

    Output:
    1=YOU
    5) Now dot-run the script without a parameter:
    . testme

    Output:
    1=YOU

    HUH??? There's the problem!!! Why did the script remember the $1 parameter value?

    The question is - how do I clear that $1 value???

    Message was edited by: GregMarino

    Message was edited by: GregMarino
    #AIX-Forum


  • 2.  Re: How to clear $1 when dot-running a script.

    Posted 03/06/08 02:06 PM

    Originally posted by: nagger


    Because that is what you asked it to do.
    The dot scripts are run by your current ksh.
    Normally, a script is run by a sub ksh started for the purpose.

    The . testme YOU
    Set $1 to the value YOU

    and its still YOU when you run it for the second time.
    Just type: echo $1
    to see this.

    Hope this helps, N
    ps: I will not ask what exactly you are trying to do in the fear of you might actually answer!
    #AIX-Forum


  • 3.  Re: How to clear $1 when dot-running a script.

    Posted 03/06/08 02:57 PM
      |   view attached

    Originally posted by: GregMarino


    Thanks for the reply...

    The second time I run it without any parameters:
    . testme
    I want $1 to actually be blank. But it is not - it is retaining the previous value "YOU"

    So the real question is : How do I get $1 unset? And don't suggest to run it like this:
    . testme ""
    cause that causes another problem. My real script tests for both the existance of $1 and it's contents of present...and if $1 does not equal a specific value, then you get and error.

    Here - put this in your testme script:

    <hr />
    echo 1=$1

    if [
    then
    if [
    then
    echo "There was at least 1 Parameter passed into this script and \$1 was not \"DB\" "
    else
    echo "There was at least 1 Parameter passed into this script and \$1 was \"DB\" "
    fi
    else
    echo "There were no parameters passed into this script"
    fi
    <hr />

    Now run these commands and observer the output (the output is based on the assumption you have not already "contaminated" your session:
    . testme
    There were no parameters passed into this script

    . testme DB
    There was at least 1 Parameter passed into this script and $1 was "DB"

    . testme
    There was at least 1 Parameter passed into this script and $1 was "DB"

    . testme ""
    There was at least 1 Parameter passed into this script and $1 was not "DB"

    This is NOT the behavior I need.

    And since you asked (not), I have attached the actual script that I am trying to run - db2_env.ksh
    This script sets the environment parameters needed for db2 to run properly in your session. This is accomplished by dot-running the $INSTHOME/sqllib/db2profile script. On many servers, we have many db2 instances. So this script finds all available db2 instances, displays a list of them and then allows you to choose which db2 instance you want to switch to for your session.

    So in order for the db2profile to set the environment in your current session, you must dot-run this script.

    This script has an "enhanced feature" which also allows you to see what databases are available in each instance. To trigger this functionality, you pass in an optional parameter. The problem is, once you trigger the functionality by typing . db2_env.ksh DB you can never run the script again without the trigger since $1 is forever being set to DB

    And since the DB function is supposed to be Optional, the user should be able to just type . db2_env.ksh and get the desired functionality.

    And one of the main reasons why the DB function is Optional is that it takes a long time to run through all the db2 instances and generate the list of DB's. So it's not a function I want to execute every time I run db2_env.ksh

    So - you were probably right - you probably did NOT want to know all of that...

    Sorry if this is TMI...
    #AIX-Forum

    Attachment(s)



  • 4.  Re: How to clear $1 when dot-running a script.

    Posted 03/06/08 04:11 PM

    Originally posted by: SystemAdmin


    In your script, after using $1, clear it with:

    set --

    Note that this will clear all positional parameters.
    #AIX-Forum


  • 5.  Re: How to clear $1 when dot-running a script.

    Posted 03/06/08 04:15 PM

    Originally posted by: GregMarino


    JohnBoy,

    THANK YOU!!!! That did it!!!
    #AIX-Forum