首页 > 解决方案 > BASH Free Space Script: Variation

问题描述

I made a script in order to check the available space on root, /var, /tmp, /usr and /opt since I need 1.5G free on each of them. Of course, there are times when all those are in root, not in another partitions.

But I got some error from my script:

1- Sometimes the output its weird when it runs in a non-English System.

2- --output in df -h its not available since some systems are old.

I want to change this script a little bit since I want it to work in every machine and I would like to ask for ideas.

I have tested the script on 650 machines already. 140 of them created the problems that I mention.

printf "root=" ; df -h --output=avail / | grep -v Avail | xargs | tr -d '[:space:]'
printf ",opt=" ; df -h --output=avail /opt | grep -v Avail | xargs | tr -d '[:space:]'
printf ",usr=" ; df -h --output=avail /usr | grep -v Avail | xargs | tr -d '[:space:]'
printf ",tmp=" ; df -h --output=avail /tmp | grep -v Avail | xargs | tr -d '[:space:]'
printf ",var=" ; df -h --output=avail /var | grep -v Avail | xargs

1- Problem:

2- Problem:

df: Unbekannte Option »--output=avail«
„df --help“ gibt weitere Informationen.
root= opt= usr= tmp= var=

EDIT:

Looks like @CharlesDufy answer worked. I made the script like this:

#!/bin/bash

sudo df -h / /var /tmp /opt /usr > freespace.txt

rootSpace=$(awk "NR==2 { print $4 }" freespace.txt)
varSpace=$(awk "NR==3 { print $4 }" freespace.txt)
tmpSpace=$(awk "NR==4 { print $4 }" freespace.txt)
optSpace=$(awk "NR==5 { print $4 }" freespace.txt)
usrSpace=$(awk "NR==6 { print $4 }" freespace.txt)

customProp="root=$rootSpace,var=$varSpace,tmp=$tmpSpace,opt=$optSpace,usr=$usrSpace"

#and the rest....

Also I want to ask 2 more questions:

  1. Is it possible to take the freespace.txt output without creating a file?
  2. If the 4 folders are in root, like they are not separte partitions, my output looks like this

    root=12G,var=12G,tmp=12G,opt=12G,usr=12G

Is there a way to make an output like this without tons of IF, ELIF or ELSE comands?

root=12G,var=root,tmp=root,opt=root,usr=root

标签: bash

解决方案


推荐阅读