Why can't I get pgrep output right to variable on bash script?2019 Community Moderator ElectionBash script doesn't TEE output to subdirectoryExiting a Bash script when a sudo child quitsConditional execution block with || and parentheses problemGet PID and return code from 1 line bash callWhy is “Doing an exit 130 is not the same as dying of SIGINT”?bash script: capturing tcp traffic on a remote server sometimes works, sometimes fails. No errorsWhat does typing a single exclamation mark do in Bash?Execute command and store everything to variable in bashWhy does bash 'read' exit with status 1?What does it mean when Duplicity exits with exit status 23?
Why do Radio Buttons not fill the entire outer circle?
A seasonal riddle
python displays `n` instead of breaking a line
Why didn’t Eve recognize the little cockroach as a living organism?
Not hide and seek
I keep switching characters, how do I stop?
What 1968 Moog synthesizer was used in the Movie Apollo 11?
Reason why a kingside attack is not justified
How do I lift the insulation blower into the attic?
C++ lambda syntax
When is the exact date for EOL of Ubuntu 14.04 LTS?
Asserting that Atheism and Theism are both faith based positions
How to preserve electronics (computers, ipads, phones) for hundreds of years?
Hashing password to increase entropy
Do people actually use the word "kaputt" in conversation?
What (if any) is the reason to buy in small local stores?
What should be the ideal length of sentences in a blog post for ease of reading?
Did I make a mistake by ccing email to boss to others?
Weird lines in Microsoft Word
Why is implicit conversion not ambiguous for non-primitive types?
In the event of Brexit being postponed beyond the EU elections, will UK voters in EU countries be eligible to participate?
Toggle window scroll bar
Should a narrator ever describe things based on a character's view instead of facts?
Do native speakers use "ultima" and "proxima" frequently in spoken English?
Why can't I get pgrep output right to variable on bash script?
2019 Community Moderator ElectionBash script doesn't TEE output to subdirectoryExiting a Bash script when a sudo child quitsConditional execution block with || and parentheses problemGet PID and return code from 1 line bash callWhy is “Doing an exit 130 is not the same as dying of SIGINT”?bash script: capturing tcp traffic on a remote server sometimes works, sometimes fails. No errorsWhat does typing a single exclamation mark do in Bash?Execute command and store everything to variable in bashWhy does bash 'read' exit with status 1?What does it mean when Duplicity exits with exit status 23?
I'm trying to make a script to either quit compton if it's running or start it if it's not running. I've read from man that it should exit 1 if process is found, so I've tried to make a script that uses that... However this just doesn't work, It starts if it's closed but doesn't close it. what am I doing wrong ??
#!/bin/bash
status=$(pgrep compton 2>&1)
if [[ $status == 1 ]];
then
killall compton
else
exec compton -b
fi
echo $status
bash stdout stderr exit-status pgrep
New contributor
add a comment |
I'm trying to make a script to either quit compton if it's running or start it if it's not running. I've read from man that it should exit 1 if process is found, so I've tried to make a script that uses that... However this just doesn't work, It starts if it's closed but doesn't close it. what am I doing wrong ??
#!/bin/bash
status=$(pgrep compton 2>&1)
if [[ $status == 1 ]];
then
killall compton
else
exec compton -b
fi
echo $status
bash stdout stderr exit-status pgrep
New contributor
0
or1
is exit status, but$(...)
captures output.
– Charles Duffy
1 min ago
add a comment |
I'm trying to make a script to either quit compton if it's running or start it if it's not running. I've read from man that it should exit 1 if process is found, so I've tried to make a script that uses that... However this just doesn't work, It starts if it's closed but doesn't close it. what am I doing wrong ??
#!/bin/bash
status=$(pgrep compton 2>&1)
if [[ $status == 1 ]];
then
killall compton
else
exec compton -b
fi
echo $status
bash stdout stderr exit-status pgrep
New contributor
I'm trying to make a script to either quit compton if it's running or start it if it's not running. I've read from man that it should exit 1 if process is found, so I've tried to make a script that uses that... However this just doesn't work, It starts if it's closed but doesn't close it. what am I doing wrong ??
#!/bin/bash
status=$(pgrep compton 2>&1)
if [[ $status == 1 ]];
then
killall compton
else
exec compton -b
fi
echo $status
bash stdout stderr exit-status pgrep
bash stdout stderr exit-status pgrep
New contributor
New contributor
edited 23 mins ago
Kusalananda
136k17257426
136k17257426
New contributor
asked 2 hours ago
TubeTube
61
61
New contributor
New contributor
0
or1
is exit status, but$(...)
captures output.
– Charles Duffy
1 min ago
add a comment |
0
or1
is exit status, but$(...)
captures output.
– Charles Duffy
1 min ago
0
or 1
is exit status, but $(...)
captures output.– Charles Duffy
1 min ago
0
or 1
is exit status, but $(...)
captures output.– Charles Duffy
1 min ago
add a comment |
2 Answers
2
active
oldest
votes
You should control exit status of pgrep process which will be in $? variable. Or check if $status variable where you're storing the output of pgrep is f.e. non zero-length string. The script in the question checks whether string in variable status is "1"
so
#!/bin/bash
pgrep compton >/dev/null
if [[ $? -eq 0 ]]
then
killall compton
else
exec compton -b
fi
or
#!/bin/bash
status=$(pgrep compton 2>&1)
if [[ -n "$status" ]]
then
killall compton
else
exec compton -b
fi
if pgrep compton >/dev/null; then
is the better practice, so one doesn't directly inspect$?
at all. Among other things, this means thatset -e
will no longer treatpgrep
returning 1 as cause to exit the script (because branching on its exit status marks it as "checked").
– Charles Duffy
45 secs ago
add a comment |
You are getting the pgrep
output in your status
variable. It's just not the output that you expect it to be.
pgrep
outputs the process IDs (PIDs) of the processes matching the pattern that you give it. If there is a process whose name matches compton
, then $status
would be the PID of that process, or of those processes. pgrep
also returns an exit status, but an exit status is not captured by a command substitution as a string.
In your test, you compare $status
against 1
. It is unlikely that compton
has PID 1.
If you want to kill any compton
process if they exist, and start compton -b
if no compton
process exists, you may do that with
#!/bin/sh
if ! pkill compton; then
exec compton -b
fi
This uses the exit status of pkill
. The pkill
tool works in an equivalent way to pgrep
(they are usually distributed and installed as a pair) but instead of outputting PIDs of matching processes like pgrep
would do, pkill
sends the TERM
signal (by default) to the matching processes.
The if
keyword uses the exit status of the command that you use with it.
The !
inverts the sense of the test so that
If
pkill compton
succeeds, it means that there was one or severalcompton
processes that have now been killed, or at least signalled, andexec compton -b
will not be executed.If
pkill compton
fails (no process matched the name, or there was some internal error inpkill
), the body of theif
statement would call yourexec compton -b
, which would replace the shell process with that ofcompton -b
.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Tube is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f507290%2fwhy-cant-i-get-pgrep-output-right-to-variable-on-bash-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should control exit status of pgrep process which will be in $? variable. Or check if $status variable where you're storing the output of pgrep is f.e. non zero-length string. The script in the question checks whether string in variable status is "1"
so
#!/bin/bash
pgrep compton >/dev/null
if [[ $? -eq 0 ]]
then
killall compton
else
exec compton -b
fi
or
#!/bin/bash
status=$(pgrep compton 2>&1)
if [[ -n "$status" ]]
then
killall compton
else
exec compton -b
fi
if pgrep compton >/dev/null; then
is the better practice, so one doesn't directly inspect$?
at all. Among other things, this means thatset -e
will no longer treatpgrep
returning 1 as cause to exit the script (because branching on its exit status marks it as "checked").
– Charles Duffy
45 secs ago
add a comment |
You should control exit status of pgrep process which will be in $? variable. Or check if $status variable where you're storing the output of pgrep is f.e. non zero-length string. The script in the question checks whether string in variable status is "1"
so
#!/bin/bash
pgrep compton >/dev/null
if [[ $? -eq 0 ]]
then
killall compton
else
exec compton -b
fi
or
#!/bin/bash
status=$(pgrep compton 2>&1)
if [[ -n "$status" ]]
then
killall compton
else
exec compton -b
fi
if pgrep compton >/dev/null; then
is the better practice, so one doesn't directly inspect$?
at all. Among other things, this means thatset -e
will no longer treatpgrep
returning 1 as cause to exit the script (because branching on its exit status marks it as "checked").
– Charles Duffy
45 secs ago
add a comment |
You should control exit status of pgrep process which will be in $? variable. Or check if $status variable where you're storing the output of pgrep is f.e. non zero-length string. The script in the question checks whether string in variable status is "1"
so
#!/bin/bash
pgrep compton >/dev/null
if [[ $? -eq 0 ]]
then
killall compton
else
exec compton -b
fi
or
#!/bin/bash
status=$(pgrep compton 2>&1)
if [[ -n "$status" ]]
then
killall compton
else
exec compton -b
fi
You should control exit status of pgrep process which will be in $? variable. Or check if $status variable where you're storing the output of pgrep is f.e. non zero-length string. The script in the question checks whether string in variable status is "1"
so
#!/bin/bash
pgrep compton >/dev/null
if [[ $? -eq 0 ]]
then
killall compton
else
exec compton -b
fi
or
#!/bin/bash
status=$(pgrep compton 2>&1)
if [[ -n "$status" ]]
then
killall compton
else
exec compton -b
fi
answered 2 hours ago
Jakub JindraJakub Jindra
307310
307310
if pgrep compton >/dev/null; then
is the better practice, so one doesn't directly inspect$?
at all. Among other things, this means thatset -e
will no longer treatpgrep
returning 1 as cause to exit the script (because branching on its exit status marks it as "checked").
– Charles Duffy
45 secs ago
add a comment |
if pgrep compton >/dev/null; then
is the better practice, so one doesn't directly inspect$?
at all. Among other things, this means thatset -e
will no longer treatpgrep
returning 1 as cause to exit the script (because branching on its exit status marks it as "checked").
– Charles Duffy
45 secs ago
if pgrep compton >/dev/null; then
is the better practice, so one doesn't directly inspect $?
at all. Among other things, this means that set -e
will no longer treat pgrep
returning 1 as cause to exit the script (because branching on its exit status marks it as "checked").– Charles Duffy
45 secs ago
if pgrep compton >/dev/null; then
is the better practice, so one doesn't directly inspect $?
at all. Among other things, this means that set -e
will no longer treat pgrep
returning 1 as cause to exit the script (because branching on its exit status marks it as "checked").– Charles Duffy
45 secs ago
add a comment |
You are getting the pgrep
output in your status
variable. It's just not the output that you expect it to be.
pgrep
outputs the process IDs (PIDs) of the processes matching the pattern that you give it. If there is a process whose name matches compton
, then $status
would be the PID of that process, or of those processes. pgrep
also returns an exit status, but an exit status is not captured by a command substitution as a string.
In your test, you compare $status
against 1
. It is unlikely that compton
has PID 1.
If you want to kill any compton
process if they exist, and start compton -b
if no compton
process exists, you may do that with
#!/bin/sh
if ! pkill compton; then
exec compton -b
fi
This uses the exit status of pkill
. The pkill
tool works in an equivalent way to pgrep
(they are usually distributed and installed as a pair) but instead of outputting PIDs of matching processes like pgrep
would do, pkill
sends the TERM
signal (by default) to the matching processes.
The if
keyword uses the exit status of the command that you use with it.
The !
inverts the sense of the test so that
If
pkill compton
succeeds, it means that there was one or severalcompton
processes that have now been killed, or at least signalled, andexec compton -b
will not be executed.If
pkill compton
fails (no process matched the name, or there was some internal error inpkill
), the body of theif
statement would call yourexec compton -b
, which would replace the shell process with that ofcompton -b
.
add a comment |
You are getting the pgrep
output in your status
variable. It's just not the output that you expect it to be.
pgrep
outputs the process IDs (PIDs) of the processes matching the pattern that you give it. If there is a process whose name matches compton
, then $status
would be the PID of that process, or of those processes. pgrep
also returns an exit status, but an exit status is not captured by a command substitution as a string.
In your test, you compare $status
against 1
. It is unlikely that compton
has PID 1.
If you want to kill any compton
process if they exist, and start compton -b
if no compton
process exists, you may do that with
#!/bin/sh
if ! pkill compton; then
exec compton -b
fi
This uses the exit status of pkill
. The pkill
tool works in an equivalent way to pgrep
(they are usually distributed and installed as a pair) but instead of outputting PIDs of matching processes like pgrep
would do, pkill
sends the TERM
signal (by default) to the matching processes.
The if
keyword uses the exit status of the command that you use with it.
The !
inverts the sense of the test so that
If
pkill compton
succeeds, it means that there was one or severalcompton
processes that have now been killed, or at least signalled, andexec compton -b
will not be executed.If
pkill compton
fails (no process matched the name, or there was some internal error inpkill
), the body of theif
statement would call yourexec compton -b
, which would replace the shell process with that ofcompton -b
.
add a comment |
You are getting the pgrep
output in your status
variable. It's just not the output that you expect it to be.
pgrep
outputs the process IDs (PIDs) of the processes matching the pattern that you give it. If there is a process whose name matches compton
, then $status
would be the PID of that process, or of those processes. pgrep
also returns an exit status, but an exit status is not captured by a command substitution as a string.
In your test, you compare $status
against 1
. It is unlikely that compton
has PID 1.
If you want to kill any compton
process if they exist, and start compton -b
if no compton
process exists, you may do that with
#!/bin/sh
if ! pkill compton; then
exec compton -b
fi
This uses the exit status of pkill
. The pkill
tool works in an equivalent way to pgrep
(they are usually distributed and installed as a pair) but instead of outputting PIDs of matching processes like pgrep
would do, pkill
sends the TERM
signal (by default) to the matching processes.
The if
keyword uses the exit status of the command that you use with it.
The !
inverts the sense of the test so that
If
pkill compton
succeeds, it means that there was one or severalcompton
processes that have now been killed, or at least signalled, andexec compton -b
will not be executed.If
pkill compton
fails (no process matched the name, or there was some internal error inpkill
), the body of theif
statement would call yourexec compton -b
, which would replace the shell process with that ofcompton -b
.
You are getting the pgrep
output in your status
variable. It's just not the output that you expect it to be.
pgrep
outputs the process IDs (PIDs) of the processes matching the pattern that you give it. If there is a process whose name matches compton
, then $status
would be the PID of that process, or of those processes. pgrep
also returns an exit status, but an exit status is not captured by a command substitution as a string.
In your test, you compare $status
against 1
. It is unlikely that compton
has PID 1.
If you want to kill any compton
process if they exist, and start compton -b
if no compton
process exists, you may do that with
#!/bin/sh
if ! pkill compton; then
exec compton -b
fi
This uses the exit status of pkill
. The pkill
tool works in an equivalent way to pgrep
(they are usually distributed and installed as a pair) but instead of outputting PIDs of matching processes like pgrep
would do, pkill
sends the TERM
signal (by default) to the matching processes.
The if
keyword uses the exit status of the command that you use with it.
The !
inverts the sense of the test so that
If
pkill compton
succeeds, it means that there was one or severalcompton
processes that have now been killed, or at least signalled, andexec compton -b
will not be executed.If
pkill compton
fails (no process matched the name, or there was some internal error inpkill
), the body of theif
statement would call yourexec compton -b
, which would replace the shell process with that ofcompton -b
.
edited 31 mins ago
answered 1 hour ago
KusalanandaKusalananda
136k17257426
136k17257426
add a comment |
add a comment |
Tube is a new contributor. Be nice, and check out our Code of Conduct.
Tube is a new contributor. Be nice, and check out our Code of Conduct.
Tube is a new contributor. Be nice, and check out our Code of Conduct.
Tube is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f507290%2fwhy-cant-i-get-pgrep-output-right-to-variable-on-bash-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
0
or1
is exit status, but$(...)
captures output.– Charles Duffy
1 min ago