shell script [RESOLVIDO]

1. shell script [RESOLVIDO]

Andi
upc0d3

(usa Gentoo)

Enviado em 10/06/2008 - 11:09h

Gurizada, alguem sabe me dizer como faco para fazer comparacao dos espacoes em disco do hd?
eu criei um script mas nao deu muito certo segue um exemplo do que u fiz:

#!/bin/bash

SDA1 = '/bin/df -h | grep sda1 | cut -d " " -f 18'
SDA3 = '/bin/df -h | grep sda3 | cut -d " " -f 17'

if [ $SDA1 > $SDA3 ]
then
echo "SDA1 MAIOR"
else
echo "SDA3 MAIOR"
fi

#EOF

mas nao funciona esse script,
os comandos:

/bin/df -h | grep sda1 | cut -d " " -f 18
/bin/df -h | grep sda3 | cut -d " " -f 17

retornam exatamente o que eu queria que retornasse, o espaco usado do disco.

mas da erro na linha do if, e do then.

mas eu naum sei pq, alguem sabe? alguem pode me ajudar?

Obrigado!


  


2. MELHOR RESPOSTA

Elgio Schlemer
elgio

(usa OpenSuSE)

Enviado em 10/06/2008 - 14:17h

1) usando o df -h tu tem valores com G, K ou M no final!
/dev/hda2 6,8G 5,9G 593M 92% /

Como vai comparar (maior) um 6,8G com 593M ???

Use -k que retorna tudo em k e sem letrinhas

2) Porque em um deles tu pega o campo 17 e no outro o campo 18?
/bin/df -h | grep sda1 | cut -d " " -f 18
/bin/df -h | grep sda3 | cut -d " " -f 17

Da maneira que TU FIZESTE o certo é o 18, mas ISTO DEPENDE!!!! Cortar por espaços é PESSIMO, pois a saida é formatada, podes ter mais ou menos espaços!

#!/bin/bash

# Campo 2 para TAMANHO TOTAL
# campo 3 para espaco USADO
# campo 4 para espaco DISPONIVEL
SDA1="`/bin/df -k |grep sda1 | sed 's/ \+/:/g'|cut -d: -f2`" # pegando CAMPO 2
SDA3="`/bin/df -k |grep sda5 | sed 's/ \+/:/g'|cut -d: -f2`"

if [ "X$SDA1" = "X" ]
then
echo "ERRO: nao existe sda1"
exit 1
fi

if [ "X$SDA3" = "X" ]
then
echo "ERRO: nao existe sda3"
exit 1
fi

echo "DEBUG: SDA1=$SDA1 SDA3=$SDA3"

if [ $SDA1 -gt $SDA3 ]
then
echo "SDA1 MAIOR"
else
echo "SDA3 MAIOR"
fi


3. Re: shell script [RESOLVIDO]

Sandro Marcell
SMarcell

(usa Slackware)

Enviado em 10/06/2008 - 12:01h

Para comparação numérica você deve utilizar:

if [ $SDA1 -gt $SDA3 ]
...

--- Editado ---

Incorreto:

SDA1 = '/bin/df -h | grep sda1 | cut -d " " -f 18'
SDA3 = '/bin/df -h | grep sda3 | cut -d " " -f 17'

Correto:

SDA1 = `/bin/df -h | grep sda1 | cut -d " " -f 18`
SDA3 = `/bin/df -h | grep sda3 | cut -d " " -f 17`

Note as crases!


4. Re: shell script [RESOLVIDO]

Andi
upc0d3

(usa Gentoo)

Enviado em 10/06/2008 - 13:36h

cara, isso nao deu certo.
mas obrigado!


5. Re: shell script [RESOLVIDO]

Denis Doria
thuck

(usa Debian)

Enviado em 10/06/2008 - 14:07h

Bem, acho q isso resolve, embora eu acredite que oq vc quer seja verificar entre dois, o script abaixo verifica o maior entre todas as partições.


#!/bin/bash

VAR=0
DEVICE=null

sed -e '1d' -e '/^$/d' -e '/sd../,/hd../!d' /proc/partitions |cut -c14- > /tmp/bugaasd
while read BLOCKS DEV
do

if [ ${BLOCKS} -gt ${VAR} ]; then

VAR=${BLOCKS}
DEVICE=${DEV}

fi


done < /tmp/bugaasd


echo $VAR
echo $DEVICE



6. Re: shell script [RESOLVIDO]

Andi
upc0d3

(usa Gentoo)

Enviado em 10/06/2008 - 14:51h

Elgio:
obrigado por ter me avisado quanto ao retorno do comando df -h, eu realamente nao tinha notado, obrigado.









Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts