LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Exit shell script (https://www.linuxquestions.org/questions/linux-newbie-8/exit-shell-script-4175466880/)

NotionCommotion 06-21-2013 09:23 AM

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

Madhu Desai 06-21-2013 09:33 AM

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)

NotionCommotion 06-21-2013 09:38 AM

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.

Madhu Desai 06-21-2013 09:46 AM

Quote:

Originally Posted by NotionCommotion (Post 4976083)
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

NotionCommotion 06-21-2013 09:49 AM

Thanks mddesai,

Worked perfect.

What was the purpose between the percent sign? For instance, kill %1?

Madhu Desai 06-21-2013 10:00 AM

Quote:

Originally Posted by NotionCommotion (Post 4976092)
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

NotionCommotion 06-21-2013 10:50 AM

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.

Madhu Desai 06-21-2013 11:15 AM

Quote:

Originally Posted by NotionCommotion (Post 4976140)
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

David the H. 06-23-2013 09:35 AM

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



All times are GMT -5. The time now is 05:46 AM.