Domanda

/! Bin / sh

   if [ "`echo $desc $status | awk -F"," '{print $3}' | awk -F" " '{print $1}' | sed '/^$/d'`" != "OK" ]; then
        echo "howdy dody"
   fi

echo $desc $status | awk -F"," '{print $3}' | awk -F" " '{print $1}' | sed '/^$/d'

Per prima cosa se condizionata non funziona, im indovinando che è a causa della citazione improprio, ma non riesco a capirlo.

Grazie in anticipo per qualsiasi aiuto.

È stato utile?

Soluzione

Se stai usando Bash, mi consiglia $(...) invece di back-citazioni. Quali messaggi di errore si ottiene? La mia ipotesi è che l'opzione -F"," a awk non viene citato correttamente. Cercando di inserire \ per sfuggire le virgolette.

Altri suggerimenti

È inoltre possibile utilizzare le virgolette singole intorno al argomento dell'opzione -F come si deve in giro altri argomenti:

if [ "`echo $desc $status | awk -F',' '{print $3}' | awk -F' ' '{print $1}' | sed '/^$/d'`" != "OK" ]; then

E 'molto più facile scrivere il test se si avvolge in una funzione:

mytest()
{
  echo "$1 $2" \
  | awk -F"," -v arg3="$3" '{print arg3}' \
  | awk -F" " -v arg1="$1" '{print arg1}' \
  | sed '/^$/d'
}

In questo modo, è possibile verificare che funzioni correttamente. Una volta che avete guadagnato questa fiducia

if [ "$(mytest "$desc" "$status")" != "OK" ]; then
        echo "howdy doody"
fi

o

if mytest "$desc" "$status" | grep -q -v '^OK$'; then
  echo "howdy doody"
fi

A prima vista, si potrebbe desiderare di provare sfuggire alcune delle virgolette:

   if [ "`echo $desc $status | awk -F"," '{print $3}' | awk -F" " '{print $1}' | sed '/^$/d'`" != "OK" ]; then
        echo "howdy dody"
   fi

a

   if [ "`echo $desc $status | awk -F\",\" '{print $3}' | awk -F\" \" '{print $1}' | sed '/^$/d'`" != "OK" ]; then
        echo "howdy doody"
   fi

Sfuggire le virgolette è certamente una buona idea, ma sembra che il $ 3 e $ 1 sono destinati ad essere interpretato da awk. Essi sono stati interpretati dalla shell, invece. Probabilmente si vuole sfuggire al '$' s. (E 'possibile che si hanno valori significativi per $ 1 e $ 3 nel guscio, ma non probabile.)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top