Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
![Reply](https://www.linuxquestions.org/questions/images/buttons/reply.gif) |
10-01-2006, 06:42 PM
|
#1
|
LQ Newbie
Registered: Jul 2004
Posts: 20
Rep:
|
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.
|
|
|
10-01-2006, 06:48 PM
|
#2
|
Senior Member
Registered: Nov 2002
Location: British Columbia, Canada
Distribution: Gentoo x86_64; FreeBSD; OS X
Posts: 3,764
Rep:
|
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 "")
Last edited by bulliver; 10-01-2006 at 06:56 PM.
|
|
|
10-01-2006, 07:00 PM
|
#3
|
LQ Newbie
Registered: Jul 2004
Posts: 20
Original Poster
Rep:
|
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.
|
|
|
10-01-2006, 07:03 PM
|
#4
|
LQ Newbie
Registered: Jul 2004
Posts: 20
Original Poster
Rep:
|
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.
|
|
|
10-01-2006, 07:07 PM
|
#5
|
Senior Member
Registered: Nov 2002
Location: British Columbia, Canada
Distribution: Gentoo x86_64; FreeBSD; OS X
Posts: 3,764
Rep:
|
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.
|
|
|
10-01-2006, 07:14 PM
|
#6
|
LQ Newbie
Registered: Jul 2004
Posts: 20
Original Poster
Rep:
|
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.
|
|
|
10-01-2006, 07:18 PM
|
#7
|
Senior Member
Registered: Nov 2002
Location: British Columbia, Canada
Distribution: Gentoo x86_64; FreeBSD; OS X
Posts: 3,764
Rep:
|
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...
|
|
|
10-01-2006, 07:21 PM
|
#8
|
Senior Member
Registered: Nov 2002
Location: British Columbia, Canada
Distribution: Gentoo x86_64; FreeBSD; OS X
Posts: 3,764
Rep:
|
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}))
Last edited by bulliver; 10-01-2006 at 07:26 PM.
|
|
|
10-01-2006, 07:29 PM
|
#9
|
LQ Newbie
Registered: Jul 2004
Posts: 20
Original Poster
Rep:
|
Perfect! Thank you very much!
Regards,
SB.
|
|
|
All times are GMT -5. The time now is 12:38 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|