LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 10-01-2006, 05:42 PM   #1
Singing Banzo
LQ Newbie
 
Registered: Jul 2004
Posts: 20

Rep: Reputation: 0
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.
 
Old 10-01-2006, 05:48 PM   #2
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
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 05:56 PM.
 
Old 10-01-2006, 06:00 PM   #3
Singing Banzo
LQ Newbie
 
Registered: Jul 2004
Posts: 20

Original Poster
Rep: Reputation: 0
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.
 
Old 10-01-2006, 06:03 PM   #4
Singing Banzo
LQ Newbie
 
Registered: Jul 2004
Posts: 20

Original Poster
Rep: Reputation: 0
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.
 
Old 10-01-2006, 06:07 PM   #5
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
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.
 
Old 10-01-2006, 06:14 PM   #6
Singing Banzo
LQ Newbie
 
Registered: Jul 2004
Posts: 20

Original Poster
Rep: Reputation: 0
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.
 
Old 10-01-2006, 06:18 PM   #7
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
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...
 
Old 10-01-2006, 06:21 PM   #8
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
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 06:26 PM.
 
Old 10-01-2006, 06:29 PM   #9
Singing Banzo
LQ Newbie
 
Registered: Jul 2004
Posts: 20

Original Poster
Rep: Reputation: 0
Perfect! Thank you very much!

Regards,
SB.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] issue with variable in bash script angel115 Programming 4 08-21-2006 01:42 PM
bash script Want to capture return key and assign a value procfs Programming 9 07-07-2006 01:38 AM
Shell script --cannot assign variable-- ralvez Programming 6 02-24-2006 04:56 PM
cannot export result from awk into a variable in a bash script Emmanuel_uk Linux - Newbie 4 03-07-2005 01:54 AM
Bash script renaming a variable zael Programming 3 09-30-2003 04:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration