The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script to update values for the parameters gmahesh2k UNIX for Dummies Questions & Answers 1 07-14-2008 10:53 PM
Positional parameters shalu@ibm UNIX for Dummies Questions & Answers 2 11-22-2007 07:58 AM
Positional Parameters ericelysia UNIX for Dummies Questions & Answers 15 10-23-2005 04:11 PM
Positional Parameters google Shell Programming and Scripting 2 09-26-2003 02:51 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 07-16-2008
nmk nmk is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 2
Resetting the Positional parameters values

Hi,

Can any one provide the Unix command to reset the positional parameters?

Please see the below example where i have to pass 2 parameters to Shell1.sh.

Step1) . ./Shell1.sh 2 3
successfully executed, Then i executed(next step only) the same shell script again,this time no parameters passed.

Step 2) . ./Shell1.sh
This is also successfully executed,because it considered(previous values) 2 & 3 as the positional parameters as i had not reseted them.

Thanks in advance.
  #2 (permalink)  
Old 07-16-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,311
I don't understand. How does the script know of the former parameters? Do you export them or write them to file?
  #3 (permalink)  
Old 07-16-2008
nmk nmk is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 2
Hi zaxxon ,

How does the script know of the former parameters?
As i know if we once assign the values to positional parameters those will be remain same until we reassign them.the same happened in my case also.

Do you export them or write them to file?
I am not exporting or not writing into a file.

I am new to Unix & shell scripting, correct me if i am wrong correct me in above.

Below is the content of Shell1.sh:
if [ $# != 2 ]
then
echo "No Of Parameters passed not equal to 2"
else
echo $1
echo $2
echo "Completed"
fi


Below is the flow of execution & their output:

. ./Shell1.sh 2 3
2
3
Completed
. ./Shell1.sh
2
3
Completed


The second time i need to get the output like "No Of Parameters passed not equal to 2", but it was not happened.This is the reason i need to know the command to reset the positional parameters, can u share the command if you know.

Thanks for your help.
  #4 (permalink)  
Old 07-16-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,938
This question has been raised a number of times on this forum. What you are seeing is the expected behavour of your shell since you are dot including i.e. sourcing Shell1.sh.
  #5 (permalink)  
Old 07-16-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,311
Now I understand what you mean ok...
There is nothing to reset as shell scripts usually don't remember anything without writing it to a file or exporting it to the environment. So there is nothing to reset.
The problem with your script is the line
Quote:
f [ $# != 2 ]
which you should try with


Code:
if (( $# != 2 ))

The double round brackets treat the values as numbers (arithmetic), not as strings, so it should work now.

Example:

Code:
root@isau02:/data/tmp/testfeld> cat script.ksh
#!/usr/bin/ksh

echo $#

if (( $# != 2 )); then
        echo "Hey! This is not 2 parameters!"
else
        echo "Everything is fine"
fi

exit 0
root@isau02:/data/tmp/testfeld> ./script.ksh  b lala
2
Everything is fine
root@isau02:/data/tmp/testfeld> ./script.ksh  b lala lal
3
Hey! This is not 2 parameters!
root@isau02:/data/tmp/testfeld> ./script.ksh  yo
1
Hey! This is not 2 parameters!


EDIT: Oh didn't notice he was sourcing, forget my post
Closed Thread

Bookmarks

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 02:45 PM.


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