Strange KSH behaviour - any comments?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Strange KSH behaviour - any comments?
# 1  
Old 06-19-2008
Tools 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  
Old 06-19-2008
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  
Old 06-19-2008
Thanks for that, Hmmmmm,

I may have to raise a call (Its on Solaris 10).
# 4  
Old 06-19-2008
ok! hope that would help
# 5  
Old 06-19-2008
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  
Old 06-20-2008
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: https://www.unix.com/shell-programmin...ng-script.html

I hope this helps.

bakunin
# 7  
Old 06-24-2008
PHP 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
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Red Hat

Crontab strange behaviour

Hi all, I'm having this scenario which for the moment I cannot resolve. :( I wrote a script to make a dump/export of the oracle database. and then put this entry on crontab to be executed daily for example. The script is like below: cat /home/oracle/scripts/db_backup.sh #!/bin/ksh ... (3 Replies)
Discussion started by: enux
3 Replies

2. HP-UX

Strange login behaviour

Hi all, I am using HP-UX and I have just noticed that when I log into the network it seems to save the previous windows that were subsequently closed on previous occasions. Does anyone know when I log in, it seems to display these previous windows, e.g. nedit windows open again? Does... (1 Reply)
Discussion started by: cyberfrog
1 Replies

3. Shell Programming and Scripting

Strange behaviour with perl i/o?

Hi All, I got a strange problem here. I have a perl script which is fetching data from a database table and writing a file with that data. If i run that script from linux command line, the file it creates is a normal ascii text file without any binary character in it.But... (9 Replies)
Discussion started by: DILEEP410
9 Replies

4. Shell Programming and Scripting

strange behaviour from sed???

Hi all, I want to do a very simple thing with sed. I want to print out the line number of a disk I have defined in /etc/exports, so I do: It's all good, but here's the problem. When I define md0 in a variable, I get nothing from sed: Why is that? can anybody please help? Thanks (2 Replies)
Discussion started by: alirezan
2 Replies

5. UNIX for Dummies Questions & Answers

Strange Program behaviour

Had a strange thing going on with my code. It's ok I figured it out for myself.... (2 Replies)
Discussion started by: mrpugster
2 Replies

6. UNIX for Dummies Questions & Answers

Longer commands and strange behaviour on ksh

Hi, I was trying to customize this archaic HP-UX box. only shell available is ksh and that too seems to be pretty old and doesn't completely conform to what I read on the web about ksh. Anyway here are my issues: - I wanted to have a dynamic title on xterm or dtterm. I put the following lines... (2 Replies)
Discussion started by: anurags
2 Replies

7. UNIX for Advanced & Expert Users

Strange sed behaviour

$ echo a.bc | sed -e "s/\|/\\|/g" |a|.|b|c| $ Is the behavior of the sed statement expected ? Or is this a bug in sed ? OS details Linux 2.6.9-55.0.0.0.2.ELsmp #1 SMP Wed May 2 14:59:56 PDT 2007 i686 i686 i386 GNU/Linux (8 Replies)
Discussion started by: vino
8 Replies

8. Shell Programming and Scripting

A Strange Behaviour!!!

Can some-one give me a view to this : I have a directory in an unix server, having permissions r-xr-xr-x .This directory is basically a source directory. Now there is another directory basically the destination directory which has all the permissions. Note:I log in as not the owner,but user... (5 Replies)
Discussion started by: navojit dutta
5 Replies

9. Linux

/etc/passwd strange behaviour!

Hi there, first of all, here is my conf of a uname -a Linux SAMBA 2.4.18-4GB #1 Wed Mar 27 13:57:05 UTC 2002 i686 unknown on a fedora machine. Here is my problem: every once in a while, the line containing root disappears in the /etc/passwd, disabling all logging on my server. Any one have... (0 Replies)
Discussion started by: penguin-friend
0 Replies
Login or Register to Ask a Question