LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-11-2010, 05:44 PM   #1
Star_Gazer
Member
 
Registered: Aug 2009
Location: Virginia, United States
Distribution: openSUSE 11.2 KDE
Posts: 32

Rep: Reputation: 15
exit in script files cause konsole terminal itself to exit


I noticed that if I have "exit" in a bash script file., e.g.
script.sh,
that when the word "exit" is reached, and the script file being executed is not in the PATH environment, i.e.
". script.sh",
the whole konsole shell profile is exited! What gives here? Is there another command compatible to "exit" to prevent this, or will I just have the leave the "." part in the PATH enviroment, which is, to my understanding, is not recommended? I desire for a "goto" function in bash script files.

Thanks,
Clifton
 
Old 04-11-2010, 05:54 PM   #2
GazL
Senior Member
 
Registered: May 2008
Posts: 3,480

Rep: Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016
It's because your sourcing. You should be starting it with:
./script.sh

(no space between the . and the slash)
 
Old 04-11-2010, 07:04 PM   #3
Star_Gazer
Member
 
Registered: Aug 2009
Location: Virginia, United States
Distribution: openSUSE 11.2 KDE
Posts: 32

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by GazL View Post
It's because your sourcing. You should be starting it with:
./script.sh

(no space between the . and the slash)
Thanks. As I recall, I attempted that method, and got the same problem. I am using openSUSE 11.2 KDE 4.42, Konsole version 2.42, if that means anything.

Clifton
 
Old 04-11-2010, 07:40 PM   #4
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by Star_Gazer View Post
Thanks. As I recall, I attempted that method, and got the same problem.
Have you tried it again, to be sure? A call to exit closing your terminal session is exactly what you'd expect from sourcing with ". script.sh", and if this isn't cured by using "./script.sh", that's a major problem.

If you try the alternate method and it doesn't work, please post the offending script (or, preferably, a minimal example that demonstrates the same behaviour).
 
Old 04-11-2010, 08:05 PM   #5
Star_Gazer
Member
 
Registered: Aug 2009
Location: Virginia, United States
Distribution: openSUSE 11.2 KDE
Posts: 32

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by JohnGraham View Post
Have you tried it again, to be sure? A call to exit closing your terminal session is exactly what you'd expect from sourcing with ". script.sh", and if this isn't cured by using "./script.sh", that's a major problem.

If you try the alternate method and it doesn't work, please post the offending script (or, preferably, a minimal example that demonstrates the same behaviour).
I took the ":." out of the PATH variable to test the script file again, and got this:
Code:
/media/drawers/practice/awkgrepsed $ ./log_gets.sh
bash: ./log_gets.sh: Permission denied
/media/drawers/practice/awkgrepsed $
That is strange, because the file is executable (-rwxr-xr-x). This is just a practice script file for educating myself. It is small. Here it is:

Code:
#!/bin/bash
if [ -z $1 ]; then
        echo "ERROR!"
        echo "You must specify valid file specs."
        echo "e.g. $(basename $0) ex100409.log ..."
        echo ""
        sleep 2
        exit 1
else
        LOGFILNAM=$1
fi

if [ -n $LOGFILNAM ]; then
grep -o -h -E 'GET /.+\.html? \W+ ' $LOGFILNAM | sed 's/GET //1' | sed 's/ \-//g' | sort
fi
But I just put the ':." part back into the PATH environment, and still get the same problems as noted above. Maybe it is the settings for the drive in the fstab?

Thanks,
Clifton
 
Old 04-11-2010, 08:15 PM   #6
Star_Gazer
Member
 
Registered: Aug 2009
Location: Virginia, United States
Distribution: openSUSE 11.2 KDE
Posts: 32

Original Poster
Rep: Reputation: 15
Fascinating... I moved the files to the ~/Documents/Practice directory, and
Code:
./log_gets.sh
log_gets.sh
work fine, yet:
Code:
. log_gets.sh
does not. It exits Konsole. I have been using Linux since May of last year, and haven't encounter these pecularities.

Clifton
 
Old 04-12-2010, 12:22 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,557
Blog Entries: 28

Rep: Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178
Quote:
Originally Posted by Star_Gazer View Post
Fascinating... I moved the files to the ~/Documents/Practice directory, and
Code:
./log_gets.sh
log_gets.sh
work fine, yet:
Code:
. log_gets.sh
does not. It exits Konsole.
That is normal. The first case runs the script in a sub-shell and its "exit" exits the sub-shell returning you to the first shell. The second case runs the script it your current shell and its exit does the same a typing exit at the command prompt -- it terminates the shell.

Are you clear about the difference between ./log_gets.sh and . log_gets.sh? They are very different.

Last edited by catkin; 04-12-2010 at 06:28 AM. Reason: exist -> exits
 
1 members found this post helpful.
Old 04-12-2010, 05:09 AM   #8
GazL
Senior Member
 
Registered: May 2008
Posts: 3,480

Rep: Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016
The filesystem containing /media/drawers/practice/awkgrepsed may have been mounted with the noexec option, That would explain why you get permission denied even when it's marked executable and works when moved elsewhere.
 
1 members found this post helpful.
Old 04-12-2010, 08:04 AM   #9
Star_Gazer
Member
 
Registered: Aug 2009
Location: Virginia, United States
Distribution: openSUSE 11.2 KDE
Posts: 32

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by catkin View Post
That is normal. The first case runs the script in a sub-shell and its "exit" exits the sub-shell returning you to the first shell. The second case runs the script it your current shell and its exit does the same a typing exit at the command prompt -- it terminates the shell.
Ah, I see. I'll have to remember that. That makes good sense, especially if one is executing a script outside the shell (i.e. clicking on the filename in Dolphin, Konqueror, or Krusader).

Quote:
Are you clear about the difference between ./log_gets.sh and . log_gets.sh? They are very different.
Not really. All I remember is that "if the file to execute is not in a directory listed in the PATH environment variable, use ". command" when in such a directory.

Thanks.
Clifton
 
Old 04-12-2010, 08:08 AM   #10
Star_Gazer
Member
 
Registered: Aug 2009
Location: Virginia, United States
Distribution: openSUSE 11.2 KDE
Posts: 32

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by GazL View Post
The filesystem containing /media/drawers/practice/awkgrepsed may have been mounted with the noexec option, That would explain why you get permission denied even when it's marked executable and works when moved elsewhere.
Yes... I thought the same. That drive does have the 'noexec' option in it.

Thanks,
Clifton
 
Old 04-12-2010, 09:05 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,557
Blog Entries: 28

Rep: Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178
Quote:
Originally Posted by Star_Gazer View Post
All I remember is that "if the file to execute is not in a directory listed in the PATH environment variable, use ". command" when in such a directory.
That's not correct; use ./command in that situation. The "." command is something different; it runs the commands in a script file within the current shell; it's useful for setting variables and defining aliases and functions; if the same script were run as ./command then all those settings would happen in a subshell and be lost when the subshell terminated.
 
1 members found this post helpful.
Old 04-12-2010, 09:32 AM   #12
Star_Gazer
Member
 
Registered: Aug 2009
Location: Virginia, United States
Distribution: openSUSE 11.2 KDE
Posts: 32

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by catkin View Post
That's not correct; use ./command in that situation. The "." command is something different; it runs the commands in a script file within the current shell; it's useful for setting variables and defining aliases and functions; if the same script were run as ./command then all those settings would happen in a subshell and be lost when the subshell terminated.
I understand better now - thank you.

Clifton
 
  


Reply

Tags
bash, exit, konsole


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
terminal hang after running a script and then exit fongthai Linux - General 1 06-04-2008 05:55 AM
Warning: at kernel/exit.c:814 do exit() (Tainted: P ) <- nVidia video problem? Hitboxx Fedora 2 10-06-2007 10:47 PM
Mandrake login won't exit terminal oxwilder Mandriva 2 10-12-2005 06:55 PM
how to exit a terminal changcheh Linux - General 3 08-30-2005 08:55 AM
I don't want the terminal to exit! dbcoder Linux - Software 15 07-29-2005 10:39 PM


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

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