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.
# 1  
Old 03-06-2008
How to clear $1 when dot-running a script.

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:

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???

edit by bakunin: while this is a very interesting question it is hardly related to AIX. I move it over to "Shell programming and scripting" therefore.

Last edited by bakunin; 03-06-2008 at 05:36 AM..
# 2  
Old 03-06-2008
Tools Which shell are you using?

I used bash shell and it works exactly fine.the variable $1 is unset.Let me know which shell you use and I can try on it , if I have access to the same.

Thanks!
nua7
# 3  
Old 03-06-2008
Originally posted in AIX Forum FOR A REASON!!!

Well, you can thank the forum administrator for all the confusion. I originally posted this question in the AIX FORUM since this behavior seemed to be specific to AIX.

I am using KSH shell running on AIX 5.x
# 4  
Old 03-07-2008
First off, I'd like to state for the record, that you should thank yourself instead of thanking me: you haven't made clear that this occurs only in AIX (to your knowledge) and among the many things i am incompetent in is reading minds. Furthermore it will become clear with the answer that i was correct moving the thread as it is indeed a shell-related problem, not a OS-related one. In fact it is a problem of understanding, not of software.

Further, and to clear up the confusion: This effect can probably be demonstrated in other implementations of the Korn Shell too, as it is no bug, but a feature. You execute a script IN THE CURRENT ENVIRONMENT and hence you can set any variable and it will stick to this value until being changed. The same is the case with system variables like $1, which you are setting here - in your current environment. You can query the value of $1 also in the current environment:

Code:
# echo $1

# . ./testme xxx
1=xxx
# echo $1
xxx

It may look a bit counter-intuitive, but: which reason would the shell have to change the value of "$1" as long as it isn't being changed?

After this explanation the solution how to clear the variable "$1":

Either provide an empty value to make it empty:
Code:
# . ./testme xxx
1=xxx
# echo $1
xxx
# . ./testme ""
1=
# . ./testme
1=

or use "shift" to rotate out $1:

Code:
# . ./testme xxx
1=xxx
# echo $1
xxx
# shift
# echo $1

# . ./testme
1=

One word about the implementation of bash: *this* in fact is a bug and the failure of the Linux pdksh to do the same as the AIX ksh should be regarded as sloppiness of implementation, not as feature.

bakunin
# 5  
Old 03-07-2008
Sorry but I disagree. Where is the variable "1" being set? The line "echo 1=$1" does not define or assign any variable. It tells the shell to expand $1 (in this case, XXX) and output "1=XXX"

Code:
$ echo ${.sh.version}
Version M 93s+ 2008-01-31
$ cat testme
echo 1=$1
$ . ./testme XXX
1=XXX
$ echo $1

$ . ./testme
1=
$

# 6  
Old 03-07-2008
You are right and wrong, fpmurphy: The variable 1 is not set in the echo-statement. It is merely displayed there. It is set when the script is started and as the script is started in the current environment the variable is set in the current environment.

When you execute "script 1", you start a new shell environment, then set the variable 1 in this environment to "1" and then the script starts to run, yes? If you issue ". script 1" you do not start a new environment but the rest of the procedure occurs quite the same.

bakunin
# 7  
Old 03-07-2008
Good, Bakunin, we agree that the variable "1" is not set by the "echo 1=$1" statement.

You claim that $1 (first positional paramater) should be available in your current environment upon sourcing a file such as in the example provided

Sorry, again, I must disagree. Positional parameters are initially assigned when you invoke a shell, temporarily replaced when a shell function is invoked, and can only be reassigned with the set special built-in command.

$1 is available within the testme script but not outside it. To illustrate this point, try the
following:
Code:
#!/usr/bin/ksh
echo "Positional Parameter 1=$1"
. ./testme XXX
echo "Positional Parameter 1=$1"

which gives the following output
Code:
$ ./demo FPM
Positional Parameter 1=FPM
1=XXX
Positional Parameter 1=FPM
$

Have you a counter example to disprove my assertion?
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