The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Longer commands and strange behaviour on ksh anurags UNIX for Dummies Questions & Answers 2 03-27-2008 11:04 AM
Strange sed behaviour vino UNIX for Advanced & Expert Users 8 02-12-2008 06:51 AM
A Strange Behaviour!!! navojit dutta Shell Programming and Scripting 5 12-21-2007 04:35 AM
/etc/passwd strange behaviour! penguin-friend Linux 0 06-06-2005 12:00 PM
very strange behaviour on unix server bolo77 UNIX for Dummies Questions & Answers 7 03-02-2005 03:32 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-19-2008
gsw_aix gsw_aix is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 4
Cool Strange KSH behaviour - any comments?

As you are probably aware, $# indicates the number of parameters passed into a korn shell script. But this appears to hang around for
sunsequent runs...????

A simple script:-
#!/usr/bin/ksh
echo "#parameters $#"
echo "\$1 $1"
echo "\$2 $2"

I run the script with 0 parameters (all fine)
# . par.ksh
#parameters 0
$1
$2

Now run it passing 2 parameters (all fine)
# . par.ksh aaa bbb
#parameters 2
$1 aaa
$2 bbb

Now run it again with no parameters (and it still thinks there are 2)!
# . par.ksh
#parameters 2
$1 aaa
$2 bbb

If I add set -- (to unset positional parameters) it works!
But I would have though that $# gets set every time its run,

Any ideas anyone ?????
  #2 (permalink)  
Old 06-19-2008
gfca gfca is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 31
Hi!

I have tried what u did in AIX5.3, that uses ksh and it worked well.

Code:
root@bbb:/> ./teste.sh ola
#parameters 1
$1 ola
$2
root@bbb:/> ./teste.sh ola bla
#parameters 2
$1 ola
$2 bla
root@bbb:/> ./teste.sh ola bla edas
#parameters 3
$1 ola
$2 bla
root@bbb:/> ./teste.sh
#parameters 0
$1
$2
root@bbb:/>
  #3 (permalink)  
Old 06-19-2008
gsw_aix gsw_aix is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 4
Thanks for that, Hmmmmm,

I may have to raise a call (Its on Solaris 10).
  #4 (permalink)  
Old 06-19-2008
gfca gfca is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 31
ok! hope that would help
  #5 (permalink)  
Old 06-19-2008
kahuna's Avatar
kahuna kahuna is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 149
Gsw_aix, you're dot including - so you are running all your scripts in the current shell.
Gfca, on the other hand, is running each script in separate child shells.
  #6 (permalink)  
Old 06-20-2008
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
Exactly this - once the script is run in the currect shell, once in a daughter shell - is reason for the different results.

The line ". ./x.ksh par1 par2" does the following: it starts in the current shell the script x.ksh and passes two parameters. These parameters are passed not to the script but to the process which runs the script. This process is the current process - our login shell. When the script is run again in the same process the two parameters passed to this process are still there. They can be displayed even in the commandline:

Code:
# cat x.ksh
#! /bin/ksh
print - "$? parameters have been passed"
exit 0

# print - $?
0                 # there are no parameters

# . ./x.ksh par1 par2
2 parameters have been passed

# print - $?
2                 # there still are the two parameters

# . ./x.ksh
2 parameters have been passed
On the other hand if we run "./x.ksh par1 par2" we first start a new process, pass the two parameters to this new process, run the script and end the process again. If we cll it agin, another(!) new process is created, which does of course know nothing about the first process and its environment.

See also this thread: How to clear $1 when dot-running a script.

I hope this helps.

bakunin
  #7 (permalink)  
Old 06-24-2008
Sai Kumar Sai Kumar is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 1
Thumbs down KSH behaviour

Hi i worked on linux with the same script, i didn't find wrong with the KSH behaviour


[saik@lnx02 mp]$ ./test.ksh
#parameters 0
$1
$2
[saik@lnx02 mp]$ ./test.ksh 111 222
#parameters 2
$1 111
$2 222
[saik@lnx02 mp]$ ./test.ksh 111 222 333
#parameters 3
$1 111
$2 222
[saik@lnx02 mp]$ ./test.ksh 111
#parameters 1
$1 111
$2
[saik@lnx02 mp]$ ./test.ksh
#parameters 0
$1
$2
[saik@lnx02 mp]$

Sai Kumar
Closed Thread

Bookmarks

Tags
linux, solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0