Ubuntu: IFS to read systeminfos



Question:

I tried to read some system infos and put them into variables:

df -k | grep /dev/mmcblk0p1 | IFS=" " read -r device blocks used available use_percent mounted_on  

It did not work, all variables are empty.


Solution:1

The issue is not the IFS, it's because in bash (and certain other shells) the RHS of a pipeline is executed in a subshell.

A simple alternative is to use process substitution to keep the read in the parent shell:

read -r device blocks used available use_percent mounted_on < <(df -k | grep /dev/mmcblk0p1)  

See Bash: Variable assignment doesn't seem to 'stick'


Solution:2

If you assign variables using read, these must be part of the same block statement { ... }:

 df -k | grep /dev/mmcblk0p1 | {      read -r device blocks used available use_percent mounted_on     printf "device=%s\nblocks=%s\nused=%s\navailable=%s\nuse_percent=%s\nmounted_on=%s\n" $device $blocks $used $available $use_percent $mounted_on  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »