| Author |
Message |
SumGuy *nix forums beginner
Joined: 21 Jul 2006
Posts: 2
|
Posted: Fri Jul 21, 2006 12:38 am Post subject:
positional parameters of a var
|
|
|
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
|
Posted: Fri Jul 21, 2006 1:09 am Post subject:
Re: positional parameters of a var
|
|
|
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
|
Posted: Fri Jul 21, 2006 2:19 am Post subject:
Re: positional parameters of a var
|
|
|
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
|
Posted: Fri Jul 21, 2006 2:25 am Post subject:
Re: positional parameters of a var
|
|
|
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
|
Posted: Fri Jul 21, 2006 12:58 pm Post subject:
Re: positional parameters of a var
|
|
|
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
|
Posted: Fri Jul 21, 2006 1:34 pm Post subject:
Re: positional parameters of a var
|
|
|
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 |
|
 |
|