How do I get the exit status when using the sed command?
up vote
8
down vote
favorite
The grep
command gives out an exit status:
$echo "foo.bar" | grep -o foo
foo
$echo $?
0
$echo "foo.bar" | grep -o pop
$echo $?
1
But I need to use sed
and I realized that it has no exit status:
$echo "foo.bar" | sed 's/bar.*$//'
foo.
$echo $?
0
$echo "foo.bar" | sed 's/pop.*$//'
foo.bar
$echo $?
0
I know that I should play around with the -q
option, but I have not succeeded.
command-line sed
add a comment |Â
up vote
8
down vote
favorite
The grep
command gives out an exit status:
$echo "foo.bar" | grep -o foo
foo
$echo $?
0
$echo "foo.bar" | grep -o pop
$echo $?
1
But I need to use sed
and I realized that it has no exit status:
$echo "foo.bar" | sed 's/bar.*$//'
foo.
$echo $?
0
$echo "foo.bar" | sed 's/pop.*$//'
foo.bar
$echo $?
0
I know that I should play around with the -q
option, but I have not succeeded.
command-line sed
7
Note thatsed
most certainly does have an exit status, it just doesn't do what you need here. If thesed
command fails, for example, if you try to run it on a file you don't have write access to or one that doesn't exist,sed
will exit with a non-0 exit status. The exit status just indicates whethersed
managed to do what you told it to do, andecho "foo.bar" | sed 's/pop.*$//'
was correctly executed. It deleted all lines withpop
. That there were no such lines is irrelevant, the command still worked.
â terdonâ¦
May 16 at 13:20
@Zanna I was going to post an answer, but I felt this doesn't actually answer the question. It only explains a misconception, but doesn't provide a helpful solution like steeldriver has below. So a comment seemed more appropriate.
â terdonâ¦
May 16 at 15:17
add a comment |Â
up vote
8
down vote
favorite
up vote
8
down vote
favorite
The grep
command gives out an exit status:
$echo "foo.bar" | grep -o foo
foo
$echo $?
0
$echo "foo.bar" | grep -o pop
$echo $?
1
But I need to use sed
and I realized that it has no exit status:
$echo "foo.bar" | sed 's/bar.*$//'
foo.
$echo $?
0
$echo "foo.bar" | sed 's/pop.*$//'
foo.bar
$echo $?
0
I know that I should play around with the -q
option, but I have not succeeded.
command-line sed
The grep
command gives out an exit status:
$echo "foo.bar" | grep -o foo
foo
$echo $?
0
$echo "foo.bar" | grep -o pop
$echo $?
1
But I need to use sed
and I realized that it has no exit status:
$echo "foo.bar" | sed 's/bar.*$//'
foo.
$echo $?
0
$echo "foo.bar" | sed 's/pop.*$//'
foo.bar
$echo $?
0
I know that I should play around with the -q
option, but I have not succeeded.
command-line sed
edited Jun 13 at 18:53
K7AAY
3,73221443
3,73221443
asked May 16 at 11:18
Josef Klimuk
542112
542112
7
Note thatsed
most certainly does have an exit status, it just doesn't do what you need here. If thesed
command fails, for example, if you try to run it on a file you don't have write access to or one that doesn't exist,sed
will exit with a non-0 exit status. The exit status just indicates whethersed
managed to do what you told it to do, andecho "foo.bar" | sed 's/pop.*$//'
was correctly executed. It deleted all lines withpop
. That there were no such lines is irrelevant, the command still worked.
â terdonâ¦
May 16 at 13:20
@Zanna I was going to post an answer, but I felt this doesn't actually answer the question. It only explains a misconception, but doesn't provide a helpful solution like steeldriver has below. So a comment seemed more appropriate.
â terdonâ¦
May 16 at 15:17
add a comment |Â
7
Note thatsed
most certainly does have an exit status, it just doesn't do what you need here. If thesed
command fails, for example, if you try to run it on a file you don't have write access to or one that doesn't exist,sed
will exit with a non-0 exit status. The exit status just indicates whethersed
managed to do what you told it to do, andecho "foo.bar" | sed 's/pop.*$//'
was correctly executed. It deleted all lines withpop
. That there were no such lines is irrelevant, the command still worked.
â terdonâ¦
May 16 at 13:20
@Zanna I was going to post an answer, but I felt this doesn't actually answer the question. It only explains a misconception, but doesn't provide a helpful solution like steeldriver has below. So a comment seemed more appropriate.
â terdonâ¦
May 16 at 15:17
7
7
Note that
sed
most certainly does have an exit status, it just doesn't do what you need here. If the sed
command fails, for example, if you try to run it on a file you don't have write access to or one that doesn't exist, sed
will exit with a non-0 exit status. The exit status just indicates whether sed
managed to do what you told it to do, and echo "foo.bar" | sed 's/pop.*$//'
was correctly executed. It deleted all lines with pop
. That there were no such lines is irrelevant, the command still worked.â terdonâ¦
May 16 at 13:20
Note that
sed
most certainly does have an exit status, it just doesn't do what you need here. If the sed
command fails, for example, if you try to run it on a file you don't have write access to or one that doesn't exist, sed
will exit with a non-0 exit status. The exit status just indicates whether sed
managed to do what you told it to do, and echo "foo.bar" | sed 's/pop.*$//'
was correctly executed. It deleted all lines with pop
. That there were no such lines is irrelevant, the command still worked.â terdonâ¦
May 16 at 13:20
@Zanna I was going to post an answer, but I felt this doesn't actually answer the question. It only explains a misconception, but doesn't provide a helpful solution like steeldriver has below. So a comment seemed more appropriate.
â terdonâ¦
May 16 at 15:17
@Zanna I was going to post an answer, but I felt this doesn't actually answer the question. It only explains a misconception, but doesn't provide a helpful solution like steeldriver has below. So a comment seemed more appropriate.
â terdonâ¦
May 16 at 15:17
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
12
down vote
accepted
You can use qn to quit with exit status n - but to make that useful, you will also need to use some Branching and Flow Control:
t
branch conditionally (that is: jump to a label) only if as///
command has succeeded since the last input line was read or another
conditional branch was taken.
It is probably best to choose a value for n that is distinct from one of the standard exit status values:
An exit status of zero indicates success, and a nonzero value
indicates failure. GNU 'sed' returns the following exit status error
values:0
Successful completion.
1
Invalid command, invalid syntax, invalid regular expression or a
GNU 'sed' extension command used with '--posix'.
2
One or more of the input file specified on the command line could
not be opened (e.g. if a file is not found, or read permission is
denied). Processing continued with other files.
4
An I/O error, or a serious processing error during runtime, GNU
'sed' aborted immediately.
So for example
$ echo "foo.bar" | sed 's/bar.*$//; t; q42' ; echo $?
foo.
0
whereas
$ echo "foo.bar" | sed 's/baz.*$//; t; q42' ; echo $?
foo.bar
42
If you want to omit the default printing of the pattern space, then replace q
by Q
(note that Q
is a GNU extension).
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
12
down vote
accepted
You can use qn to quit with exit status n - but to make that useful, you will also need to use some Branching and Flow Control:
t
branch conditionally (that is: jump to a label) only if as///
command has succeeded since the last input line was read or another
conditional branch was taken.
It is probably best to choose a value for n that is distinct from one of the standard exit status values:
An exit status of zero indicates success, and a nonzero value
indicates failure. GNU 'sed' returns the following exit status error
values:0
Successful completion.
1
Invalid command, invalid syntax, invalid regular expression or a
GNU 'sed' extension command used with '--posix'.
2
One or more of the input file specified on the command line could
not be opened (e.g. if a file is not found, or read permission is
denied). Processing continued with other files.
4
An I/O error, or a serious processing error during runtime, GNU
'sed' aborted immediately.
So for example
$ echo "foo.bar" | sed 's/bar.*$//; t; q42' ; echo $?
foo.
0
whereas
$ echo "foo.bar" | sed 's/baz.*$//; t; q42' ; echo $?
foo.bar
42
If you want to omit the default printing of the pattern space, then replace q
by Q
(note that Q
is a GNU extension).
add a comment |Â
up vote
12
down vote
accepted
You can use qn to quit with exit status n - but to make that useful, you will also need to use some Branching and Flow Control:
t
branch conditionally (that is: jump to a label) only if as///
command has succeeded since the last input line was read or another
conditional branch was taken.
It is probably best to choose a value for n that is distinct from one of the standard exit status values:
An exit status of zero indicates success, and a nonzero value
indicates failure. GNU 'sed' returns the following exit status error
values:0
Successful completion.
1
Invalid command, invalid syntax, invalid regular expression or a
GNU 'sed' extension command used with '--posix'.
2
One or more of the input file specified on the command line could
not be opened (e.g. if a file is not found, or read permission is
denied). Processing continued with other files.
4
An I/O error, or a serious processing error during runtime, GNU
'sed' aborted immediately.
So for example
$ echo "foo.bar" | sed 's/bar.*$//; t; q42' ; echo $?
foo.
0
whereas
$ echo "foo.bar" | sed 's/baz.*$//; t; q42' ; echo $?
foo.bar
42
If you want to omit the default printing of the pattern space, then replace q
by Q
(note that Q
is a GNU extension).
add a comment |Â
up vote
12
down vote
accepted
up vote
12
down vote
accepted
You can use qn to quit with exit status n - but to make that useful, you will also need to use some Branching and Flow Control:
t
branch conditionally (that is: jump to a label) only if as///
command has succeeded since the last input line was read or another
conditional branch was taken.
It is probably best to choose a value for n that is distinct from one of the standard exit status values:
An exit status of zero indicates success, and a nonzero value
indicates failure. GNU 'sed' returns the following exit status error
values:0
Successful completion.
1
Invalid command, invalid syntax, invalid regular expression or a
GNU 'sed' extension command used with '--posix'.
2
One or more of the input file specified on the command line could
not be opened (e.g. if a file is not found, or read permission is
denied). Processing continued with other files.
4
An I/O error, or a serious processing error during runtime, GNU
'sed' aborted immediately.
So for example
$ echo "foo.bar" | sed 's/bar.*$//; t; q42' ; echo $?
foo.
0
whereas
$ echo "foo.bar" | sed 's/baz.*$//; t; q42' ; echo $?
foo.bar
42
If you want to omit the default printing of the pattern space, then replace q
by Q
(note that Q
is a GNU extension).
You can use qn to quit with exit status n - but to make that useful, you will also need to use some Branching and Flow Control:
t
branch conditionally (that is: jump to a label) only if as///
command has succeeded since the last input line was read or another
conditional branch was taken.
It is probably best to choose a value for n that is distinct from one of the standard exit status values:
An exit status of zero indicates success, and a nonzero value
indicates failure. GNU 'sed' returns the following exit status error
values:0
Successful completion.
1
Invalid command, invalid syntax, invalid regular expression or a
GNU 'sed' extension command used with '--posix'.
2
One or more of the input file specified on the command line could
not be opened (e.g. if a file is not found, or read permission is
denied). Processing continued with other files.
4
An I/O error, or a serious processing error during runtime, GNU
'sed' aborted immediately.
So for example
$ echo "foo.bar" | sed 's/bar.*$//; t; q42' ; echo $?
foo.
0
whereas
$ echo "foo.bar" | sed 's/baz.*$//; t; q42' ; echo $?
foo.bar
42
If you want to omit the default printing of the pattern space, then replace q
by Q
(note that Q
is a GNU extension).
edited May 16 at 23:32
answered May 16 at 11:29
steeldriver
62.1k1196163
62.1k1196163
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1036912%2fhow-do-i-get-the-exit-status-when-using-the-sed-command%23new-answer', 'question_page');
);
Post as a guest
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
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
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
7
Note that
sed
most certainly does have an exit status, it just doesn't do what you need here. If thesed
command fails, for example, if you try to run it on a file you don't have write access to or one that doesn't exist,sed
will exit with a non-0 exit status. The exit status just indicates whethersed
managed to do what you told it to do, andecho "foo.bar" | sed 's/pop.*$//'
was correctly executed. It deleted all lines withpop
. That there were no such lines is irrelevant, the command still worked.â terdonâ¦
May 16 at 13:20
@Zanna I was going to post an answer, but I felt this doesn't actually answer the question. It only explains a misconception, but doesn't provide a helpful solution like steeldriver has below. So a comment seemed more appropriate.
â terdonâ¦
May 16 at 15:17