You are not logged in.
Pages: 1
Hello all,
I normally don't have a need to write any BASH scripts for my own purposes but I have come upon a situation where I'm thinking it would just be easier to do so. I've read a few BASH tutorials and have seen enough scripts to know the basics but it's the little details that I think are throwing me off at this point. If you could help me out with some syntax stuff, I'd appreciate it.
Here is where I'm at. I want to first check the command line parameters and make sure there are four. Then I want to enter a loop based on the number specified in 4th parameter. This is what it looks like:
if [$# -ne 4]; then
usage()
while ["$count" -lt $4]; do
trap "usage" SIGINT SIGTERM
#do some other stuff
done
fi
This is what I'm getting: line 20: [0: command not found. Line 20 is the if statement but from every example I've seen, it's syntactically correct.
As you can see, I'm not really sure how to nest a loop inside the if. I basically want it to be something like "if there aren't four parameters, display usage and exit (there's an exit in the usage function). If there are four parameters, go into this loop". Hopefully, that's clear.
Last edited by Thrillhouse (2008-01-20 20:36:33)
For the strength of the pack is the wolf, and the strength of the wolf is the pack.
Offline
Put spaces between the brackets and the expression:
if [ $# -ne 4 ]; then
fi
Offline
Ah, thank you. I thought it might be something like that. It doesn't make my script work but it does get rid of the error. I'll keep working on it.
For the strength of the pack is the wolf, and the strength of the wolf is the pack.
Offline
Guess you need an else after usage().
if [ $# -ne 4 ]; then
usage()
else
#while loop
fi
Edit: Since you did have an exit in usage(), you can also write it like this:
if [ $# -ne 4 ]; then
usage()
fi
#while loop
Last edited by PJ (2008-01-20 20:50:59)
Offline
Guess you need an else after usage().
if [ $# -ne 4 ]; then usage() else #while loop fi
I tried that but got another syntax error:
./myscript: line 20: syntax error near unexpected token `else'
./myscript: line 20: `else'
Edit: Since you did have an exit in usage(), you can also write it like this:
if [ $# -ne 4 ]; then usage() fi #while loop
Tried that too. Same syntax error except it's on the fi.
Last edited by Thrillhouse (2008-01-20 21:04:19)
For the strength of the pack is the wolf, and the strength of the wolf is the pack.
Offline
My fault, I sholdn't have used a comment between an else an a fi in the example I did submit. The comment is actually positioned where you should put your code (or a call to a function) if there is 4 parameters. I made the assumtion that the while loop was actually the code that should be used if there were 4 parameters. Looking at your code again I see that it some kind off error handling and I think you can remove it since it doesn't have a real purpose. You are calling exit before this code, which means that it will not be executed.
Offline
I understood what you meant. I do have an actual while loop there and it is the ideal situation. You're right, if there are four parameters, I want to enter the while loop and do what the script should do. I just put the trap in to handle ctrl-c's . Yet, I still get the errors even if I comment out the trap.
For the strength of the pack is the wolf, and the strength of the wolf is the pack.
Offline
Guess I have to post a working example then:
helptext() {
echo "help text"
}
whenfourparameters() {
echo $1 $2 $3 $4
}
if [ $# -ne 4 ]; then
helptext
else
whenfourparameters $@
fi
edit: It seems like I overlooked the parentheses regarding the function call. Remove those and hopefully it will work.
Last edited by PJ (2008-01-20 21:22:36)
Offline
There we go. That seems to have done it. I don't know why but putting the loop in a function and then calling it from the else seems to have done the trick. I'm not sure I understand that, though. One would think it doesn't matter how it's called. Oh well, I must have just had something wrong in the syntax.
Thank you, sir.
For the strength of the pack is the wolf, and the strength of the wolf is the pack.
Offline
Pages: 1