LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash script: how to assign an output (not the result) to a variable? (https://www.linuxquestions.org/questions/programming-9/bash-script-how-to-assign-an-output-not-the-result-to-a-variable-488563/)

Singing Banzo 10-01-2006 05:42 PM

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.

bulliver 10-01-2006 05:48 PM

Hi.

Try:
Code:

$((ls -l *.lis|grep -c ""))
or...
`ls -l *.lis|grep -c ""`

Double-up the brackets or use backticks to get the output of a command.

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 "")

Singing Banzo 10-01-2006 06:00 PM

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.

Singing Banzo 10-01-2006 06:03 PM

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.

bulliver 10-01-2006 06:07 PM

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
cuenta1=$(ls -l *.lis|grep -c "")
cuenta2=$(cat *.lis|grep -c "Total Pages Marked Corrupt : 0")
echo "Resultado: ${cuenta1} ${cuenta2}"

Does that work how you like it?

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.

Singing Banzo 10-01-2006 06:14 PM

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.

bulliver 10-01-2006 06:18 PM

Quote:

I tried without the space, and it has no error, but the variables has nothing (not even 0). What can be wrong?
Run the command(s) directly in the shell to ensure it is giving you the output you expect...

bulliver 10-01-2006 06:21 PM

Heh, we have to stop posting before reading responses like this ;)

Try this:
Code:

echo "Resultado: $((${cuenta1}-${cuenta2}))"
That's what the double brackets are for: math!

Edit: If you prefer the printf try this:
Code:

printf "Resultado: %d\n" $((${cuenta1}-${cuenta2}))

Singing Banzo 10-01-2006 06:29 PM

Perfect! Thank you very much!

Regards,
SB.


All times are GMT -5. The time now is 04:39 PM.