Array declaration in Shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Array declaration in Shell script
# 1  
Old 02-28-2011
Data Array declaration in Shell script

this is my code

Code:
        declare -a USERCERT
        declare -a CACERT
        declare -a KEYSRC

this is the error

Code:
+ declare -a USERCERT
./clone.sh: 1: declare: not found
+ declare -a CACERT
./clone.sh: 1: declare: not found
+ declare -a KEYSRC
./clone.sh: 1: declare: not found


Suggest resolution....
# 2  
Old 02-28-2011
Code:
# ksh
typeset -A array 
# bash
declare -a array

# 3  
Old 02-28-2011
Quote:
Originally Posted by xerox
Code:
+ declare -a KEYSRC
./clone.sh: 1: declare: not found


declare is a bashism not found in other shells. In ksh, use typeset.

And most shells do not have arrays at all.
# 4  
Old 02-28-2011
i need it in shell script....

Is it not possible to define my own array name?
# 5  
Old 02-28-2011
What is your shell ? ksh, bash, ...
And most of use case, you don't need to define array. Use it.
Code:
a[1]=value
a=( * )
set -A array  -- $*

This User Gave Thanks to kshji For This Post:
# 6  
Old 02-28-2011
bash...

so the array size doesnt't matter?

Code:
set -A array  -- $*

what does this line do?
# 7  
Old 02-28-2011
Quote:
Originally Posted by xerox
i need it in shell script....

Is it not possible to define my own array name?

Yes ... if you are using a shell which supports arrays.

First, don't declare the arrays; just assign values to them.

---------- Post updated at 01:21 AM ---------- Previous update was at 01:13 AM ----------

Quote:
Originally Posted by xerox
bash...

If you were running the script in bash, you wouldn't have seen those error messages.
Quote:

so the array size doesnt't matter?

No (so long as there is enough memory).
Quote:

Code:
set -A array  -- $*

what does this line do?

In bash, there is no -A option to set.

In ksh, it assigns all the words (not the arguments) on the command line as elements in the array named array.

The bash equivalent (which also works in ksh93) is:
Code:
array=( $* )

Though what was probably meant was:
Code:
set -A array  -- "$@"

Which should be written:
Code:
array=( "$@" )


Last edited by cfajohnson; 02-28-2011 at 02:29 AM..
This User Gave Thanks to cfajohnson For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding Two Array in shell script

Hi Experts, I've got this problem where I need to add two array in shell script such that that is an element is greater than 9 then it get further split into individual digit, something like below : Input :- array1=(2 6 8 9 10 12) array2=(5 4 6 8 12 14) Output :- array3=(7 1 0 1 4 1 7 2 2... (8 Replies)
Discussion started by: mukulverma2408
8 Replies

2. Shell Programming and Scripting

Pass C shell array to another C shell script(csh) and shell(sh)

Dear Friends, Please help me on this my script name is send.csh In this i have written the statement like this set args = ( city state country price ) I want to pass this array to another c shell called receiver.csh. and i want to use it in this c shell or how to pass to... (2 Replies)
Discussion started by: SA_Palani
2 Replies

3. Shell Programming and Scripting

Need Shell Script for Array

Hi all, I have an input file like below (a comma seperated file) 345,12,10 400,11,8 328,1,3 I need to get the output as below ... record 345 sum is 12 record 400 sum is 10 record 328 sum is 1 record 345 count is 10 record 400 count is 8 record 328 count... (15 Replies)
Discussion started by: hemanthsaikumar
15 Replies

4. Shell Programming and Scripting

Global declaration of Array in Shell

Hi, Have assigned values in Array and iterating in while loop and would require values outside of the loop. But its returning NULL. could you please help us how to define Global array declaration in Unix shell scripting? i am using Kron shell. Thanks in advance. (2 Replies)
Discussion started by: periyasamycse
2 Replies

5. Shell Programming and Scripting

Array in shell script

Hi, check=("/usr/local/bin/chk_nag | awk -F":" '{print $1}'" "/usr/local/bin/chk_kas | awk -F":" '{print $1}'" "/usr/local/bin/chk_was | awk -F":" '{print $1}'" ) for i in "${check}"; do echo $i; done when I run this. It says Syntax error: "(" unexpected Please advise. (5 Replies)
Discussion started by: ashokvpp
5 Replies

6. Shell Programming and Scripting

Comparing two array in shell script

In shell script i have two variables with value debit_sal="DOG,HIU,JIU,GYT,PPI,KIU,HUY........." debit_req='HIU, JIU, HUY, GTR, KOI, ............" i stored this two in two array arr=$(echo $debit_sal| tr "," "\n"); arr1=$(echo $debit_req| tr ", " "\n"); #(note second arry has comma with... (5 Replies)
Discussion started by: greenworld123
5 Replies

7. UNIX for Dummies Questions & Answers

Array declaration problem

Hi all, I would like to declare a vector of variables and access them sequentially. Here is my code ARRAY_CT="0001000000 0000100000 0000010000" ELEMENTS_CT=${#ARRAY_CT} echo $ELEMENTS_CT for (( j=1;j<=$ELEMENTS_IS;j++)); do echo ${ARRAY_IS} done ... (2 Replies)
Discussion started by: f_o_555
2 Replies

8. Solaris

i cannot give array declaration in shell script

Dear all, i m unable to give array decalartion in solaris Operating system shell script and script was so.sh declare -a bull for i in 1 2 3 4 5 6 7 8 9 do bull=$i echo "${bull}" done it is throwing an error called so.sh: declare: not found so.sh: bull=1: not... (20 Replies)
Discussion started by: naree
20 Replies

9. Shell Programming and Scripting

Global variable declaration in shell scripting

Hi all, How to print variable value which i have assigned inside for loop because that variable scope is local, but i want to use that variable outside for loop. Awaiting your great help Thanks, Susil (1 Reply)
Discussion started by: susilgandhi
1 Replies

10. Shell Programming and Scripting

Array Declaration and For Loop

I am just stucked in syntax.This is more like a array and for loop problem. I want to use ls -l command and get filezise and filename of all filenames in the directory in an array (say array#1). After 2 minutes of sleep, i want to get the same information in another array (say array#2). The... (4 Replies)
Discussion started by: 33junaid
4 Replies
Login or Register to Ask a Question