4

I am trying to just echo a command within my bash script code.

OVERRUN_ERRORS="$ifconfig | egrep -i "RX errors" | awk '{print $7}'"
echo ${OVERRUN_ERRORS}

however it gives me an error and the $7 does not show up in the command. I have to store it in a variable, because I will process the output (OVERRUN_ERRORS) at a later point in time. What's the right syntax for doing this? Thanks.

4
  • 1
    Why it is $ifconfig? Commented Sep 27, 2016 at 15:44
  • 1
    The assignment to OVERRUN_ERRORS does not execute any command as written (OVERRUN_ERRORS="$ifconfig | egrep -i "RX errors" | awk '{print $7}'"). What are you really doing? What does bash -x the-script.sh show? Commented Sep 27, 2016 at 15:45
  • 2
    BTW -- is there a reason you're extracting this from ifconfig, rather than lifting it directly from sysfs (which is where ifconfig itself gets it from)? Pulling it straight from the source is going to be more reliable in terms of not needing to worry about future changes in ifconfig's output format messing up your script. Commented Sep 27, 2016 at 15:48
  • 3
    There's also no reason to use both grep and awk. awk '/RX errors/ { print $7 }' does the work of both in one command. Commented Sep 27, 2016 at 15:52

3 Answers 3

7

On Bash Syntax

foo="bar | baz"

...is assigning the string "bar | baz" to the variable named foo; it doesn't run bar | baz as a pipeline. To do that, you want to use command substitution, in either its modern $() syntax or antiquated backtick-based form:

foo="$(bar | baz)"

On Storing Code For Later Execution

Since your intent isn't clear in the question --

The correct way to store code is with a function, whereas the correct way to store output is in a string:

# store code in a function; this also works with pipelines
get_rx_errors() { cat /sys/class/net/"$1"/statistics/rx_errors; }

# store result of calling that function in a string
eth0_errors="$(get_rx_errors eth0)"

sleep 1 # wait a second for demonstration purposes, then...

# compare: echoing the stored value, vs calculating a new value
echo "One second ago, the number of rx errors was ${eth0_errors}"
etho "Right now, it is $(get_rx_errors eth0)"

See BashFAQ #50 for an extended discussion of the pitfalls of storing code in a string, and alternatives to same. Also relevant is BashFAQ #48, which describes in detail the security risks associated with a eval, which is often suggested as a workaround.


On Collecting Interface Error Counts

Don't use ifconfig, or grep, or awk for this at all -- just ask your kernel for the number you want:

#!/bin/bash
for device in /sys/class/net/*; do
  [[ -e $device/statistics/rx_errors ]] || continue
  rx_errors=$(<"${device}/statistics/rx_errors")
  echo "Number of rx_errors for ${device##*/} is $rx_errors"
done
Sign up to request clarification or add additional context in comments.

Comments

4

Use $(...) to capture the output of a command, not double quotes.

overrun_errors=$(ifconfig | egrep -i "RX errors" | awk '{print $7}')

Comments

0

Your double quotes around RX errors are a problem. Try;

OVERRUN_ERRORS="$ifconfig | egrep -i 'RX errors' | awk '{print $7}'"

To see the commands as they are executing, you can use

set -v

or

set -x

For example;

set -x 
OVERRUN_ERRORS="$ifconfig | egrep -i 'RX errors' | awk '{print $7}'"
set +x

4 Comments

The OP says clearly in the question that they want output of the command -- that is, that they want the command to actually be run. Making the change you suggest here does not cause that to happen.
If they were going to eval "$OVERRUN_ERRORS" rather than echo "$OVERRUN_ERRORS", we might be having a different discussion (but it'd also be one about how storing code in strings is a bad practice in general, with lots of references to BashFAQ #50 and BashFAQ #48).
@CharlesDuffy, the OP wrote 'I am trying to just echo a command', and he is trying to use echo. He says 'the $7 does not show up in the command', not 'does not show up in the output'. I don't think my interpretation of that text is unreasonable at all. If he said 'I am trying to run a command', or some such, I would have talked about eval.
Look at the title. "echo output of a piped command" -- those exact words, in that exact order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.