argument hysteresis?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting argument hysteresis?
# 1  
Old 07-09-2008
argument hysteresis?

Hi there,

When I try to execute the following simple script "simple_cript.ksh":
PHP Code:
NB_ARG=$#
ARGS=$@ 
the number of arguments and the arguments themselves seem to be remembered. If I run it a first time with argument "arg1", and a second time without any arguments, it still displays "arg1" at the second run.

How can I reset the arguments? please help...
# 2  
Old 07-09-2008
I cannot reproduce this behavior:

Code:
zsh-4.3.4% cat simple_script.ksh
printf "%d\n" $#
printf "%s " "$@"
printf "\n"
zsh-4.3.4% ./simple_script.ksh arg1
1
arg1
zsh-4.3.4% ./simple_script.ksh
0

Could you post the entire test case?
# 3  
Old 07-09-2008
here it is, my file "arguments_try.ksh":

PHP Code:
#!/bin/ksh
# my shell arguments
NB_ARG=$#
ARGS=$@
echo 
nb arguments\: $NB_ARGS; echo
echo 
arguments\: $ARGS; echo 
commands in the terminal:
PHP Code:
st25219@mx73:ianarguments_try.ksh arg1
st25219
@mx73:ianarguments_try.ksh 
and the screen output:

nb arguments: 1
arguments: arg1
**
nb arguments: 1
arguments: arg1


ps: the only way for me to modify the "arg1" in the screen output is to give another argument list ...

Last edited by MarkZWEERS; 07-09-2008 at 12:36 PM.. Reason: ps
# 4  
Old 07-09-2008
I don't get the same output, perhaps someone else can try to reproduce it:


Code:
$ arguments_try.ksh arg1
nb arguments: 1

arguments: arg1

$ arguments_try.ksh
nb arguments: 0

arguments:

$ cat arguments_try.ksh
#!/bin/ksh
# my shell arguments
NB_ARG=$#
ARGS=$@
echo nb arguments: $NB_ARG; echo
echo arguments: $ARGS; echo

The variable name $NB_ARGS is misspelled.
# 5  
Old 07-09-2008
that's a copy-paste error...
I'll try the script at home on my linux machine as well. Thanks anyways!
# 6  
Old 07-09-2008
Yes, it does do the same thing on my system (Debian Etch).

This is the entire script:
PHP Code:
#!/bin/ksh
#
# try-outs for script arguments from command line
#

echo You\'ve typed $# arguments, being\:
echo $*

echo
echo

echo You\'ve launched the script $0 with $# arguments, being\:
ARGS=$@
for arg_i in $ARGS
do
  set -- $arg_i
  echo $arg_i
done 
and this is the screen output:
PHP Code:
mark@zweers:~/progs/scripts$ . arguments.ksh 
You
've typed 0 arguments, being:



You'
ve launched the script bash with 0 argumentsbeing:
mark@zweers:~/progs/scripts$ . arguments.ksh tmp1
You
've typed 1 arguments, being:
tmp1


You'
ve launched the script bash with 1 argumentsbeing:
tmp1
mark
@zweers:~/progs/scripts$ . arguments.ksh
You
've typed 1 arguments, being:
tmp1

You'
ve launched the script bash with 1 argumentsbeing:
tmp1 

Last edited by MarkZWEERS; 07-09-2008 at 03:46 PM.. Reason: correction 'php' tags
# 7  
Old 07-09-2008
This script is different ...
You're resetting the arguments in the for loop (why?):

Code:
$ . arguments.ksh
Youve typed 0 arguments, being:



Youve launched the script bash with 0 arguments, being:
$ . arguments.ksh 1
Youve typed 1 arguments, being:
1


Youve launched the script bash with 1 arguments, being:
1
$ . arguments.ksh 1 2
Youve typed 2 arguments, being:
1 2


Youve launched the script bash with 2 arguments, being:
1
2
$ . arguments.ksh
Youve typed 1 arguments, being:
2


Youve launched the script bash with 1 arguments, being:
2

In the end of the for loop your're setting $1 to the last argument.

Is this a joke?

Try commenting the set statement and rerun the script ...

Last edited by radoulov; 07-09-2008 at 04:56 PM.. Reason: corrected
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to substitute one argument into another?

Hello Friends, I am not able to solve something which looks silly..after spending some time around it, thought of positing it here. I want to get $2, $3... into user. but I want to control the sequence by count. I want to assign $2 user..then $3 to user..then $4 to user etc. how can I... (4 Replies)
Discussion started by: venkat4tech
4 Replies

2. UNIX for Advanced & Expert Users

Error:--test: argument expected--Even though i give an argument.

Hi All, I am running the script VBoxManage list vms |sed 's/"//g' | cut -d " " -f1 > har1out.mytxt result=`cat har1out.mytxt | grep $1' echo $result echo $1 { if then echo pass else echo fail fi (2 Replies)
Discussion started by: harsha85
2 Replies

3. Shell Programming and Scripting

Make script that run with argument if not run from configuration file argument

Hello, Is there any method thorugh which script can take argument if pass otherwise if argument doesn't pass then it takes the argument from the configuration file i.e I am workiing on a script which will run through crontab and the script will chekout the code ,zip and copy to the... (3 Replies)
Discussion started by: rohit22hamirpur
3 Replies

4. Shell Programming and Scripting

argument

Im sorry but I'm in need of help. How would I give the user the option to include the name of an html file as an arugment for the script? but if none is provided, then the script should prompt the user for the file name. I have sed 's/<*>//g' yourfile.html | uniq >newfile.txt so far I have... (7 Replies)
Discussion started by: clicstic
7 Replies

5. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with “-“

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

6. Shell Programming and Scripting

Array as argument

hi I've 2 bash script, in the first i define an array and I'd like to pass it as argument to the second script. For example: first_bash: k=(1 2 3 4) ....... #in the end i call the 2 script ./second_bash $k then in the second_bash: ..... echo "${1}" #this not work but i'm... (7 Replies)
Discussion started by: Dedalus
7 Replies

7. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

8. UNIX for Dummies Questions & Answers

How to find the last argument in a argument line?

How to find the last argument in a argument line? (4 Replies)
Discussion started by: nehagupta2008
4 Replies

9. Shell Programming and Scripting

argument parsing...

Hi all, Iam a beginer in shell scripting. i need a script that can parse the arguments and store them in variables. ex: ./myScript -v v1 -h v2 -c v3...... can someone suggest me...? tnx in adv. (1 Reply)
Discussion started by: midhun_u
1 Replies

10. Shell Programming and Scripting

argument help

if i have 2 arguments $1 and $2 how can i append them so that $2 is added to $1? It is really confusing me because it seems so simple, yet i can't get the shell script to work properly. Thanks. (2 Replies)
Discussion started by: brentdeback
2 Replies
Login or Register to Ask a Question