-1

I have my filesystem as like below

Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/rootvg-rootvol
                       20G  3.7G   15G  20% /
tmpfs                  71G  8.0K   71G   1% /dev/shm

And my code is as below:

varone = df -h | awk ' {print $1 }'
vartwo = df -h | awk ' NR == 2 {print $2","$3","$4","$5","$6 }'
echo "$varone $vartwo" >> /home/jeevagan/test_scripts/sizes/excel.csv

I want to export the 'df -h' into a csv file. Why I printed $1 alone in one variable means, there is space in the 'df -h' output. I want it to be printed in a single line.

When I run the script, it throws an error like

varone: command not found

vartwo: command not found

zeewagon
  • 1,835
  • 4
  • 18
  • 22
  • Possible duplicate of [How to set a variable equal to the output from a command in Bash?](http://stackoverflow.com/questions/4651437/how-to-set-a-variable-equal-to-the-output-from-a-command-in-bash) – hek2mgl Aug 03 '16 at 10:17

2 Answers2

0

You can't put spaces around the equal sign, and you need backquotes to put the result of your command in a variable.

Try this:

varone=`df -h | awk ' {print $1 }'`
vartwo=`df -h | awk ' NR == 2 {print $2","$3","$4","$5","$6 }'`
echo "$varone $vartwo" >> /home/jeevagan/test_scripts/sizes/excel.csv
yoones
  • 2,394
  • 1
  • 16
  • 20
  • Consider to use `$()` instead of backtics. – hek2mgl Aug 03 '16 at 10:19
  • That works @yoones. But it is not giving the output as expected. My expected output is to print /dev/mapper/rootvg-rootvol 20G 3.7G 15G 20% / in separate cells in a csv file – zeewagon Aug 03 '16 at 11:17
  • Then consider doing something like: `varone=\`df -h | awk ' {print $1 }' | tr '\n' ';'\`` – yoones Aug 04 '16 at 09:05
0

If you just wanted to capture df -h

varOne=`df -h -P| awk '{print $1","$2","$3","$4","$5","$6 }'`
echo  "$varOne" >> /home/jeevagan/test_scripts/sizes/excel.csv

Read about 'P' here

How can I *only* get the number of bytes available on a disk in bash?

Community
  • 1
  • 1