"let" command error in bash script
I have found this simple code to echo options passed to a function.
I does work , however, I am sometime getting strange unwanted output. At present it is harmless, but I like to know what is the cause.. PS Since the "cnt" is really not "initialized", Could that be a problem? Test code Code:
# Printing the data available in $@ Quote:
Quote:
Note that the "error #" is indicating problem is indeed associated with "let" command. Quote:
|
That probably means that you are not running that as bash, but in some other shell environment.
Check the interpreter line, is it #!/bin/bash or #!/bin/sh? You also say it is called from another script so it is hard to know what the actual running context is from information provided. Fix it by changing that line to not use let, perhaps: Code:
cnt=$((cnt+1)) |
I did changed "let " to ((cnt++)) with same results.
I think you are correct - the problem is when the "side" process returns to the main script and it generates "no". That must do something to the positional parameters at that point. Quote:
#! /bin/sh same as the "main" script. Anyway, appreciate your input, but this is no big deal, just was curious why is this happening. I need to clean-up the main problems and then I will have time to get back to this minor one. Cheers |
Code:
ls -al $SHELL |
Quote:
|
that is your loop code, all of it?
cnt not initialized Code:
#!/bin/bash Code:
$ ./letters /pack/gp/de -exist -hne |
Quote:
This is definitely a problem of mixing shells. Your code is running fine with bash, but will not run with sh. You can simply try it by creating a small script (containing the loop from your original post): Code:
user@host:/tmp$ bash ./a.sh d f g But you can also try to insert #!/bin/bash and #!/bin/sh, make it executable and run by ./a.sh and you will see the same result. |
Quote:
Using same attitude - it would help if you read the post. See attached. Either way - so far nobody seems to be able to answer or guess why it does not work PART of the time. It would be helpful to post replies actually answering the original question without getting sidetracked with references to man or doc. The standard positional parameters are available / passed to the code and the correct prints reflects that, regardless if it posts the "not found" error or not. What else do I need to supply to analyse the issue? Quote:
|
what is the script, not just that little bit, because me and another ran your little sniplet and they both worked. therefore it has to be something you are not showing up in order for anyone else to try and deduce what the actual problem is.
|
Quote:
You can ask why is it executed with bash and/or sh. The answer: depends on how it was invoked. |
SOLVED
Code:
for i Current version does not print the numbers of the parameters, so did not the original code. The numbers are irrelevant, I need just to verify the "arguments" AKA options passed to the function. Thanks for all the help provided by the group. |
Still unclear.
Code:
for i Code:
for i in "$@" |
just a comment: I have never heard something like this: "code failed to use the positional parameters correctly". Probably you mean incorrect handling/implementation. The positional parameters work perfectly in all the apps/languages I tried.
|
still didn't show us the script, prefers to be opinionated instead.
|
Quote:
If it helps YOU , here it is. Have a ball, but since the problem is solved keep in mind I am NOT interested in your analysis of this script and no longer subscribe to this thread. Code:
#! /bin/sh |
Quote:
Quote:
Your reply is needlessly defensive and the only display of "attitude" in this thread. Quote:
Everyone here volunteers their time and is making good faith effort to help with your problem. This is a friendly place for exchange of knowledge, and that is a two way exchange, please make effort to deal with others in that spirit, and in a less confrontational manner. Please review the Site FAQ for guidance in posting your questions and general forum usage. |
All times are GMT -5. The time now is 11:52 PM. |