12

When using

df -h | grep /dev/root | awk '{print $5}'

I get the usage of my SD Card in my Pi: 78%

But when I use

/usr/bin/ssh -i /path/to/key user@server "df -h | grep /dev/root | awk '{print $5}'"

from another computer, I get:

/dev/root       7.2G  5.3G  1.6G  78% /

Full df -h:

Filesystem      Size  Used Avail Use% Mounted on
/dev/root       7.2G  5.3G  1.6G  78% /
devtmpfs        364M     0  364M   0% /dev
tmpfs           368M   68K  368M   1% /dev/shm
tmpfs           368M  5.2M  363M   2% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           368M     0  368M   0% /sys/fs/cgroup
/dev/mmcblk0p1   60M   21M   40M  35% /boot
tmpfs            74M  4.0K   74M   1% /run/user/1000
8
  • 4
    Try scaping the $ with a backslash: "df -h | grep /dev/root | awk '{print \$5}'"
    – clk
    Jul 8, 2016 at 14:54
  • 1
    escape the dollar, it's being expanded by the shell before ssh.
    – 123
    Jul 8, 2016 at 14:55
  • 1
    df /dev/root --output=pcent | tail -1
    – Costas
    Jul 8, 2016 at 15:16
  • @Costas df: ‘/dev/root’: No such file or directory. The Pi handles the storage different than other Linux.
    – Motte001
    Jul 8, 2016 at 15:18
  • 3
    Escape the dollar; it will collapse within a few decades Jul 8, 2016 at 17:19

3 Answers 3

21

You're hitting a quoting problem; the $5 is being interpreted at the wrong time. There are at least two solutions:

  1. Put a \ before the $; e.g.

    /usr/bin/ssh -i /path/to/key user@server "df -h | grep /dev/root | awk '{print \$5}'"
    
  2. Run the df remotely but the grep and awk locally. e.g.

    /usr/bin/ssh -i /path/to/key user@server df -h | grep /dev/root | awk '{print $5}'
    

FWIW, I'd run a version of the second option but merging grep and awk

/usr/bin/ssh -i /path/to/key user@server df -h | awk '/\/dev\/root/ {print $5}'
1
  • 4
    +1 for "merging grep & awk" ... if you use awk anyway there's really no need for grep. :)
    – tink
    Jul 8, 2016 at 18:13
6

This should work:

/usr/bin/ssh -i /path/to/key user@server "df -h | grep /dev/root | awk '{print \$5}'"

Notice the \ included before the $. Without this, the local shell will expand the empty variable $5 and send that to the remote server. Essentially, printing the entire line.

4

For completeness, another way is to use the fact $n in awk isn't a special case of $variable syntax like shell, but instead the $ operator applied to an integer expression:

(ssh key&remote) "df -h | grep /dev/root | awk '{print $ 5}'"

or combined as

(ssh key&remote) "df -h | awk '/\\/dev\\/root/ {print $ 5}'"
# can use [/] instead of ugly \\/ in gawk, but maybe not others

or maybe better as a variable

(ssh key&remote) "df -h | awk -vm=/dev/root '$ 0 ~ m {print $ 5}'"

But personally I'd go with Stephen Harris' preference to run the awk locally.

1
  • 1
    Huh, 25 years of using awk and I'd not spotted that subtlety! Nice. Jul 16, 2016 at 21:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .