Skip to main content
added 351 characters in body
Source Link
Utku
  • 1.5k
  • 2
  • 15
  • 30

UPDATE:

I have changed the grep $1 part to grep '$1' (while I was trying to mean grep "$1") in the script and this time I got the

kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

message (instead of the Terminated: 15 message). I don't understand what's going on.

QUESTION:

I have written a simple shell script named mykill.

mykill:

#!/bin/sh

kill `ps -A | grep $1 | grep -v 'grep' | grep -Eom 1 '^[0-9]+'`

However, there is a weird behavior. When I write the line:

kill `ps -A | grep process_name | grep -v 'grep' | grep -Eom 1 '^[0-9]+'`

manually on bash, if nothing comes up as the output of ps -A | grep process_name, I get the following:

kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

If something comes up, the command executes correctly and terminates silently.

Now, if I run the script by executing the mykill file, if something comes up as the output of ps -A | grep process_name, the script executes correctly and terminates silently, which is the same behavior as executing the command manually.

But if nothing comes up as the output of ps -A | grep process_name, I don't get the message about the usage of the kill command. Instead, I get:

Terminated: 15

I have also checked out the return codes. After I try to terminate a non existent process by manually writing the command on the shell, echo $? gives 1. However, after I try to terminate a non existent process by calling the script, echo $? gives 143.

What's going on here? Why am I observing different behaviors when executing the same command by manually writing it on the shell, vs executing it within a shell script?

NOTE: Both sh and my working shell are bash.

BONUS: Could my shell script be written in a more efficient and/or elegant way, using only POSIX utilities? If so, how?

I have written a simple shell script named mykill.

mykill:

#!/bin/sh

kill `ps -A | grep $1 | grep -v 'grep' | grep -Eom 1 '^[0-9]+'`

However, there is a weird behavior. When I write the line:

kill `ps -A | grep process_name | grep -v 'grep' | grep -Eom 1 '^[0-9]+'`

manually on bash, if nothing comes up as the output of ps -A | grep process_name, I get the following:

kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

If something comes up, the command executes correctly and terminates silently.

Now, if I run the script by executing the mykill file, if something comes up as the output of ps -A | grep process_name, the script executes correctly and terminates silently, which is the same behavior as executing the command manually.

But if nothing comes up as the output of ps -A | grep process_name, I don't get the message about the usage of the kill command. Instead, I get:

Terminated: 15

I have also checked out the return codes. After I try to terminate a non existent process by manually writing the command on the shell, echo $? gives 1. However, after I try to terminate a non existent process by calling the script, echo $? gives 143.

What's going on here? Why am I observing different behaviors when executing the same command by manually writing it on the shell, vs executing it within a shell script?

NOTE: Both sh and my working shell are bash.

BONUS: Could my shell script be written in a more efficient and/or elegant way, using only POSIX utilities? If so, how?

UPDATE:

I have changed the grep $1 part to grep '$1' (while I was trying to mean grep "$1") in the script and this time I got the

kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

message (instead of the Terminated: 15 message). I don't understand what's going on.

QUESTION:

I have written a simple shell script named mykill.

mykill:

#!/bin/sh

kill `ps -A | grep $1 | grep -v 'grep' | grep -Eom 1 '^[0-9]+'`

However, there is a weird behavior. When I write the line:

kill `ps -A | grep process_name | grep -v 'grep' | grep -Eom 1 '^[0-9]+'`

manually on bash, if nothing comes up as the output of ps -A | grep process_name, I get the following:

kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

If something comes up, the command executes correctly and terminates silently.

Now, if I run the script by executing the mykill file, if something comes up as the output of ps -A | grep process_name, the script executes correctly and terminates silently, which is the same behavior as executing the command manually.

But if nothing comes up as the output of ps -A | grep process_name, I don't get the message about the usage of the kill command. Instead, I get:

Terminated: 15

I have also checked out the return codes. After I try to terminate a non existent process by manually writing the command on the shell, echo $? gives 1. However, after I try to terminate a non existent process by calling the script, echo $? gives 143.

What's going on here? Why am I observing different behaviors when executing the same command by manually writing it on the shell, vs executing it within a shell script?

NOTE: Both sh and my working shell are bash.

BONUS: Could my shell script be written in a more efficient and/or elegant way, using only POSIX utilities? If so, how?

edited tags
Link
Utku
  • 1.5k
  • 2
  • 15
  • 30
added 112 characters in body
Source Link
Utku
  • 1.5k
  • 2
  • 15
  • 30

I have written a simple shell script named mykill.

mykill:

#!/bin/sh

kill `ps -A | grep $1 | grep -v 'grep' | grep -Eom 1 '^[0-9]+'`

However, there is a weird behavior. When I write the line:

kill `ps -A | grep process_name | grep -v 'grep' | grep -Eom 1 '^[0-9]+'`

manually on bash, if nothing comes up as the output of ps -A | grep process_name, I get the following:

kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

If something comes up, the command executes correctly and terminates silently.

Now, if I run the script by executing the mykill file, if something comes up as the output of ps -A | grep process_name, the script executes correctly and terminates silently, which is the same behavior as executing the command manually.

But if nothing comes up as the output of ps -A | grep process_name, I don't get the message about the usage of the kill command. Instead, I get:

Terminated: 15

I have also checked out the return codes. After I try to terminate a non existent process by manually writing the command on the shell, echo $? gives 1. However, after I try to terminate a non existent process by calling the script, echo $? gives 143.

What's going on here? Why am I observing different behaviors when executing the same command by manually writing it on the shell, vs executing it within a shell script?

NOTE: Both sh and my working shell are bash.

BONUS: Could my shell script be written in a more efficient and/or elegant way, using only POSIX utilities? If so, how?

I have written a simple shell script named mykill.

mykill:

#!/bin/sh

kill `ps -A | grep $1 | grep -v 'grep' | grep -Eom 1 '^[0-9]+'`

However, there is a weird behavior. When I write the line:

kill `ps -A | grep process_name | grep -v 'grep' | grep -Eom 1 '^[0-9]+'`

manually on bash, if nothing comes up as the output of ps -A | grep process_name, I get the following:

kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

If something comes up, the command executes correctly and terminates silently.

Now, if I run the script by executing the mykill file, if something comes up as the output of ps -A | grep process_name, the script executes correctly and terminates silently, which is the same behavior as executing the command manually.

But if nothing comes up as the output of ps -A | grep process_name, I don't get the message about the usage of the kill command. Instead, I get:

Terminated: 15

I have also checked out the return codes. After I try to terminate a non existent process by manually writing the command on the shell, echo $? gives 1. However, after I try to terminate a non existent process by calling the script, echo $? gives 143.

What's going on here? Why am I observing different behaviors when executing the same command by manually writing it on the shell, vs executing it within a shell script?

NOTE: Both sh and my working shell are bash.

BONUS: Could my shell script be written in a more efficient and/or elegant way? If so, how?

I have written a simple shell script named mykill.

mykill:

#!/bin/sh

kill `ps -A | grep $1 | grep -v 'grep' | grep -Eom 1 '^[0-9]+'`

However, there is a weird behavior. When I write the line:

kill `ps -A | grep process_name | grep -v 'grep' | grep -Eom 1 '^[0-9]+'`

manually on bash, if nothing comes up as the output of ps -A | grep process_name, I get the following:

kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

If something comes up, the command executes correctly and terminates silently.

Now, if I run the script by executing the mykill file, if something comes up as the output of ps -A | grep process_name, the script executes correctly and terminates silently, which is the same behavior as executing the command manually.

But if nothing comes up as the output of ps -A | grep process_name, I don't get the message about the usage of the kill command. Instead, I get:

Terminated: 15

I have also checked out the return codes. After I try to terminate a non existent process by manually writing the command on the shell, echo $? gives 1. However, after I try to terminate a non existent process by calling the script, echo $? gives 143.

What's going on here? Why am I observing different behaviors when executing the same command by manually writing it on the shell, vs executing it within a shell script?

NOTE: Both sh and my working shell are bash.

BONUS: Could my shell script be written in a more efficient and/or elegant way, using only POSIX utilities? If so, how?

Source Link
Utku
  • 1.5k
  • 2
  • 15
  • 30
Loading