Originally posted by jaimese
I have no idea about the meaning of this line 'set -e' and itï¿½s not included in the code. Iï¿½m going to try it.
Anyway, the $eq variable means the destination ip formatted as "10.1.X.X".
The output line from the ps is somthing like this 'root 25306 23477 0 10:37 pts/2 00:00:00 telnet 10.1.6.5 40000'. I filter it looking for this ip, avoiding the lines that has the words grep or ping (the script has to try connectivity before making the connections)
Okay, I think I got your problem. Try modifying the line like this:
id=$(ps -ef | grep $eq | grep -v 'grep' | grep -v 'ping' | sed -e 's,\t, ,g' -e 's, *, ,g' | cut -f2 -d' ')
What happened? The 'cut' command expects tab characters for column separation by default. In your example that somehow returned the whole line. The kill command then looked like this:
kill -9 root 25306 23477 0 10:37 pts/2 00:00:00 telnet 10.1.6.5 40000
In this case, kill tried to kill the following processes: 25306, 23477, 0, 40000. The 23477 is the PARENT process of the telnet process: your script.
Or in other words: all parameters were checked, and if they were integer numbers, the process will be killed. Had you not used 2>/dev/null you would have seen error messages about the remaining parameters.
With the new code I sent you above the output from ps is changed to turn tabs into spaces, consecutives spaces into one space and then cut is instructed to use a single space as column separator. This then returns only the second column, the process id you want.
set -e means: stop this script if any command returns non-zero status, ie: fails.
Another command you might be interested in: set -x
This means that each command is printed to stderr before it is executed. This can help a lot