1826417 Members
3812 Online
109692 Solutions
New Discussion

Script Question

 
Shannon_1
Occasional Advisor

Script Question

Using the Bourne shell is there a command to
find the length of a string?
14 REPLIES 14
Rodney Hills
Honored Contributor

Re: Script Question

Try-

x="abcdef"
echo ${#x}
6

-- Rod Hills
There be dragons...
Jeffrey Davis_1
Frequent Advisor

Re: Script Question

Hi. Try this: echo $PWD | wc -m
Anthony deRito
Respected Contributor

Re: Script Question


#man wc

wc recognizes the following command-line options:

-c Write to the standard output the number of bytes in
each input file.

-m Write to the standard output the number of characters
in each input file.

-w Write to the standard output the number of words in
each input file.

-l Write to the standard output the number of newline
characters in each input file.
Rajeev Tyagi
Valued Contributor

Re: Script Question

echo $string | wc -c

hpuxrox
Respected Contributor

Re: Script Question

echo "12345" | wc -c

Then minus 1
MANOJ SRIVASTAVA
Honored Contributor

Re: Script Question

Hi Shannon

What about wc -c , where in it counts the no. of bytes in the input .


Manoj Srivastava
A. Clay Stephenson
Acclaimed Contributor

Re: Script Question

Another method:

S1="This is a test"
LEN=`expr length "${S1}"`

If it ain't broke, I can fix that.

Re: Script Question

Hi,

Rodneys method works fine if you mean to use the posix shell (/usr/bin/sh on HP-UX). The *real* bourne shell on HP-UX is in /usr/old/bin/sh. Posix shells aren't available on all flavours of UNIX so if you *really* want to use the bourne shell then the methods using wc will work fine - but remember that wc will include the new line from the echo command in the output so you will always get the length of your string plus one unless you tell echo not to include the newline:

# /usr/old/bin/sh
# x="1234"
# echo ${#x}
bad substitution
# exit

# x="1234"
# echo ${#x}
4
# echo $x | wc -m
5
# echo "$x\c" | wc -m
4


HTH

Duncan

I am an HPE Employee
Accept or Kudo
Rodney Hills
Honored Contributor

Re: Script Question

Duncan's answer is correct.

I bad.

I started /usr/bin/sh and forgot it was posix.

Good call Duncan...

-- Rod Hills
There be dragons...
hpuxrox
Respected Contributor

Re: Script Question


$YOUR_STRING=1234567
$STRING_NUM=`echo "$YOUR_STRING\c" | wc -c`

#echo $STRING_NUM
7
David Burgess
Esteemed Contributor

Re: Script Question

a=$(echo hello | wc -c)
((a=a-1))
echo $a
5

Regards,

Dave.
John Palmer
Honored Contributor

Re: Script Question

The ${#} construct is by far the most efficient because it is evaluated by the shell itself rather than calling a seperate process.

Bear in mind however that it is not valid in the real Bourne shell (/usr/old/bin/sh). You're not using that are you?

Regards,
John






harry d brown jr
Honored Contributor

Re: Script Question

Shannon,

Try this:

$ DAH1="this is a test"
$ echo $DAH1
this is a test
$ echo ${#DAH1}
14
$

If it works you are really using the posix shell. The bourne shell in 10+ is in /usr/old/bin. The bourne shell still exists for older scripts, and should be "ported" to the posix shell.

live free or die
harry
Live Free or Die
Magdi KAMAL
Respected Contributor

Re: Script Question

Hi Shannon,

# var=0123456789
# echo ${#var}
10

Magdi