"exit" in bash .bashrc procedure/shell function exits xterm
Linux - NewbieThis forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
testfunction() {
if [ "foo" = "foo" ]; then
echo "Foo does equal itself!"
# This kills xterm too! I just want to stop here, and not display "This should not be seen."
exit
fi
echo "This should not be seen."
}
Within X, I open a new xterm, which loads in this new .bashrc.
I type "testfunction" and my xterm window is killed.
I am expecting to see "Foo does equal itself!" and then my prompt.
I've searched and searched, and I think that I just don't know the lingo to cobble together the right phrase to find an answer.
Is "exit" the right thing to use in this way inside of a procedure? I suspect not.
I understand how to code things differently to avoid this, but the entire point of doing things in this way is so that I don't have to have a billion nested ifs. I really really hate that.
Would appreciate a working example, and a pointer to some documentation..
I am expecting to see "Foo does equal itself!" and then my prompt.
...
Is "exit" the right thing to use in this way inside of a procedure? I suspect not.
...
so that I don't have to have a billion nested ifs. I really really hate that.
Imagine that this code is much larger and includes many other for/case structures.
Those many instances of "if" or "case" would all being forcibly nested. The indenting alone would make me grind my teeth.
BUT, I did some thinking, and came up with using "break" in this manner:
Code:
testfunction() {
until [ "sky" = "falling" ]; do
if [ "foo" = "foo" ]; then
echo "Foo does equal itself!"
# This kills xterm too! I just want to stop here, and not display "This should not be seen."
break
fi
echo "This should not be seen."
done
}
I'm sure some will say this is wrong and evil, but it looks to be a good solution for me. I can make many little if structures and bail out of my procedure without killing xterm.
I may also be able to turn the if structures into run-once mini procedures. Perhaps there are some other tricks, but I think this solution saves any extra typing and only gives one indentation level.
#!/bin/bash
testfunction() {
until [ "sky" = "falling" ]; do
if [ "foo" = "foo" ]; then
echo "Foo does equal itself!"
# This kills xterm too! I just want to stop here
return 0
fi
echo "This should not be seen."
done
}
#main
echo This is a test line.
testfunction
Because you are making these "functions" for your SHELL by placing them in your
.bashrc, an "exit" statement causes your shell to exit, killing your xterm.
Also, in the tfunction, you were giving the until loop a condition that could NEVER
be satisfied: until [ "sky" = "falling" ] -- this is a comparison between two character
strings that can NEVER be equal. I changed your string "sky" to the variable ${sky}.
Try them now
#This WON'T kill your xterm
testfunction() {
if [ "foo" = "foo" ]
then
echo "Foo does equal itself!"
else
echo "This should not be seen."
fi
}
#This one now has an exit condition
tfunction() {
sky="sky"
until [ ${sky} = "falling" ]
do
if [ "foo" = "foo" ]
then
echo "Foo does equal itself!"
sky="falling"
else
echo "This should not be seen."
fi
done
echo "I am out of tfunction"
}
Last edited by eascown3; 03-26-2009 at 01:22 AM..
Reason: needed it
My use of "break" works great, and I was able to make a rather cool script out of it, finally scratching a 10 year old itch. =)
Quote:
Originally Posted by vindoan
Try using return in your function
This does work in the test case given. But it does not work if I do something a bit more complex like this:
Code:
#!/bin/bash
testfunction() {
suicide() { return 0 ; }
until [ "sky" = "falling" ]; do
if [ "foo" = "foo" ]; then
echo "Foo does equal itself!"
# This kills xterm too! I just want to stop here
suicide
fi
echo "This should not be seen."
done
}
#main
echo This is a test line.
testfunction
I'm expecting it to abort the script. I'd have to have some additional coding to pick up the return code and act accordingly. But then I'd still be stuck with additional "if" structures.
Quote:
Originally Posted by eascown3
Also, in the tfunction, you were giving the until loop a condition that could NEVER be satisfied: until [ "sky" = "falling" ] -- this is a comparison between two character strings that can NEVER be equal.
This was intentional. I simply put a break right at the end so that it iterates only once.
But I do understand what your example is showing. If I can set the condition for "while" to abort, will it abort immediately or will it continue the rest of that iteration?
In your example, I do end up seeing "I am out of tfunction", but I don't want that. I want it to halt the rest of that iteration.
Quote:
Originally Posted by kiran431
I have a prob with this system back end tools ....i m not able to update any of my packages
I have a feeling you would better off creating a script file called testfunction and ensuring that it is on your path.
Then exit will work fine.
I agree with you about the depth problem and using exit like this (usually with status 1) is a good way to terminate the script if there is a problem with the parameters, a missing file etc.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.