How to clear $1 when dot-running a script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to clear $1 when dot-running a script.
# 8  
Old 03-07-2008
ok people, stop fighting over my question...

bakunin - I appologize. I did not "state" that the problem was in AIX, I merely tried to imply it by placing the question in the AIX Forum - and it seems that that still mis-characterizes it since it's really a ksh issue.


I did find an answer to my question - which was
Quote:
The question is - how do I clear that $1 value???
An answer is to use the following:
Code:
set --

or, as was pointed out earlier by bakunin, use the shift command to shift the positional parameters until they are all empty. For what I am looking for, set -- is more efficient.

Ok - let's lay this out this issue/question a bit better. My issue is that when I am dot-running a script over and over, I don't want the positional parameters to persist in my session. If they do, then I can never re-dot-run the script - or any other script - with NO parameters. In addition, my actual script is scrutinizing the $1 parameter and checking for specific values - so just passing in a "" as the first parameter actual fails my check and spit out a USAGE message explaining how to use the script. The first positional parameter is used to activate optional functions within the script. To just run the script "normally" without the optional features, you don't pass in a parameter.

And there in lies my problem.

The solution for me was to set a Variable=$1 (and repeating that for all other passed in parameters as needed) and immediately issuing set --. This clears all positional parameters that were permanently set for my current environment/session (due to the use of dot-run).

Problem solved.


Now - to address some of the other comments/assertions being made in the other comments/replies posted here:

Here's some new code for testme which shows an example of how I am checking the value of $1 in my "real script" (which I have still not shown you - but we are trying to keep things simple here).

Code:
echo 1=$1

if [[ $# > 0 ]]
then
   if [[ ! "${1}" = "DB" ]]
   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

Now run these commands and observe the output.

First, let's clear all positional parameters:

set --

Now let's start our experiment:

. 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"

The above output shows that $1 persists in the current environment. This is NOT the behavior I need/want.


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

The above output shows that $1 is not empty and thus fails the test
if [[ ! "${1}" = "DB" ]]
This is NOT the behavior I need.

This also shows that $1 is available in your current environment/session. To further prove this, run the following commands:

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

echo 1=$1
1=DB

The value in $1 would persist and potentially mess with other scripts ONLY when those scripts are dot-run. I hope you would agree that this is NOT a desirable behavior. set -- takes care of that behavior by wiping clean all positional parameters ($1, $2, $3, etc)
# 9  
Old 03-07-2008
First off, I'd like to apologize to fpmurphy and HobieCoop if they had the impression of a fight going on - this is not the case. In fact i'm enjoying tremendously the possibility to gnaw my teeth into a problem which is *not* all-too-obvious to solve.

fpmurphy has raised some interesting points, but i need a bit of time to come up with a (clever) response.

bakunin
# 10  
Old 03-08-2008
HobieCoop, Bakunin and I are not fighting. We are simply trying to come up with a correct understanding of the interesting problem presented to us by you.
# 11  
Old 03-09-2008
Computer

And I appreciate it! Thanks for the help! Smilie
# 12  
Old 03-10-2008
@fmurphy

No counter example needed. Taking your example:

Code:
$ uname -a
SunOS db012a 5.8 Generic_117350-35 sun4us sparc FJSV,GPUZC-M
$ echo $0
-ksh
$ . testme
1=
$ . testme YOU
1=YOU
$ . testme
1=YOU
$ ./demo FPM
Positional Parameter 1=FPM
1=XXX
Positional Parameter 1=XXX

However, bash doesn't seem to remember passed positional parameters across dot invocations:

Code:
$ cat other
echo "1=$1 2=$2 3=$3 4=$4"
$ bash
$ . other
1= 2= 3= 4=
$ . other a b
1=a 2=b 3= 4=
$ . other z
1=z 2= 3= 4=
$ . other
1= 2= 3= 4=

Ksh on the other hand will reset only when something is passed:

Code:
$ ksh
$ . other
1= 2= 3= 4=
$ . other a b
1=a 2=b 3= 4=
$ . other z         # here $2 (and higher) are cleared since at least one positional parameter was passed.
1=z 2= 3= 4=
$ . other
1=z 2= 3= 4=

So in a nutshell, agree with bakunin's points on ksh.

HTH

Last edited by rikxik; 03-10-2008 at 05:36 AM..
# 13  
Old 06-11-2008
The AIX ksh behaviour discussed in this thread might be different depending on which ksh version is used to call the dot command. IIRC AIX came with both ksh88 and ksh93 since V5.2. While the ksh93 implementation in AIX was quite buggy (e.g. using it as default login shell could lead to the user being unable to log in) it seems to be usable in the current oslevels.
I assume that most posters used AIX ksh88 (which is still the AIX default shell) for testing. I am unsure about the environment fpmurphy used as
Quote:
Originally Posted by fpmurphy
[...]
Code:
$ echo ${.sh.version}
Version M 93s+ 2008-01-31
[...]

does not look like an AIX ksh93 to me straight away.
However, I am going to point out some difference between the two ksh versions coming with AIX 5L. Ksh versions are identical in the current versions of 5.2 and 5.3. My samples were collected from an AIX 5.3 server.

Code:
(0)admin2:/home/shockneck/scripts > oslevel -s
5300-06-06-0811
(0)admin2:/home/shockneck/scripts >

I copied HobieCoop's script from his previous post. I first run the script in ksh88. Mind that the script itself calls /bin/ksh in both tests I am going to do.
Code:
(0)admin2:/home/shockneck/scripts > cat testme
#/bin/ksh

echo 1=$1

if [[ $# > 0 ]]
then
   if [[ ! "${1}" = "DB" ]]
   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
(0)admin2:/home/shockneck/scripts >

First the script runs in ksh88 which has not changed in the last years and probably won't in the future.
Code:
(1)admin2:/home/shockneck/scripts > Version M-11/16/88f
(130)admin2:/home/shockneck/scripts > set --
(0)admin2:/home/shockneck/scripts > . testme
1=
There were no parameters passed into this script
(0)admin2:/home/shockneck/scripts > . testme DB
1=DB
There was at least 1 Parameter passed into this script and $1 was  "DB"
(0)admin2:/home/shockneck/scripts > . testme
1=DB
There was at least 1 Parameter passed into this script and $1 was  "DB"
(0)admin2:/home/shockneck/scripts >

So this is the test outcome HobieCoop complained about.
Second test, but this time in ksh93 as starting shell

Code:
(0)admin2:/home/shockneck/scripts > echo ${.sh.version}
Version M-12/28/93e
(0)admin2:/home/shockneck/scripts > set --
(0)admin2:/home/shockneck/scripts > . testme
1=
There were no parameters passed into this script
(0)admin2:/home/shockneck/scripts > . testme DB
1=DB
There was at least 1 Parameter passed into this script and $1 was  "DB"
(0)admin2:/home/shockneck/scripts > . testme
1=
There were no parameters passed into this script
(0)admin2:/home/shockneck/scripts >

The difference could be considered either a bug or a feature. Regardless using ksh93 would spare HobieCoop from having to flush the whole environment to clear the $1 value.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. HP-UX

Script to clear filesystem

Hi, I have created a script to clear up the /var filesystem once it reaches > 90%. This is part of the script : #!/bin/bash DIR = ./adm DIR2=./adm/sw DIR3 = ./spool/mqueue DIR4 = ./adm/syslog DIR5 = ./adm/sulog DIR6 = ./tmp F1 = ./tmp/dead.letter F2 = ./adm/wtmps file1 =... (5 Replies)
Discussion started by: anaigini45
5 Replies

2. Shell Programming and Scripting

Why script is running sometimes and not running sometimes?

Hi, I have a script which does couple of database connection and run some SELECT queries to get some output in the file. I am surprised to see :eek: that when i run my script some times it gives the desired out put and sometimes it shows some error :confused: . Suppose if i execute it say... (3 Replies)
Discussion started by: Sharma331
3 Replies

3. Shell Programming and Scripting

Script will keep checking running status of another script and also restart called script at night

I am using blow script :-- #!/bin/bash FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not if then # echo "process found" exit 0; else echo "process not found" exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Discussion started by: ketanraut
1 Replies

4. Shell Programming and Scripting

Need to exclude .NFSxxx files in clear old files batch script

I am new to Shell Scripting and need some help. The following batch job has been failing for me due to the .nfsxxx files in use. I need to know how to modify the following script to exclude the .nfsxxx files so this batch job will not fail on me. I have done lots of googling and keep coming back... (2 Replies)
Discussion started by: kimberlyg2007
2 Replies

5. AIX

Help with clear printer queue script in AIX 5.3

Good day UNIX forum, could you help me with my clear printer queue script, i have problems with the consistency of this function, sometimes it works sometimes it doesn't. Thanks in advance } preRemovePrintQ(){ clear; echo; echo... (1 Reply)
Discussion started by: beware187
1 Replies

6. SCO

Parameter passing to dot shell script

OS SCO Open Server 6.0 MP4 I am trying to change the value of a enviornment variable thru a script and want to pass a parameter on the commande line, If I hard code the value inside the script the script changes the enviornment variable . mytest where my test is MYVAR=$1 export MYVAR... (6 Replies)
Discussion started by: atish0
6 Replies

7. Solaris

Running from Shell Vs running from RC script

Hi, i have a script which need to do behave differently when run as a startup process from init.d/ rc2.d script and when run manually from shell. How do i distinguish whether my script is run by init process or by shell?? Will the command /proc/$$/psinfo | grep "myscript" work well???... (2 Replies)
Discussion started by: vickylife
2 Replies

8. Linux

SED/AWK Script to clear log file using timestamp?

I have a log file on our system which fills up with lines that have been timestamped, as follows.... 03/03/2008 10:56:06:815] (ERROR) balance: continuing session to genapp02 : 18500 03/03/2008 10:56:06:820] (ERROR) balance: continuing session to genapp02 : 18500 03/03/2008 10:56:07:003]... (2 Replies)
Discussion started by: davesimm
2 Replies

9. Shell Programming and Scripting

Clear Case, Awk and script

Hello. I should have asked this awhile ago but here is my situation. My task is to generate LOC for different directories. I have a text file that has dates in this format (01-Aug-2006). My task is to read each line and compare it to a branch date. Depending on the date, it should generate a... (0 Replies)
Discussion started by: mastachef
0 Replies
Login or Register to Ask a Question