LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux 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

Reply
 
Search this Thread
Old 03-25-2009, 08:59 PM   #1
anonguy9
LQ Newbie
 
Registered: Mar 2009
Posts: 25

Rep: Reputation: 16
"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..
 
Old 03-25-2009, 09:36 PM   #2
grizly
Member
 
Registered: Nov 2006
Location: Melbourne Australia
Distribution: Centos, RHEL, Debian, Ubuntu, Mint
Posts: 128

Rep: Reputation: 16
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..

Last edited by grizly; 03-25-2009 at 10:13 PM.
 
Old 03-25-2009, 10:13 PM   #3
grizly
Member
 
Registered: Nov 2006
Location: Melbourne Australia
Distribution: Centos, RHEL, Debian, Ubuntu, Mint
Posts: 128

Rep: Reputation: 16
Quote:
Originally Posted by anonguy9 View Post
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..
 
Old 03-25-2009, 11:50 PM   #4
anonguy9
LQ Newbie
 
Registered: Mar 2009
Posts: 25

Original Poster
Rep: Reputation: 16
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.
 
Old 03-26-2009, 12:11 AM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,974
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
But break terminates your loop ... that's what break is
there for. If you just want to sit there do a "read something".
 
Old 03-26-2009, 12:13 AM   #6
vindoan
LQ Newbie
 
Registered: Mar 2009
Posts: 23

Rep: Reputation: 17
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
 
Old 03-26-2009, 12:18 AM   #7
eascown3
LQ Newbie
 
Registered: Mar 2009
Posts: 4

Rep: Reputation: 0
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"
}

Last edited by eascown3; 03-26-2009 at 12:22 AM. Reason: needed it
 
Old 03-26-2009, 12:22 AM   #8
kiran431
LQ Newbie
 
Registered: Mar 2009
Posts: 2

Rep: Reputation: 0
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
 
Old 03-26-2009, 12:25 AM   #9
kiran431
LQ Newbie
 
Registered: Mar 2009
Posts: 2

Rep: Reputation: 0
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
 
Old 03-26-2009, 10:58 AM   #10
anonguy9
LQ Newbie
 
Registered: Mar 2009
Posts: 25

Original Poster
Rep: Reputation: 16
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 View Post
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 View Post
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 View Post
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.

Last edited by anonguy9; 03-26-2009 at 10:59 AM.
 
Old 05-19-2009, 05:23 AM   #11
Whippy
LQ Newbie
 
Registered: May 2009
Posts: 2

Rep: Reputation: 0
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.
 
Old 07-13-2011, 03:54 PM   #12
jrogers316
LQ Newbie
 
Registered: Jul 2011
Posts: 1

Rep: Reputation: Disabled
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 ())
 
  


Reply

Tags
bash, bashrc, exit, function, functions, kills, procedure, shell, xterm


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ns:"error when calling class OldSim"&tclsh:"invalid command+child process exits abn." shojaru Linux - Newbie 0 03-05-2009 04:23 AM
editing startup script of bash -".bashrc" raju.mopidevi Suse/Novell 1 02-04-2009 10:17 AM
"Have to automate the SSH login procedure through shell scripting" sorav Programming 2 11-21-2007 04:53 AM
Assembly, "bash: ./exit: cannot execute binary file" User Name. Programming 4 08-07-2007 09:09 AM
How do I customised shell ".bashrc" or set keyboard options? Mimicafe Linux - General 2 01-11-2004 06:38 AM


All times are GMT -5. The time now is 03:53 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration