Error expr: argumento no numérico al ejecutar el script bash

There is txt file-:


Shell script-:

old_count=`grep 'abhinav' |  awk  'BEGIN { FS = "," } ; { print $2 }' | awk  'BEGIN { FS = "_" } ; { print $2 }'`
new_count=`grep 'dixit' | awk  'BEGIN { FS = "," } ; { print $2 }'`
sum=`expr $old_count + $new_count`

But when this script is executed than error expr: non-numeric argument is coming . Though both variable $old_count $new_count are numeric.

preguntado el 15 de noviembre de 13 a las 09:11

awk 'BEGIN { FS = "_" } ; { print $3 }' wouldn't produce any output. You wanted to print the second field, i.e., say print $2 en lugar de. -

yes dev i want second value of T_10 i.e 10 to get into variable old_count. Apologies that was typo error but than also same error -

También tienes un | desaparecido antes awk in the same line. -

After inserting | also result is same -

Try some bit of debugging. Say echo "#${old_count}#${new_count}#" to see what it produces. -

1 Respuestas

Sidestepping whatever the problem actually is, it's simpler to write this as

old_count=$( awk -F, '/abhinav/ {split($2, a, "_"); print a[2]}' file1 )
new_count=$( awk -F, '/Dixit/ {print $2}' file2 )
sum=$(( old_count + new_count ))

One issue might have been that you were grepping for dixitno, Dixit, resulting in an empty value for new_count. I get a different error, but that could be based on the implementation of expr. Tenga en cuenta que expr is no longer necessary for arithmetic in the shell; $((...)) should be available in any POSIX-compliant shell.

respondido 15 nov., 13:14

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.