ksh array question

ksh array question

Post by Jonh David Sto » Thu, 11 Jun 1998 04:00:00



This is a script I've been trying to convert from perl

#!/bin/ksh
dir=~/lib/sigs
set -A i $( ls $dir )
count=$( ls $dir | grep -c "." )
i=${i[$RANDOM%$count]}
cat ~/.sig.head $dir/$i > ~/.signature

It does what I want it to now, but I was wondering if there was a way to get
the dimensions of the array directly
There are also probably many useless uses of things so feel free to point
those out if you're bored

P.S. .sig.head contains the first line of my sig and the directory has
various quote type things in case that wasn't clear
--

Before they invented drawing boards, what did they go back to?

 
 
 

ksh array question

Post by Dan A. Merc » Thu, 11 Jun 1998 04:00:00



: This is a script I've been trying to convert from perl

: #!/bin/ksh
: dir=~/lib/sigs
: set -A i $( ls $dir )
: count=$( ls $dir | grep -c "." )


A few things you need to know:

assuming:

set -A S 1235 "456 789" 1011

$S is equivalent to ${S[0]}

Thus,  ${#S} gives you the character length of ${S[0]},  in this case
4.




the answer is 4.

"${S[*]}" returns all the elements in the array as a single argument


$ echo $#
3
$ echo $2
456 789

: i=${i[$RANDOM%$count]}

Within brackets,  text is presumed to be arithmetic variables,  so

i=${i[RANDOM%count]}

works just as well

: cat ~/.sig.head $dir/$i > ~/.signature

: It does what I want it to now, but I was wondering if there was a way to get
: the dimensions of the array directly
: There are also probably many useless uses of things so feel free to point
: those out if you're bored

: P.S. .sig.head contains the first line of my sig and the directory has
: various quote type things in case that wasn't clear
: --

: Before they invented drawing boards, what did they go back to?

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.

 
 
 

ksh array question

Post by nobo » Tue, 16 Jun 1998 04:00:00



Quote:> This is a script I've been trying to convert from perl
> #!/bin/ksh
> dir=~/lib/sigs
> set -A i $( ls $dir )
> count=$( ls $dir | grep -c "." )
> i=${i[$RANDOM%$count]}
> cat ~/.sig.head $dir/$i > ~/.signature

Since your question has already been well answered, I'll put forward a
non-array "one-liner" solution, if only to prove that you don't *have*
to use arrays for this kind of problem:

#!/bin/ksh
cat ~/.sig.head $( set -- ~lib/sigs/*; eval print \$$(( ( RANDOM % $# ) + 1 )) ) > ~/.signature

Quote:> Before they invented drawing boards, what did they go back to?

Basics.

Fred Surr
Melbourne

 
 
 

1. Ksh array question

Hi all,

I need some help using ksh arrays.

I've declared an array such and want to compare the users in the
/etc/passwd file against the elements of this arrray

<snip>
#!/bin/ksh

set -A users root daemon bin sys adm

for user in `cat /etc/passwd | cut -d':' -f1`
do
      if [ $user != ${users[*]} ]; then
             echo "you're not in my array"
      fi
done
<snip>

this works for the first user cut from the passwd file (root) but all
the others fail the if comparison.

What am I doing wrong?

Regards

Stuart.

2. cramfs

3. Ksh-88e shell array question

4. Backup recommendations?

5. working with arrays [ksh]

6. PC for linux

7. ksh array resizing?

8. string comparison in sh

9. Exporting array variables in ksh

10. How? ksh arrays...

11. ksh: using arrays as lists

12. ksh arrays

13. copy strings from file into KSH array