How to capture error message from executed command?
I was tasked to create an automated server hardening script and one thing that they need is a report of all the output of each command executed. I want to store the error message inside a string and append it in a text file.
Let's say I ran this command:
/sbin/modprobe -n -v hfsplus
The output of running this in my machine would be:
FATAL: Module hfsplus not found
How can I store that error message inside a string? Any help would be greatly appreciated. Thanks!
bash scripting string
add a comment |
I was tasked to create an automated server hardening script and one thing that they need is a report of all the output of each command executed. I want to store the error message inside a string and append it in a text file.
Let's say I ran this command:
/sbin/modprobe -n -v hfsplus
The output of running this in my machine would be:
FATAL: Module hfsplus not found
How can I store that error message inside a string? Any help would be greatly appreciated. Thanks!
bash scripting string
I tried running this command: var=$(/sbin/modprobe -n -v hfsplush) And then displaying it: $var But it still doesn't capture the error message inside the string.
– Miguel Roque
May 29 '14 at 7:42
add a comment |
I was tasked to create an automated server hardening script and one thing that they need is a report of all the output of each command executed. I want to store the error message inside a string and append it in a text file.
Let's say I ran this command:
/sbin/modprobe -n -v hfsplus
The output of running this in my machine would be:
FATAL: Module hfsplus not found
How can I store that error message inside a string? Any help would be greatly appreciated. Thanks!
bash scripting string
I was tasked to create an automated server hardening script and one thing that they need is a report of all the output of each command executed. I want to store the error message inside a string and append it in a text file.
Let's say I ran this command:
/sbin/modprobe -n -v hfsplus
The output of running this in my machine would be:
FATAL: Module hfsplus not found
How can I store that error message inside a string? Any help would be greatly appreciated. Thanks!
bash scripting string
bash scripting string
asked May 29 '14 at 7:25
Miguel RoqueMiguel Roque
1954414
1954414
I tried running this command: var=$(/sbin/modprobe -n -v hfsplush) And then displaying it: $var But it still doesn't capture the error message inside the string.
– Miguel Roque
May 29 '14 at 7:42
add a comment |
I tried running this command: var=$(/sbin/modprobe -n -v hfsplush) And then displaying it: $var But it still doesn't capture the error message inside the string.
– Miguel Roque
May 29 '14 at 7:42
I tried running this command: var=$(/sbin/modprobe -n -v hfsplush) And then displaying it: $var But it still doesn't capture the error message inside the string.
– Miguel Roque
May 29 '14 at 7:42
I tried running this command: var=$(/sbin/modprobe -n -v hfsplush) And then displaying it: $var But it still doesn't capture the error message inside the string.
– Miguel Roque
May 29 '14 at 7:42
add a comment |
6 Answers
6
active
oldest
votes
you can do it by redirecting errors command:
/sbin/modprobe -n -v hfsplus 2> fileName
as a script
#!/bin/bash
errormessage=$( /sbin/modprobe -n -v hfsplus 2>&1)
echo $errormessage
or
#!/bin/bash
errormessage=`/sbin/modprobe -n -v hfsplus 2>&1 `
echo $errormessage
if you want to append the error use >>
instead of >
Make sure to use 2>&1
and not 2> &1
to avoid the error
"syntax error near unexpected token `&'"
I've tried that approach and it stores it DIRECTLY in the text file. I want it to store inside a string first so I can format the contents easily.
– Miguel Roque
May 29 '14 at 7:45
1
@MiguelRoque see updates
– Networker
May 29 '14 at 7:46
1
I tried putting the output inside a HEREDOC and it worked also. Thanks a lot @Networker!
– Miguel Roque
May 29 '14 at 7:52
1
Someone reverted the edit I did, because I had a "syntax error near &" and removed the space after >. A justification would have been nice.
– Pierre.Sassoulas
Jan 12 '18 at 10:08
I tried to edit because : Make sure to use 2>&1 and not 2> &1 to avoid the error "syntax error near unexpected token `&'"
– peter_v
Jun 28 '18 at 8:14
add a comment |
Simply to store as a string in bash script:
X=`/sbin/modprobe -n -v hfsplus 2>&1`
echo $X
This can be a bit better as you will see messages when command is executed:
TMP=$(mktemp)
/sbin/modprobe -n -v hfsplus 2>&1 | tee $TMP
OUTPUT=$(cat $TMP)
echo $OUTPUT
rm $TMP
1
Always use $(command) instead of backticks for command substitution. It is better :)
– Sree
Feb 13 '15 at 7:02
add a comment |
I capture error like this
. ${file} 2>&1 | {
read -d "" -t 0.01 error
[ -z "$error" ] || log_warn Load completion ${file} failed: "n${error}"
}
if source failed, I will capture the error and log it.log_warn is just a simple function.
BTW, I use this in my dotfiles
add a comment |
To append to a file use /sbin/modprobe -n -v hfsplus 2>> filename
add a comment |
Newer bash versions (I.e. bash 4.1+):
$ msg=$(ls -la nofile 2>&1)
$ echo $msg
ls: cannot access nofile: No such file or directory
$
add a comment |
To return the error message in a variable, simply;
error=$(/sbin/modprobe -n -v hfsplus 2>&1 1>/dev/null)
echo $error
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
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%2f132511%2fhow-to-capture-error-message-from-executed-command%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
you can do it by redirecting errors command:
/sbin/modprobe -n -v hfsplus 2> fileName
as a script
#!/bin/bash
errormessage=$( /sbin/modprobe -n -v hfsplus 2>&1)
echo $errormessage
or
#!/bin/bash
errormessage=`/sbin/modprobe -n -v hfsplus 2>&1 `
echo $errormessage
if you want to append the error use >>
instead of >
Make sure to use 2>&1
and not 2> &1
to avoid the error
"syntax error near unexpected token `&'"
I've tried that approach and it stores it DIRECTLY in the text file. I want it to store inside a string first so I can format the contents easily.
– Miguel Roque
May 29 '14 at 7:45
1
@MiguelRoque see updates
– Networker
May 29 '14 at 7:46
1
I tried putting the output inside a HEREDOC and it worked also. Thanks a lot @Networker!
– Miguel Roque
May 29 '14 at 7:52
1
Someone reverted the edit I did, because I had a "syntax error near &" and removed the space after >. A justification would have been nice.
– Pierre.Sassoulas
Jan 12 '18 at 10:08
I tried to edit because : Make sure to use 2>&1 and not 2> &1 to avoid the error "syntax error near unexpected token `&'"
– peter_v
Jun 28 '18 at 8:14
add a comment |
you can do it by redirecting errors command:
/sbin/modprobe -n -v hfsplus 2> fileName
as a script
#!/bin/bash
errormessage=$( /sbin/modprobe -n -v hfsplus 2>&1)
echo $errormessage
or
#!/bin/bash
errormessage=`/sbin/modprobe -n -v hfsplus 2>&1 `
echo $errormessage
if you want to append the error use >>
instead of >
Make sure to use 2>&1
and not 2> &1
to avoid the error
"syntax error near unexpected token `&'"
I've tried that approach and it stores it DIRECTLY in the text file. I want it to store inside a string first so I can format the contents easily.
– Miguel Roque
May 29 '14 at 7:45
1
@MiguelRoque see updates
– Networker
May 29 '14 at 7:46
1
I tried putting the output inside a HEREDOC and it worked also. Thanks a lot @Networker!
– Miguel Roque
May 29 '14 at 7:52
1
Someone reverted the edit I did, because I had a "syntax error near &" and removed the space after >. A justification would have been nice.
– Pierre.Sassoulas
Jan 12 '18 at 10:08
I tried to edit because : Make sure to use 2>&1 and not 2> &1 to avoid the error "syntax error near unexpected token `&'"
– peter_v
Jun 28 '18 at 8:14
add a comment |
you can do it by redirecting errors command:
/sbin/modprobe -n -v hfsplus 2> fileName
as a script
#!/bin/bash
errormessage=$( /sbin/modprobe -n -v hfsplus 2>&1)
echo $errormessage
or
#!/bin/bash
errormessage=`/sbin/modprobe -n -v hfsplus 2>&1 `
echo $errormessage
if you want to append the error use >>
instead of >
Make sure to use 2>&1
and not 2> &1
to avoid the error
"syntax error near unexpected token `&'"
you can do it by redirecting errors command:
/sbin/modprobe -n -v hfsplus 2> fileName
as a script
#!/bin/bash
errormessage=$( /sbin/modprobe -n -v hfsplus 2>&1)
echo $errormessage
or
#!/bin/bash
errormessage=`/sbin/modprobe -n -v hfsplus 2>&1 `
echo $errormessage
if you want to append the error use >>
instead of >
Make sure to use 2>&1
and not 2> &1
to avoid the error
"syntax error near unexpected token `&'"
edited Jun 28 '18 at 10:43
peter_v
1034
1034
answered May 29 '14 at 7:42
NetworkerNetworker
6,017104069
6,017104069
I've tried that approach and it stores it DIRECTLY in the text file. I want it to store inside a string first so I can format the contents easily.
– Miguel Roque
May 29 '14 at 7:45
1
@MiguelRoque see updates
– Networker
May 29 '14 at 7:46
1
I tried putting the output inside a HEREDOC and it worked also. Thanks a lot @Networker!
– Miguel Roque
May 29 '14 at 7:52
1
Someone reverted the edit I did, because I had a "syntax error near &" and removed the space after >. A justification would have been nice.
– Pierre.Sassoulas
Jan 12 '18 at 10:08
I tried to edit because : Make sure to use 2>&1 and not 2> &1 to avoid the error "syntax error near unexpected token `&'"
– peter_v
Jun 28 '18 at 8:14
add a comment |
I've tried that approach and it stores it DIRECTLY in the text file. I want it to store inside a string first so I can format the contents easily.
– Miguel Roque
May 29 '14 at 7:45
1
@MiguelRoque see updates
– Networker
May 29 '14 at 7:46
1
I tried putting the output inside a HEREDOC and it worked also. Thanks a lot @Networker!
– Miguel Roque
May 29 '14 at 7:52
1
Someone reverted the edit I did, because I had a "syntax error near &" and removed the space after >. A justification would have been nice.
– Pierre.Sassoulas
Jan 12 '18 at 10:08
I tried to edit because : Make sure to use 2>&1 and not 2> &1 to avoid the error "syntax error near unexpected token `&'"
– peter_v
Jun 28 '18 at 8:14
I've tried that approach and it stores it DIRECTLY in the text file. I want it to store inside a string first so I can format the contents easily.
– Miguel Roque
May 29 '14 at 7:45
I've tried that approach and it stores it DIRECTLY in the text file. I want it to store inside a string first so I can format the contents easily.
– Miguel Roque
May 29 '14 at 7:45
1
1
@MiguelRoque see updates
– Networker
May 29 '14 at 7:46
@MiguelRoque see updates
– Networker
May 29 '14 at 7:46
1
1
I tried putting the output inside a HEREDOC and it worked also. Thanks a lot @Networker!
– Miguel Roque
May 29 '14 at 7:52
I tried putting the output inside a HEREDOC and it worked also. Thanks a lot @Networker!
– Miguel Roque
May 29 '14 at 7:52
1
1
Someone reverted the edit I did, because I had a "syntax error near &" and removed the space after >. A justification would have been nice.
– Pierre.Sassoulas
Jan 12 '18 at 10:08
Someone reverted the edit I did, because I had a "syntax error near &" and removed the space after >. A justification would have been nice.
– Pierre.Sassoulas
Jan 12 '18 at 10:08
I tried to edit because : Make sure to use 2>&1 and not 2> &1 to avoid the error "syntax error near unexpected token `&'"
– peter_v
Jun 28 '18 at 8:14
I tried to edit because : Make sure to use 2>&1 and not 2> &1 to avoid the error "syntax error near unexpected token `&'"
– peter_v
Jun 28 '18 at 8:14
add a comment |
Simply to store as a string in bash script:
X=`/sbin/modprobe -n -v hfsplus 2>&1`
echo $X
This can be a bit better as you will see messages when command is executed:
TMP=$(mktemp)
/sbin/modprobe -n -v hfsplus 2>&1 | tee $TMP
OUTPUT=$(cat $TMP)
echo $OUTPUT
rm $TMP
1
Always use $(command) instead of backticks for command substitution. It is better :)
– Sree
Feb 13 '15 at 7:02
add a comment |
Simply to store as a string in bash script:
X=`/sbin/modprobe -n -v hfsplus 2>&1`
echo $X
This can be a bit better as you will see messages when command is executed:
TMP=$(mktemp)
/sbin/modprobe -n -v hfsplus 2>&1 | tee $TMP
OUTPUT=$(cat $TMP)
echo $OUTPUT
rm $TMP
1
Always use $(command) instead of backticks for command substitution. It is better :)
– Sree
Feb 13 '15 at 7:02
add a comment |
Simply to store as a string in bash script:
X=`/sbin/modprobe -n -v hfsplus 2>&1`
echo $X
This can be a bit better as you will see messages when command is executed:
TMP=$(mktemp)
/sbin/modprobe -n -v hfsplus 2>&1 | tee $TMP
OUTPUT=$(cat $TMP)
echo $OUTPUT
rm $TMP
Simply to store as a string in bash script:
X=`/sbin/modprobe -n -v hfsplus 2>&1`
echo $X
This can be a bit better as you will see messages when command is executed:
TMP=$(mktemp)
/sbin/modprobe -n -v hfsplus 2>&1 | tee $TMP
OUTPUT=$(cat $TMP)
echo $OUTPUT
rm $TMP
edited May 29 '14 at 8:06
answered May 29 '14 at 7:47
graphitegraphite
41136
41136
1
Always use $(command) instead of backticks for command substitution. It is better :)
– Sree
Feb 13 '15 at 7:02
add a comment |
1
Always use $(command) instead of backticks for command substitution. It is better :)
– Sree
Feb 13 '15 at 7:02
1
1
Always use $(command) instead of backticks for command substitution. It is better :)
– Sree
Feb 13 '15 at 7:02
Always use $(command) instead of backticks for command substitution. It is better :)
– Sree
Feb 13 '15 at 7:02
add a comment |
I capture error like this
. ${file} 2>&1 | {
read -d "" -t 0.01 error
[ -z "$error" ] || log_warn Load completion ${file} failed: "n${error}"
}
if source failed, I will capture the error and log it.log_warn is just a simple function.
BTW, I use this in my dotfiles
add a comment |
I capture error like this
. ${file} 2>&1 | {
read -d "" -t 0.01 error
[ -z "$error" ] || log_warn Load completion ${file} failed: "n${error}"
}
if source failed, I will capture the error and log it.log_warn is just a simple function.
BTW, I use this in my dotfiles
add a comment |
I capture error like this
. ${file} 2>&1 | {
read -d "" -t 0.01 error
[ -z "$error" ] || log_warn Load completion ${file} failed: "n${error}"
}
if source failed, I will capture the error and log it.log_warn is just a simple function.
BTW, I use this in my dotfiles
I capture error like this
. ${file} 2>&1 | {
read -d "" -t 0.01 error
[ -z "$error" ] || log_warn Load completion ${file} failed: "n${error}"
}
if source failed, I will capture the error and log it.log_warn is just a simple function.
BTW, I use this in my dotfiles
answered Feb 13 '15 at 6:22
wenerwener
27127
27127
add a comment |
add a comment |
To append to a file use /sbin/modprobe -n -v hfsplus 2>> filename
add a comment |
To append to a file use /sbin/modprobe -n -v hfsplus 2>> filename
add a comment |
To append to a file use /sbin/modprobe -n -v hfsplus 2>> filename
To append to a file use /sbin/modprobe -n -v hfsplus 2>> filename
edited May 29 '14 at 7:52
answered May 29 '14 at 7:44
harish.venkatharish.venkat
4,5731925
4,5731925
add a comment |
add a comment |
Newer bash versions (I.e. bash 4.1+):
$ msg=$(ls -la nofile 2>&1)
$ echo $msg
ls: cannot access nofile: No such file or directory
$
add a comment |
Newer bash versions (I.e. bash 4.1+):
$ msg=$(ls -la nofile 2>&1)
$ echo $msg
ls: cannot access nofile: No such file or directory
$
add a comment |
Newer bash versions (I.e. bash 4.1+):
$ msg=$(ls -la nofile 2>&1)
$ echo $msg
ls: cannot access nofile: No such file or directory
$
Newer bash versions (I.e. bash 4.1+):
$ msg=$(ls -la nofile 2>&1)
$ echo $msg
ls: cannot access nofile: No such file or directory
$
answered Nov 4 '17 at 10:58
BurningKromeBurningKrome
1235
1235
add a comment |
add a comment |
To return the error message in a variable, simply;
error=$(/sbin/modprobe -n -v hfsplus 2>&1 1>/dev/null)
echo $error
add a comment |
To return the error message in a variable, simply;
error=$(/sbin/modprobe -n -v hfsplus 2>&1 1>/dev/null)
echo $error
add a comment |
To return the error message in a variable, simply;
error=$(/sbin/modprobe -n -v hfsplus 2>&1 1>/dev/null)
echo $error
To return the error message in a variable, simply;
error=$(/sbin/modprobe -n -v hfsplus 2>&1 1>/dev/null)
echo $error
edited Feb 8 at 11:49
PRY
2,53531026
2,53531026
answered Feb 8 at 10:56
JonathanJonathan
11
11
add a comment |
add a comment |
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');
});
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%2f132511%2fhow-to-capture-error-message-from-executed-command%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');
});
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');
});
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');
});
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
I tried running this command: var=$(/sbin/modprobe -n -v hfsplush) And then displaying it: $var But it still doesn't capture the error message inside the string.
– Miguel Roque
May 29 '14 at 7:42