[colug-432] sed basics

Rick Hornsby richardjhornsby at gmail.com
Fri May 15 09:32:11 EDT 2015


> On May 15, 2015, at 07:59, Zach Villers <zachvatwork at gmail.com> wrote:
> 
> I'm kind of a newb as well, but are you looking for;
> 
> Exit 1;
> 
> To stop the script?

Yep.  'exit' (watch your case, probably just DYAC here) inside a script will stop the script, it will not close the terminal.  Also, you don't need the semicolon in bash.

The clue for why this won't close your terminal is in the very first line of your script - the shebang: #!/bin/bash.  When the script runs, a new shell to execute the script is launched.  That's what you're exiting.  Thus, exit in a script sends you back to your login shell.  If you type exit on the command line, you're telling bash to exit the login shell you're running in, which then as you said has the effect of logging you off and closing the PuTTY window.

This goes beyond the original question, but is relevant to the topic - 

The number after exit is optional, and is referred to as the exit code or return value, in the range of 0-255.  If you do not specify, it is zero.  Generally speaking when writing 'exit' in a script you should specify the exit code to make it clear to the reader (and yourself in a few months) what you meant -

Convention is that 0 (zero) is a normal exit, no problems. Non-zero more or less means something went wrong.  "Went wrong" does not mean a catastrophic failure, but it can - depends entirely on the author of the script/command.  80-85% of what I do I either exit 0 when things are good, and exit 1 when things are not good.  I only occasionally use other exit values.

Here's a simple example of how grep uses the exit code ($?) to communicate information (### comments are mine):

skylane:~ rhornsby$ cat foo
apple
orange
peach
skylane:~ rhornsby$ grep apple foo
apple
skylane:~ rhornsby$ echo $?
0     ### no problem
skylane:~ rhornsby$ grep watermelon foo
skylane:~ rhornsby$ echo $?
1     ### problem ("watermelon" was not found)


Note - $? only returns the exit value of the *last* command.  Note what happens if I continue from where I left off above:

skylane:~ rhornsby$ echo $?
0

Wait - that variable $? was "1" just a minute ago, what happened?  It is now "0" because the last command executed (right above where I show that watermelon was not found) was echo - which completed successfully.


> I am working through the Learn to script tutorial at bash.cyberciti.biz/guide/Main_Page . You might find that helpful.

Cyberciti has some good stuff.


-rj

> Sent from my iPhone
> 
>> On May 15, 2015, at 8:23 AM, Steve VanSlyck <s.vanslyck at postpro.net> wrote:
>> 
>> Good advice.
>> 
>> Problem is the eyes/stomach differential. I want to do a lot and all the
>> lawyering during the day interferes.
>> 
>> Here's something I've googled without finding a simple answer.
>> 
>> #1/bin/bash
>> command to be tested
>> untested pwd command
>> untested ls command
>> untested unintentionally silently destroy my remote backups command
>> untested unintentionally silently send colug all my money command
>> untested unintentionally silently destroy my server command
>> 
>> Is there a command can enter between "command being tested" and
>> "untested pwd command" to stop the script and return to the terminal to
>> avoid running the rest of the script? "Exit," I understand, will close
>> the terminal, which I don't want to do.
>> 
>> A blank space is between PermitRootLogin and the no or yes.
>> 
>> 
>> 
>>> On Thu, May 14, 2015, at 23:00, jep200404 at columbus.rr.com wrote:
>>> First, try the boring way:
>>> 
>>>   sed 's...' sshd_config >sshd_config.new
>>>   diff sshd_config sshd_config.new
>> 
>> _______________________________________________
>> colug-432 mailing list
>> colug-432 at colug.net
>> http://lists.colug.net/mailman/listinfo/colug-432
> 
> _______________________________________________
> colug-432 mailing list
> colug-432 at colug.net
> http://lists.colug.net/mailman/listinfo/colug-432




More information about the colug-432 mailing list