Parameters + arrays in unix shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parameters + arrays in unix shell
# 1  
Old 10-05-2010
Parameters + arrays in unix shell

Say I have ./param HEY

What would I do if I wanted to store each character into an array?

Example.

ARRAY1[0]="H"
ARRAY1[1]="E"
ARRAY1[2]="Y"

thank you!
# 2  
Old 10-05-2010
Code:
S="HELLOWORLD"
for ((i=0; i<${#S}; i++))
do A[$i]=${S:$i:1}
done

# 3  
Old 10-05-2010
Neat /bin/bash solution frans, here is one with /bin/sh
Code:
#!/bin/sh
i=1
LEN=$( echo $1 | wc -c )
while [ $i -lt $LEN ]
do
   ARRAY[$i]="$( echo $1 | cut -c$i )"
   let i=i+1
done
echo Element #3 is ${ARRAY[3]}

This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 10-05-2010
Quote:
Originally Posted by Chubler_XL
Neat /bin/bash solution frans, here is one with /bin/sh
Code:
#!/bin/sh
i=1
LEN=$( echo $1 | wc -c )
while [ $i -lt $LEN ]
do
   ARRAY[$i]="$( echo $1 | cut -c$i )"
   let i=i+1
done
echo Element #3 is ${ARRAY[3]}

I like this answer!
# 5  
Old 10-05-2010
Quote:
Originally Posted by puttster
I like this answer!
I don't Smilie look like your sh is symlinked to bash

Quote:
Therefore the Bourne shell has one array, but only one.

Did you know that
Quote:
Originally Posted by Chubler_XL
Code:
LEN=$( echo $1 | wc -c )

Should be
Code:
LEN=${#$1}


Last edited by danmero; 10-05-2010 at 11:00 PM..
# 6  
Old 10-05-2010
Quote:
Originally Posted by danmero
I don't Smilie look like your sh is symlinked to bash




Did you know that

Should be
Code:
LEN=${#$1}

why is that?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell arrays need help

Ok so spaces separate elements. What if you wanted an element to have a space in it? For instance: nums="one two three and a half" where "three and a half" is THE SAME element? (3 Replies)
Discussion started by: stevenswj
3 Replies

2. Shell Programming and Scripting

Using arrays in shell

I have three arrays. One is Master array and that has list of other array in config file. for e.g (for simplicity I have only defined array with 2 elements each) set +A MASTERARRAY SQLUPDATE_ONETIME SQLUPDATE_DAILY END_OF_ARRAY set +A SQLUPDATE_ONETIME update12 update22 END_OF_ARRAY... (4 Replies)
Discussion started by: anish
4 Replies

3. Shell Programming and Scripting

Unix Shell Script to loop over Directory and send Filesname as parameters

Hi there I'm new to UNIX scripting; I’m stuck with the following I have an Oracle SQL script that takes three parameters 1- File Name 2- File Path 3- File creation date Under UNIX I have a folder where files will be placed frequently and I need to upload those files to Oracle, what I need... (3 Replies)
Discussion started by: windjashi
3 Replies

4. Shell Programming and Scripting

arrays in C shell

hi guys, i have the following code in C shell.. set i=0 while ($i < 11) master_array=${ARRAY} i++ done it gives me error at line 3: Variable syntax. what is wrong here? any help is appreciated. (4 Replies)
Discussion started by: npatwardhan
4 Replies

5. Shell Programming and Scripting

I need help with arrays in C Shell

Hi guys could you please post links that explain how to use and manipulate arrays in c shell (.csh files) ? examples are useful too :rolleyes: (5 Replies)
Discussion started by: domain
5 Replies

6. Shell Programming and Scripting

C shell arrays

how do you declare an array in the C shell and loop through each element? (2 Replies)
Discussion started by: npatwardhan
2 Replies

7. Shell Programming and Scripting

help me in sending parameters from sqlplus script to unix shell script

Can anybody help me out in sending parameters from sql*plus script to unix shell script without using flat files.. Initially in a shell script i will call sql*plus and after getting some value from some tables, i want that variable value in unix shell script. How can i do this? Please tell me... (2 Replies)
Discussion started by: Hara
2 Replies

8. Shell Programming and Scripting

how to use arrays in c shell

hi :) i need help to explain arrays 2D in c shell like this in c++ int a (6 Replies)
Discussion started by: hgphsf
6 Replies

9. UNIX for Dummies Questions & Answers

Call a UNIX shell with parameters from C

Hello...I hava quite a problem, couldn't find a solution anywhere :(. I have a C program, and from that C program I have to call a shell script. This is not difficult, I can do it using the "system" command from C. But the ugly part is how can I send as parameters some variables? For example...i... (1 Reply)
Discussion started by: dustman
1 Replies
Login or Register to Ask a Question