LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   "exit" in bash .bashrc procedure/shell function exits xterm (http://www.linuxquestions.org/questions/linux-newbie-8/exit-in-bash-bashrc-procedure-shell-function-exits-xterm-714462/)

anonguy9 03-25-2009 08:59 PM

"exit" in bash .bashrc procedure/shell function exits xterm
 
editing ~/.bashrc I append this:

Code:

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..

grizly 03-25-2009 09:36 PM

Do you have super-vision? or is your computer as slow as an ENIAC?.. you won't have time to see that before the terminal disappears.. ;)

grizly 03-25-2009 10:13 PM

Quote:

Originally Posted by anonguy9 (Post 3487926)
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.

Ahh.. you are looking for case: http://linuxhelp.blogspot.com/2005/1...ash-shell.html

Also, you don't want to use exit at all.. it closes the xterm. ;)

If you change it to a case, then you don't need an exit at all.

Code:

testfunction(){

case "foo" in
 "foo") echo "Foo equals itself!";;
*)echo "This should not be seen";;
esac

Tested on Centos 5.. ;)

anonguy9 03-25-2009 11:50 PM

No, using case is not what I want either.

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.

Tinkster 03-26-2009 12:11 AM

But break terminates your loop ... that's what break is
there for. If you just want to sit there do a "read something".

vindoan 03-26-2009 12:13 AM

Try using return in your function

Code:

#!/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


eascown3 03-26-2009 12:18 AM

Here are your functions as they should be
 
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"
}

kiran431 03-26-2009 12:22 AM

I have a prob with this system back end tools ....i m not able to update any of my packages

E: system-tools-backends: subprocess post-installation script returned error exit status 1

kiran431 03-26-2009 12:25 AM

update failed
 
E: system-tools-backends: subprocess post-installation script returned error exit status 1


this is my prob where i cant update my packages can u help me out

anonguy9 03-26-2009 10:58 AM

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 (Post 3488051)
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 (Post 3488055)
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 (Post 3488058)
I have a prob with this system back end tools ....i m not able to update any of my packages

Please start a new thread for a new topic.

Whippy 05-19-2009 05:23 AM

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.

jrogers316 07-13-2011 03:54 PM

Put it in a subshell
 
Just put the entire function in a subshell, so when the exit command is run, it exits the subshell (and in turn sets the exit status)

Code:

function foo {
(
 exit 123
)
}

Code:

$ foo
$ echo $?
123
$

(notice the parenthesis ())


All times are GMT -5. The time now is 11:44 PM.