bash script: how to assign an output (not the result) to a variable?
The simplest way to ask my question is with the practical case: I generate an undefined number of files with logs (of Oracle's dbv), and I want to scan the results to see how many have certain result distinct from 0. So, I tried this:
#!/bin/sh set $cuenta1= $(ls -l *.lis|grep -c "") set $cuenta2= $(cat *.lis|grep -c "Total Pages Marked Corrupt : 0") printf "Resultado: $cuenta1 $cuenta2 %d" $cuenta1-$cuenta2 But $() assign the result of the command, not the output. Is there a way to get what I need? (Besides, if you have tips for optimizations, they are welcome, I'm newbie to shell programmig.) Regards, SB. |
Hi.
Try: Code:
$((ls -l *.lis|grep -c "")) Edit: sorry, double parenthesis is wrong, you do want single, however, I think your problem may be the space between the '=' and the '$'. Try this: Code:
cuenta1=$(ls -l *.lis|grep -c "") |
Not working:
set $cuenta1= $((ls -l *.lis|grep -c "")) ./cuenta.sh: line 2: ls -l *.lis|grep -c "": syntax error: operand expected (error token is ".lis|grep -c """) Back quotes do the same as $(). Thanks anyway. SB. |
Sorry, I replied before your editing.
I tried without the space, and it has no error, but the variables has nothing (not even 0). What can be wrong? Regards, SB. |
Hi again.
I am also unsure what you are trying to do with the printf. You have one conversion specifier, but two arguments, which are in the string anyway. I would write your script as: Code:
#!/bin/sh Notice I ditched the 'set'. They are unneeded, but also notice I dropped the '$' from the assignment. You only need the '$' when referencing the variable. |
That works!!
The specifier is for the difference ($cuenta1-$cuenta2), but it's incorrect: ./cuenta.sh: line 4: printf: 62-62: invalid number How should that be written? Regards, SB. |
Quote:
|
Heh, we have to stop posting before reading responses like this ;)
Try this: Code:
echo "Resultado: $((${cuenta1}-${cuenta2}))" Edit: If you prefer the printf try this: Code:
printf "Resultado: %d\n" $((${cuenta1}-${cuenta2})) |
Perfect! Thank you very much!
Regards, SB. |
All times are GMT -5. The time now is 04:39 PM. |