Simple Array in Ksh Scripting


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Simple Array in Ksh Scripting
# 1  
Old 11-01-2006
Simple Array in Ksh Scripting

Ksh Scripting

Can some one give me a simple example of array operations using ksh.

For Ex:
week_array = {Sunday Monday Tuesday Wednesday Thursday Friday Saturday}

I want to assign and retrieve and print them along with their index.

I am looking for the o/p like:
0 Sunday
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
# 2  
Old 11-01-2006
I don't know if there's any way to retrieve the index/subscript directly. You'll probably have to count and increment it yourself:
Code:
set -A week_array Sunday Monday Tuesday Wednesday Thursday Friday Saturday
integer idx=0

for day in ${week_array[*]}
 do
  print $idx $day
  idx=$idx+1
done

You can also set the array variables this way:
Code:
week_array[0]=Sunday
week_array[1]=Monday
week_array[2]=Tuesday
week_array[3]=Wednesday
week_array[4]=Thursday
week_array[5]=Friday
week_array[6]=Saturday

# 3  
Old 11-01-2006
Thats cool Glenn. I am getting confused with different shells.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Simple Shell Scripting

1. The problem statement, all variables and given/known data: An argument example: ../path/cse/lab3/remove Right now, it's printing out all the directory and files in 'lab3'. I want it to print out all the files in 'remove'. I'm not sure how to do that. (I want to use a for loop) 2.... (2 Replies)
Discussion started by: spider-man
2 Replies

2. Shell Programming and Scripting

ksh scripting- array format

Hi Everyone, I have a ksh script that queries a database. The query output looks like this: $queryResult = echo ${queryResult} = name1 echo ${queryResult} = age1 echo ${queryResult} = name2 echo ${queryResult} = age2 ... I need to insert those values into a new array that would... (2 Replies)
Discussion started by: practitioner
2 Replies

3. Shell Programming and Scripting

simple array problem

Hello experts, I need help in my code. I have an input file like this: 100814 1205 1724127 7451382 -10 00:30:1b:48:92:3a 100814 1206 1724127 7451382 -72 00:30:1b:48:92:3a 100814 1207 1724127 7451382 -72 00:30:1b:48:90:3b 100814 1208 1724127 7451382 -72 00:30:1b:48:92:3a 100814 1209... (12 Replies)
Discussion started by: enes71
12 Replies

4. UNIX for Dummies Questions & Answers

simple way to read an array in ksh

hi, I'm a newbie to shell scripting. I wanted to initialise an array using basic for loop and read it. Then i want to print it as a .CSV file.. Any help would me much appreciated.. (1 Reply)
Discussion started by: pravsripad
1 Replies

5. Shell Programming and Scripting

Simple list file ls to an array

Hi all, Simple question, how can I simply create an array from listing the files in a directory i..e myvar=`ls`; echo $myvar gives a fulllist of files/ directories how can I now convery myvar to an array so I can loop around and read each file/dir? Thnaks CF:) (6 Replies)
Discussion started by: cyberfrog
6 Replies

6. Shell Programming and Scripting

Simple scripting.

echo "what is your username?" read username echo $username echo /home/$username $backup="backup" $restore="restore" # # if then echo "No username provided" else echo "hi $username would you like to backup or restore?" read userrequest echo $userrequest if then ... (4 Replies)
Discussion started by: EwanD
4 Replies

7. Shell Programming and Scripting

help with simple korn scripting

Hi, The logic is very simple but I can't seem to make this work in Korn shell. I need to check two files to make sure there is no errors. Each of the file will have number. For example, first file btt.txt will have 112 which is good. Second file bgg.txt will have 6 which is also good. If I... (4 Replies)
Discussion started by: samnyc
4 Replies

8. Shell Programming and Scripting

Simple Scripting Problem

Hi there, I was trying to add a line of text in the middle line of a file. I have counted the lines in the file, and then I divide it into 2, after that I am stuck on how am I suppose to append the line on that file? When I tried to use this command 'second line >> filename' it appends it at... (3 Replies)
Discussion started by: felixwhoals
3 Replies

9. Shell Programming and Scripting

Simple ksh question

Hi, Simple question I am sure... I need to put yesterdays date in the name of a filename, ie assign yesterdays date to a variable and then I can use it where I want.. using the format yyyymmdd Any suggestions? (1 Reply)
Discussion started by: frustrated1
1 Replies

10. UNIX for Dummies Questions & Answers

any tutorials on simple scripting?

i'm not looking for anything that deals with "if-then" scripts. i'd like something simple on how to run a series of processes. for example the following: 1. ftp://ftp.netbsd.org/pub/NetBSD/NetBSD-current/tar_files/ 2. lcd / 3. get pkgsrc.tar.gz 4. bye 5. cd /usr 6. rm -rf pkgsrc 7. cd... (3 Replies)
Discussion started by: xyyz
3 Replies
Login or Register to Ask a Question