2013-04-28 18 views
7

Sto cercando di verificare se l'è la versione di Ubuntu è supportata o meno, e nel caso in cui se non lo è, allora aggiorno source.list nella cartella APTBash: confrontando una stringa come un intero

so che non posso usare <> entro [[ ]], quindi ho provato [()], provato [], e ho anche provato a usare un'espressione regolare c'è e "-" nella variabile, ma non ha funzionato, perché non è stato possibile trovare "file: 76" .

Come devo scrivere il confronto per funzionare?

Il mio codice:

#!/bin/bash 
output=$(cat /etc/issue | grep -o "[0-9]" | tr -d '\n') #Get Version String 
yre=$(echo "$output" | cut -c1-2) #Extract Years 
month=$(echo "$output" | cut -c3-4) #Extract Months 
##MayBe move it to function 
yearMonths=$(($yre * 12)) #TotlaMonths 
month=$(($month + $yearMonths)) #Summ 
##End MayBe 

curMonths=$(date +"%m") #CurrentMonts 
curYears=$(date +"%y") 

##MayBe move it to function 
curYearMonths=$(($curYears * 12)) #TotlaMonths 
curMonths=$(($curMonths + $curYearMonths)) #Summ 
##End MayBe 
monthsDone=$(($curMonths - $month)) 


if [[ "$(cat /etc/issue)" == *LTS* ]] 
then 
    supportTime=$((12 * 5)) 
else 
    supportTime=9 
fi 

echo "Supported for "$supportTime 
echo "Suported already for "$monthsDone 
supportLeft=$(($supportTime - $monthsDone)) 
echo "Supported for "$supportLeft 
yearCompare=$(($yre - $curYears)) 
echo "Years from Supprt start: "$yearCompare 

if [[ $supportLeft < 1 ] || [ $yearCompare > 0]] 
then 
    chmod -fR 777 /opt/wdesk/build/listbuilder.sh 
    wget -P /opt/wdesk/build/ "https://placeofcode2wget.dev/listbuilder.sh" 
    sh /opt/wdesk/build/listbuilder.sh 
else 
    echo "Still Supported" 
fi 
+0

Per inciso, per evitare il [UUCA] (http://partmaps.org/era/unix/award.html), provate 'uscita = $ (grep -o "[0-9]"/etc/issue) '(sì, anche qui' tr' è completamente superfluo). Probabilmente dovresti 'grep' per più di una sola cifra, immagino? – tripleee

+1

In realtà, ottenere una versione leggibile dalla macchina da ['lsb_release'] (http://linux.die.net/man/1/lsb_release) è molto più semplice e più affidabile del tentativo di analizzare'/etc/issue'. – tripleee

+0

@tripleee possibile, come ho detto che sono bewbie, quindi grazie per l'avvertimento (su UUCA). lsb_release ha fatto alcuni messaggi di avvertimento, quindi l'ho saltato, ma credo che lo riconsidererò. –

risposta

7

Ti piace questa:

[[ $supportLeft -lt 1 || $yearCompare -gt 0 ]] 

Potete trovare questi e altri operatori connessi nella man test

+2

Hai bisogno di uno spazio tra '0' e']] ' –

+0

Ah, sì ... Ho trascurato che quando si copiava Grazie! – janos

+0

:(Lo stesso errore che ho avuto con "if (($ supportLeft <1 || $ yearCompare> 0))" ... –

4

Questo sembra funzionare:

if (($supportLeft < 1)) || (($yearCompare > 0)) 

o

if (($supportLeft < 1 || $yearCompare > 0)) 
+0

:(Provato, ma termina con 3 avvisi (suppongo): impossibile aprire 1, file non trovato, 993 non trovato, 72 non trovato; .. Sembra che sia correlato a $ anniCompare => 72, 1 a 1 e 993 a $ supportLeft. Qualcosa del genere Oggi prima ... Qualche idea a riguardo ?? –

3

Non sono sicuro se questo è di aiuto, ma la questione è stata alta in Google quando ho cercato per "confrontare stringa da int in bash"

È possibile "fusione "una stringa in un int in bash con l'aggiunta di 0

NUM="99" 
NUM=$(($NUM+0)) 

Questa grande opera se avete a che fare con i NULL così

NUM="" 
NUM=$(($NUM+0)) 

Assicurati che non ci siano spazi nella stringa!

NUM=`echo $NUM | sed -e 's/ //g'` 

(Testato su Solaris 10)

Problemi correlati