LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 02-12-2016, 09:27 PM   #1
krapykreme
LQ Newbie
 
Registered: Feb 2016
Posts: 8

Rep: Reputation: Disabled
Writing a shell program in C. How to EXIT?


Hello, I am implementing the exit function in my custom shell program. When I input exit however, it does not return to the initial command terminal. I would really appreciate if anyone can tell me what I am doing wrong. Here is my code:

Code:
if(strcmp(args[0], "exit") == 0){ 
        printf("Bye\n");
        exit(0);
    }
 
Old 02-13-2016, 12:52 AM   #2
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,622
Blog Entries: 40

Rep: Reputation: Disabled
Quote:
Originally Posted by krapykreme View Post
Hello, I am implementing the exit function in my custom shell program. When I input exit however, it does not return to the initial command terminal.
What is happening, instead?
 
Old 02-13-2016, 01:19 AM   #3
krapykreme
LQ Newbie
 
Registered: Feb 2016
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Michael Uplawski View Post
What is happening, instead?
It is just showing the command prompt, instead of exiting to the terminal prompt. But I think I have figure out why. I think it is because when I input a command like "cd" or "pwd", the position at args[0] stores it, and is not overwritten by exit. I need to figure a way to to overwrite it.

Sorry if I am not clearly communicating.
 
Old 02-13-2016, 01:26 AM   #4
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,622
Blog Entries: 40

Rep: Reputation: Disabled
Quote:
Originally Posted by krapykreme View Post
It is just showing the command prompt, instead of exiting to the terminal prompt. But I think I have figure out why. I think it is because when I input a command like "cd" or "pwd", the position at args[0] stores it, and is not overwritten by exit. I need to figure a way to to overwrite it.

Sorry if I am not clearly communicating.
Do not worry. But you provided a missing piece of information, in deed. ;-)
Have you seen the manpage on exit (man 3 exit)? I am not in your project, but feel that there is some nice hint for you in the documentation for the command. You may need to “prepare” the call to exit in a way that will permit the termination of the program in the way you want.
 
1 members found this post helpful.
Old 02-13-2016, 03:05 AM   #5
krapykreme
LQ Newbie
 
Registered: Feb 2016
Posts: 8

Original Poster
Rep: Reputation: Disabled
It works! Thanks
 
Old 02-13-2016, 09:51 AM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
these two terms are euphemisms
Quote:
it is just showing the command prompt, instead of exiting to the terminal prompt.
a command prompt is within the terminal, hense it can be called a terminal prompt. so thats confusing me.

but, this is how I exit my scripts (for the most part)

Code:
if [[ -z "$DIRNAME" ]] ; then
echo "DIRNAME "$DIRNAME" is empty Yo"
exit 1
fi
it kicks it out of the script and leaves me at the command prompt in the trerminal I'm using.

Last edited by BW-userx; 02-13-2016 at 09:57 AM.
 
1 members found this post helpful.
Old 02-13-2016, 10:00 AM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by krapykreme View Post
It is just showing the command prompt, instead of exiting to the terminal prompt. But I think I have figure out why. I think it is because when I input a command like "cd" or "pwd", the position at args[0] stores it, and is not overwritten by exit. I need to figure a way to to overwrite it.

Sorry if I am not clearly communicating.
before calling exit, reset it to where ever or whatever this terminal prompt is then call exit?

Code:
if(strcmp(args[0], "exit") == 0){ 
        printf("Bye\n");

        working_dir="/path/to/wherever"

        exit(0);
    }
just an idea.

Last edited by BW-userx; 02-13-2016 at 10:02 AM.
 
2 members found this post helpful.
Old 02-13-2016, 10:07 AM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by krapykreme View Post
It works! Thanks
dang I missed this post,
plubish how you did it, then make solved. ples
 
1 members found this post helpful.
Old 02-13-2016, 01:35 PM   #9
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,622
Blog Entries: 40

Rep: Reputation: Disabled
Cool

Quote:
Originally Posted by BW-userx View Post
these two terms are euphemisms
a command prompt is within the terminal, hense it can be called a terminal prompt. so thats confusing me.
:-)) When krapykreme writes about her/his “custom shell program” you should just read “shell”, i.e. a kind of replacement for bash, dash and the like, probably with custom behaviour and not “full-featured”. It renders the exit() in deed quite interesting, though not overly complicated.

But I wondered in the same way, at first.

Last edited by Michael Uplawski; 02-13-2016 at 01:40 PM.
 
Old 03-10-2016, 06:04 PM   #10
krapykreme
LQ Newbie
 
Registered: Feb 2016
Posts: 8

Original Poster
Rep: Reputation: Disabled
[QUOTE=BW-userx;5499606]Sorry, I am still learning the ways of the forum. Thanks for your help.
 
Old 03-10-2016, 06:13 PM   #11
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
This has been a gripping thread, but I still have no idea what OP did to solve the problem. Can anyone enlighten me please?
 
Old 03-11-2016, 06:21 AM   #12
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by BW-userx View Post
before calling exit, reset it to where ever or whatever this terminal prompt is then call exit?

Code:
if(strcmp(args[0], "exit") == 0){ 
        printf("Bye\n");

        working_dir="/path/to/wherever"

        exit(0);
    }
just an idea.
no need. The working directory of the parent process id cannot be affected by the child process.
 
  


Reply



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
Exit shell script NotionCommotion Linux - Newbie 8 06-23-2013 09:35 AM
Find the exit code of a program from within a shell script?? yanom Programming 2 09-30-2011 11:47 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
Help writing shell program mral Linux - Newbie 6 12-29-2005 08:26 PM
exit GDM to a shell PlatinumRik Linux - Software 2 04-18-2005 12:31 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:29 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration