Timeline for Killing multiple process by process name using shell script
Current License: CC BY-SA 4.0
14 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jun 29, 2022 at 11:55 | comment | added | Shashank Gb | @StéphaneChazelas \@ilkkachu Gotcha !!! lots of info, thx | |
| Jun 29, 2022 at 11:44 | comment | added | ilkkachu | See Why does my shell script choke on whitespace or other special characters? and How can we run a command stored in a variable? | |
| Jun 29, 2022 at 11:43 | comment | added | ilkkachu |
@ShashankGb, ... well, yes. That's not an array, that's a string. You are initializing temp as an array (with temp=()) and then only using the first element of the array. That's what the comments above tried to tell you. There is a difference, and you're only getting away with it because your values don't contain whitespace so you can rely on word splitting without messing everything up. You could remove the initial assignment, or change it to just temp=. Or heck, you could change it to temp=( '' foo bar ), and it'd still do the same. Assuming Bash for the whole time, of course.
|
|
| Jun 29, 2022 at 11:35 | comment | added | Shashank Gb |
@ilkkachu I am using temp as an array and appending each pid(with space) to temp in for. Then at last, I am sending those pid's to kill command
|
|
| Jun 29, 2022 at 9:37 | comment | added | Stéphane Chazelas |
@ilkkachu, Ah, I hadn't seen the " " appended at the end of awk's output. Yes, then $temp (same as ${temp[0]}) would then be subject to split+glob in bash as it's not quoted, and assuming $IFS contains the space character and doesn't contain digits, it should work.
|
|
| Jun 29, 2022 at 9:19 | comment | added | ilkkachu |
as long as that jps .. | awk pipeline outputs just a process id number, it should work in Bash (in the usual cases) even with using temp as a scalar and relying on word-splitting. But then the temp=() initialization is confusing, as you're not using temp as an array here.
|
|
| Jun 29, 2022 at 9:14 | history | rollback | ilkkachu |
Rollback to Revision 1
|
|
| Jun 29, 2022 at 9:13 | history | edited | ilkkachu | CC BY-SA 4.0 |
quotes
|
| Jun 29, 2022 at 8:41 | comment | added | Stéphane Chazelas |
Note that the re in grep stands for regular expression, so . would still be treated as a regexp operator there.
|
|
| Jun 29, 2022 at 8:40 | comment | added | Stéphane Chazelas |
Your code would only work in the zsh shell. In ksh93/bash, you'd need to quote all the variables and also use temp+=( "$(...)" ) instead of temp+=$(...) which for those shells would be the same as temp[0]+=$(...). In zsh, you can also use ${(j[|])argv} to join the positional parameters with | without having to modify $IFS.
|
|
| Jun 29, 2022 at 8:38 | comment | added | Stéphane Chazelas |
java may invoke a shell to interpret some shell code such as yourscript name1 name2, where those process names are indeed space-separated, but that's just because it happens to be that in the shell language, space is used to delimit command arguments. Your script will not receive the process names space separated it will receive them as different arguments ($1, $2...) and "$*" can be used to join those arguments with the first character of $IFS.
|
|
| Jun 29, 2022 at 8:26 | vote | accept | Shashank Gb | ||
| Jun 29, 2022 at 8:26 | |||||
| S Jun 29, 2022 at 8:26 | review | First answers | |||
| Jul 13, 2022 at 8:26 | |||||
| S Jun 29, 2022 at 8:26 | history | answered | Shashank Gb | CC BY-SA 4.0 |