LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-16-2012, 09:30 AM   #1
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
How to store a number in variable ?


How can I store a number in a variable in a shell script?
Support I have a script:
#!/bin/bash
TOTAL="/tmp/totalcount"
ls -la | wc -l > $TOTAL
echo "Total count is: $TOTAL"


This script is not giving me the desired output. Though I can use:
echo "Total count is: `cat $TOTAL`"
But is this a good way? Can't I directly store the numerical value of command ls -la | wc -l into variable $TOTAL, so I need not to do a "cat" this variable while echo it. I just wanna use:
echo "Total count is: $TOTAL", and I want that it give me output as:
Total count is: 35 (35 is just any number)
So question is how to store a numerical value in a variable?
 
Old 10-16-2012, 09:34 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966
Why the intermediate file at all??

TOTAL=$(ls -la | wc -l)

Note that whilst still used, `backticks` are officially very obsolete. try using $(brackets) instead for much more flexible and readable code.
 
Old 10-16-2012, 09:53 AM   #3
valdinei
LQ Newbie
 
Registered: Jul 2012
Location: Brazil
Distribution: Ubuntu, Freebsd, CentOS
Posts: 17

Rep: Reputation: Disabled
is correct

TOTAL=$(ls -la | wc -l)
echo $TOTAL


but

TOTAL=`ls -a | wc -l`
echo $TOTAL

work is not wrong for scripts snippet
 
Old 10-16-2012, 09:57 AM   #4
valdinei
LQ Newbie
 
Registered: Jul 2012
Location: Brazil
Distribution: Ubuntu, Freebsd, CentOS
Posts: 17

Rep: Reputation: Disabled
Cool

Quote:
Originally Posted by valdinei View Post
is correct

TOTAL=$(ls -la | wc -l)
echo $TOTAL


but

TOTAL=`ls -a | wc -l`
echo $TOTAL

work is not wrong for scripts snippet
Remember these two snippet scripts will display information about current directory in directory tree
 
Old 10-16-2012, 10:11 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
Quote:
Originally Posted by acid_kewpie View Post
Why the intermediate file at all??

TOTAL=$(ls -la | wc -l)

Note that whilst still used, `backticks` are officially very obsolete. try using $(brackets) instead for much more flexible and readable code.
Thanks for the answer. It's working fine on Linux, but not on Solaris. Actually I will run this script on a Solaris machine. It's giving me syntax error on Solaris. What to do?
 
Old 10-17-2012, 05:22 AM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
You'll need to show us the exact error msgs.
You may be using a different shell; on Linux the default is bash.
Try putting
Code:
#!/bin/bash

#This next line shows what the parser is doing
set -xv
at the top of your script.

Check the location of bash or ksh using
Code:
which bash

which ksh
 
Old 10-17-2012, 07:31 AM   #7
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
Quote:
Originally Posted by chrism01 View Post
You'll need to show us the exact error msgs.
You may be using a different shell; on Linux the default is bash.
Try putting
Code:
#!/bin/bash

#This next line shows what the parser is doing
set -xv
at the top of your script.

Check the location of bash or ksh using
Code:
which bash

which ksh
Whenever, I use this expression in Solaris, either as root (shell is /sbin/sh) or as my user i.e. jack (shell is /bin/bash), in both cases it throws error-
<scriptname>: syntax error at line 4:`TOTAL=$' unexpected
So how can I get rid of this?

Last edited by shivaa; 10-17-2012 at 07:32 AM.
 
  


Reply

Tags
shell scripting, variable


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] Store a value to linked name of variable or array emanation Programming 2 02-08-2012 10:41 AM
how to store result of wc -l command in variable? trynq Linux - Newbie 11 04-20-2011 07:49 AM
how to store a command in a variable lemon09 Programming 11 02-01-2010 02:03 AM
How to store SQL output in a variable atulsh General 1 09-18-2008 01:22 PM
Store PATH variable permanently Skute Linux - Software 8 01-15-2004 04:05 PM


All times are GMT -5. The time now is 05:23 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration