LinuxQuestions.org
Review your favorite Linux distribution.
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 06-21-2013, 10:23 AM   #1
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 536

Rep: Reputation: Disabled
Exit shell script


I just entered the below script into my bash shell. Then, realized, I wanted 192.168.0..., and not 192.168.1. So, tried Ctrl-C, esc, etc, but I could not break out, and finally had to close the terminal. How does exit the shell script command? Thanks

Code:
for ip in $(seq 1 254); do ping -c 1 192.168.1.$ip>/dev/null; [ $? -eq 0 ] && echo "192.168.1.$ip UP" || : ; done
 
Old 06-21-2013, 10:33 AM   #2
mddnix
Member
 
Registered: Mar 2013
Location: Bangalore, India
Distribution: Redhat, Arch, Ubuntu
Posts: 506

Rep: Reputation: 139Reputation: 139
Better way to check whether hosts are up or not in subnet is by issuing
Code:
# nmap -sn 192.168.1.0/24
note: you may need to install nmap first (yum install nmap)
 
Old 06-21-2013, 10:38 AM   #3
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 536

Original Poster
Rep: Reputation: Disabled
Thanks mddesai,

Yes, it is better.

What about existing the shell script command? When I do cntr-c or esc, it just shows the escaped characters on the screen. I type exit, quit, etc, but I just see the words on the screen, but can't exit.
 
Old 06-21-2013, 10:46 AM   #4
mddnix
Member
 
Registered: Mar 2013
Location: Bangalore, India
Distribution: Redhat, Arch, Ubuntu
Posts: 506

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by NotionCommotion View Post
What about existing the shell script command? When I do cntr-c or esc, it just shows the escaped characters on the screen. I type exit, quit, etc, but I just see the words on the screen, but can't exit.
1. you can hit CTRL+Z to suspend running job.
2. enter 'jobs' at command prompt to check job number
3. then, enter kill %<number> to kill the job permanently
 
2 members found this post helpful.
Old 06-21-2013, 10:49 AM   #5
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 536

Original Poster
Rep: Reputation: Disabled
Thanks mddesai,

Worked perfect.

What was the purpose between the percent sign? For instance, kill %1?
 
Old 06-21-2013, 11:00 AM   #6
mddnix
Member
 
Registered: Mar 2013
Location: Bangalore, India
Distribution: Redhat, Arch, Ubuntu
Posts: 506

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by NotionCommotion View Post
What was the purpose between the percent sign? For instance, kill %1?
It means kill process by 'job number'. if % is excluded, then it means kill process by PID.

to check for both job number and PID, give 'jobs -l'.

ex:
kill %1 //kills process with job number 1
kill 4546 // kills process with PID 4546

Edit: For 'sure kill', you can give kill -9 4546

More: Job Control Commands

Last edited by mddnix; 06-21-2013 at 11:05 AM.
 
Old 06-21-2013, 11:50 AM   #7
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 536

Original Poster
Rep: Reputation: Disabled
Thanks mddesai,

kill 4546 didn't take care of it, but kill -9 4546 had the same effect as kill %1.

Sorry for digressing, but tried to get more info about kill and tried kill -h, but no help page.
 
Old 06-21-2013, 12:15 PM   #8
mddnix
Member
 
Registered: Mar 2013
Location: Bangalore, India
Distribution: Redhat, Arch, Ubuntu
Posts: 506

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by NotionCommotion View Post
kill 4546 didn't take care of it, but kill -9 4546 had the same effect as kill %1.
The syntax for kill is:
kill [signal] PID

When no signal is explicitly included in the command, signal 15, named SIGTERM, is sent by default. Once the process receives the notice, a few different things can happen:
  • the process may stop immediately
  • the process may stop after a short delay after cleaning up resources
  • the process may keep running indefinitely
If this fails, the stronger signal 9, called SIGKILL, should be used.

More:
The 3 most important "kill" signals on the Linux/UNIX command line
UNIX Kill Command Examples
SIGTERM vs. SIGKILL
 
Old 06-23-2013, 10:35 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
The kill you're using is almost certainly the bash built-in. See the JOB CONTROL section of the man page for details on how to use it, and more.


Incidentally, there's very little need for seq these days, now that advanced shells like bash have brace expansions that accept ranges. The [url=http://wiki.bash-hackers.org/syntax/ccmd/c_for]c-style for loop[/code] is another common option.

It's also recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://mywiki.wooledge.org/BashFAQ/031
http://mywiki.wooledge.org/ArithmeticExpression

Or in this specific case you could also just test the exit code directly:

Code:
for ip in 192.168.1.{1..254}; do

    if ping -c 1 "$ip" >/dev/null ; then
        echo "$ip UP"
    else
        echo "$ip DOWN"
    fi

done
 
  


Reply


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
Shell Script running in another shell script and then exit dreamervlk Linux - General 3 09-16-2011 07:40 AM
exit mail in shell script j-me Linux - General 1 02-15-2010 01:28 PM
shell script fails to exit twrig3846 Linux - Newbie 11 02-10-2010 01:10 AM
how to exit out of the calling Shell Script harimac Linux - Newbie 3 08-19-2009 02:49 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 03:34 PM


All times are GMT -5. The time now is 08:25 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration