niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » shell
positional parameters of a var
Post new topic   Reply to topic Page 1 of 1 [6 Posts] View previous topic :: View next topic
Author Message
SumGuy
*nix forums beginner


Joined: 21 Jul 2006
Posts: 2

PostPosted: Fri Jul 21, 2006 12:38 am    Post subject: positional parameters of a var Reply with quote

How can I obtain the positional parameters of a var

if

var=abc I want to show the output "3"
var=1234567 I want to show the ouput "7"


Tks
Back to top
base60
*nix forums Guru


Joined: 07 Sep 2005
Posts: 322

PostPosted: Fri Jul 21, 2006 1:09 am    Post subject: Re: positional parameters of a var Reply with quote

SumGuy wrote:
Quote:
How can I obtain the positional parameters of a var

if

var=abc I want to show the output "3"
var=1234567 I want to show the ouput "7"

What shell are you using?
Back to top
Chris F.A. Johnson
*nix forums Guru


Joined: 20 Feb 2005
Posts: 2268

PostPosted: Fri Jul 21, 2006 2:19 am    Post subject: Re: positional parameters of a var Reply with quote

On 2006-07-21, SumGuy wrote:
Quote:
How can I obtain the positional parameters of a var

if

var=abc I want to show the output "3"
var=1234567 I want to show the ouput "7"

In any POSIX shell (bash, ksh, ash, etc.):

echo "${#var}"

In other shells:

expr "x$var" : '.*' - 1

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Back to top
Barry Margolin
*nix forums Guru


Joined: 24 Feb 2005
Posts: 323

PostPosted: Fri Jul 21, 2006 2:25 am    Post subject: Re: positional parameters of a var Reply with quote

In article <1153442331.100910.195180@m79g2000cwm.googlegroups.com>,
"SumGuy" <vidalch_1@yahoo.com> wrote:

Quote:
How can I obtain the positional parameters of a var

if

var=abc I want to show the output "3"
var=1234567 I want to show the ouput "7"

Do you mean you want to know how many characters are in the variable's
value?

echo -n "$var" | wc -c

is probably the most portable solution. But in bash and similar shells,
you can use

echo ${#var}

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Back to top
Geoff Clare
*nix forums addict


Joined: 25 Feb 2005
Posts: 64

PostPosted: Fri Jul 21, 2006 12:58 pm    Post subject: Re: positional parameters of a var Reply with quote

Barry Margolin <barmar@alum.mit.edu> wrote, on Thu, 20 Jul 2006:

Quote:
var=abc I want to show the output "3"
var=1234567 I want to show the ouput "7"

echo -n "$var" | wc -c

is probably the most portable solution.

"echo -n" is very non-portable. On many systems (in particular on
every UNIX(R) certified system, in a conforming environment) it will
output the string "-n".

If you want to be portable to very old systems (i.e. not assume
POSIX), then:

case `echo -n` in
-n) echo "$var\c" ;;
*) echo -n "$var" ;;
esac | wc -c

will work as long as $var does not contain any backslashes.
Otherwise:

wc -c << EOF
$var
EOF

--
Geoff Clare <netnews@gclare.org.uk>
Back to top
SumGuy
*nix forums beginner


Joined: 21 Jul 2006
Posts: 2

PostPosted: Fri Jul 21, 2006 1:34 pm    Post subject: Re: positional parameters of a var Reply with quote

Yeah .... ${#var} I thought before sending the post was the answer. It
wasnt working .. why ... because the knucklehead rookie on this end of
the key board didnt define the shell to run the script in #! /bin/ksh
.....

Thanks alot Chris



Chris F.A. Johnson wrote:
Quote:
On 2006-07-21, SumGuy wrote:
How can I obtain the positional parameters of a var

if

var=abc I want to show the output "3"
var=1234567 I want to show the ouput "7"

In any POSIX shell (bash, ksh, ash, etc.):

echo "${#var}"

In other shells:

expr "x$var" : '.*' - 1

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [6 Posts] View previous topic :: View next topic
The time now is Sat Nov 22, 2008 8:49 pm | All times are GMT
navigation Forum index » Programming » shell
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts AIX parameters for Oracle Vincent M Server 2 Thu Jul 20, 2006 6:58 pm
No new posts Must GET/POST Parameters Have Values? (And What is the Sy... David T. Ashley PHP 2 Wed Jul 19, 2006 1:10 am
No new posts timeout - ? & kernel parameters tomcaml@yahoo.com AIX 2 Tue Jul 18, 2006 9:03 pm
No new posts Calling functions with the wrong parameters Kenneth Brody C 7 Tue Jul 18, 2006 6:17 pm
No new posts Optimal parameters for Squid Prabu Squid 0 Mon Jul 17, 2006 9:42 am

Mortgage | PrePaid Credit Cards | Loans | Ringtone | Big Brother 9
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2059s ][ Queries: 16 (0.1267s) ][ GZIP on - Debug on ]