-1

I have something like this:

echo "string1" > /sys/../../../..

In case this throws an error I want to capture that. Currently I am unable to do that. Can you suggest how can I do this?

I tried:

(echo "string1" > /sys/../../../..) > abc 2>&1 # does not work

All the directly paths exist here.

Ankur Agarwal
  • 23,692
  • 41
  • 137
  • 208
  • `if echo "abc" > /nonexistant/none ; then echo OK; else echo nope ; fi` (same idea, a path that doesn't exist). Good luck. – shellter Feb 26 '14 at 03:11
  • Please explain what you mean by "does not work". As long as it is possible to create and/or write to the file `abc` in the current working directory, that line should work fine, although `(echo something > some_directory) 2>abc` would be a somewhat simpler solution. – rici Feb 26 '14 at 04:13
  • The problem is that as I execute this command a bunch of data appears in the console but I am not able to capture it. – Ankur Agarwal Feb 26 '14 at 04:14
  • Then you are probably doing something slightly different from what you say you are doing. – rici Feb 26 '14 at 04:44
  • @rici I have seen this quite happen actually. I echo to a sys entry and bunch of data appears on the console. I am not able to capture it by any means. Could it be that the echo triggers something ( a process) which in turn spews the logs I see on the console. That's my suspicion. – Ankur Agarwal Feb 26 '14 at 05:47
  • @abc '...not able to capture it by any means'. You have not tried my post below? I can capture this just fine. – JayInNyc Feb 26 '14 at 12:07
  • 1
    @abc: There are some idiosyncratic interfaces on linux, and probably other operating systems, which you can use to cause the kernel to dump information directly to the console. If, as I suspect, you are using one of these, you should be much more specific in your question, rather than wasting everyone's time by falsifying information about what you are actually doing. (There is a big difference between `echo h > /proc/sysrq-trigger` and "something like `echo "string1" > /sys/../../../..`) – rici Feb 26 '14 at 13:25
  • @rici I was hoping someone would mention that the idiosyncratic interfaces could lead to the behavior I was observing. Thanks for mentioning that. Your comment actually answered my question unless someone else has a different answer. – Ankur Agarwal Feb 26 '14 at 23:38

4 Answers4

1

My interpretation of your question is that you'd like to capture any error in a bash variable. You can use

$ error=$( echo 'hw' 2>&1 > /dev/null)
$ echo $error
<blank>
$ error=$( echo 'string' > /sys/not-a-path 2>&1 > /dev/null )
$ echo $error
bash: /sys/not-a-path/: Is a directory

I've based my answer on the helpful SO post here: How to store standard error in a variable in a Bash script.

The point is to direct stderr to stdout, and stdout to /dev/null.

Of course you can check your error code that returns by inspecting $? after the call. For instance,

$ ./bitcoins_bad.sh 
$ echo $?
-1
$ ./bitcoins_good.sh
$ echo $?
0
Community
  • 1
  • 1
JayInNyc
  • 2,193
  • 3
  • 20
  • 21
1

If you want to catch the full errormessage in a (log)file:

echo "string1" 2>logerror >/sys/../../../.. 

to see it:

cat logerror    

If you want the full errormessage in a variable

ERROR=$( echo "string1" 2>&1 >/sys/../../../.. )

to see it:

echo $ERROR

If you only want to know if it succeeded or failed:

echo "string1" 2>/dev/null >/sys/../../../..
RESULT="$?"

to see it:

[ "$RESULT" = "0" ] && echo OK || echo failed

and the most elegant of all:

echo "string1" 2>/dev/null >/sys/../../../.. && echo OK || echo FAIL
thom
  • 2,294
  • 12
  • 9
0

It is not a error for echo. It is a error for shell (failed to open the file for write). You can do something like this

sh -c 'echo > /sys/power/wakeup_count' 2> abc
Tim Green
  • 3,571
  • 2
  • 23
  • 23
-1

look ata program called tee:

from How to redirect output to a file and stdout

ls -lR / 2>&1 | tee output.file
Community
  • 1
  • 1
ioseph
  • 1,887
  • 20
  • 25