Problem with Array in Script


 
Thread Tools Search this Thread
Homework and Emergencies Emergency UNIX and Linux Support Problem with Array in Script
# 1  
Old 06-21-2010
Problem with Array in Script

Below is my script. This script is getting an error code such as this one.

Code:
fileListener.bat: entityArray[1]=craig.uss@pnc.com: not found
craig.uss@pnc.com
fileListener.bat: entityArray[2]=duns_noncusts.txt: not found
duns_noncusts.txt
fileListener.bat: entityArray[3]=duns_misc.cpy: not found
duns_misc.cpy
fileListener.bat: bad substitution

Code:
#!/bin/sh
cd /discovery/trigger
set -a entityArray[3]
count=1
while [ true ] 
do
    for f in `ls /discovery/trigger`
    do
   
   if [ $f -eq "*.trg" ]; then
     echo ${f}
     cd /export/home/disadm/BAT
     while read line
     do
         entityArray[$count]=$line
  echo $line
  
  if [ $count -eq 3 ]; then
      break
  fi
  count=`expr $count + 1`
            done < /discovery/trigger/${f}
     cd /export/home/disadm/BAT
     createentity \"${entityArray[1]}\" "Mainframe Flat Files" datafile \"${entityArray[2]}\" schemafile \"${entityArray[3]}\"
     cd /discovery/trigger
     ${f%.trg}
     mv ${f} ${f}.prc
     rm ${f}
    fi
     done    
done

I have no idea what is wrong with this code, or why it won't work. Please help

Last edited by Scott; 06-21-2010 at 01:39 PM..
# 2  
Old 06-21-2010
I think your problem is that you are using /bin/sh as your interpreter:
Code:
#!/bin/sh

I believe it does not support arrays, so its not able to parse your assignment statement properly and tries to execute it as a command and thus the error. Try using ksh:
Code:
#!/bin/ksh

# 3  
Old 06-21-2010
I tried

Thank you for helping me with this. I ran the script changing that to #!/bin/ksh

however, I get the following error
______________________________
$ r 275
filelistener
filelistener[12]: new.trg: bad number
filelistener[12]: new.trg: bad number
filelistener[12]: new.trg: bad number
filelistener[12]: new.trg: bad number
# 4  
Old 06-21-2010
You can not use '-eq' operator to compare strings. It is used with integers only.

change:
Code:
if [ $f -eq "*.trg" ]; then

to:

Code:
if [[ $f = *.trg ]]; then

# 5  
Old 07-27-2010
If you wanted to use Bash instead of Korn (and on some systems sh is Bash), you'll want to define arrays like this:
Code:
declare -a entityArray

And use them like this:
Code:
${#entityArray[*]} #Gets number of elements in array
${entityArray[$x]} #Gets x'th element is array
${entityArray[*]} #Gets all elements in array

Ben
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Array problem in Bash Script

I am trying to write a Bash Script using a couple of arrays. I need to perform a countdown of sorts on an array done once daily, but each day would start with the numbers from the previous day. This is what I'm starting with : #!/bin/bash days=(9 8 7 6 5) for (( i = 0 ; i < ${#days} ; i++... (4 Replies)
Discussion started by: cogiz
4 Replies

2. UNIX for Dummies Questions & Answers

Bsub array problem

I'm trying to run some computations on a cluster and have found that a bsub array is what I need.. So I tried running a simple example I found bsub -J "myArray" echo "Job \$LSB_JOBINDEX" This should create 10 jobs that output "Job 1" "Job 2" .. etc. But when I run this from afs space I get... (1 Reply)
Discussion started by: ehj
1 Replies

3. Shell Programming and Scripting

Using awk array problem

I am trying to map values in the input file, where 2nd column depends on the specific value in the 1st column. When 1st column is A place 1 into 2nd column, when it is B, place 2, when C place 3, otherwise no change. My input: U |100|MAIN ST |CLMN1|1 A |200|GREEN LN |CLMN2|2 1 |12... (4 Replies)
Discussion started by: migurus
4 Replies

4. Shell Programming and Scripting

Array reference problem

i have a variable MYHOST that has my host name.depending on the host i have an array like A_<hostname>.Everytime i need to append the hostname to A_ to get the array.but in the shell script i am nt able to access the members of that array. code of what i hav done: export temp=A_$MYHOST for... (15 Replies)
Discussion started by: niteesh_!7
15 Replies

5. Programming

Help on some array problem!!

i have no idea how to make a text file abc efg hij klm nop qrs to be a array such as, arr to be "abc efg" arr "hij kml" etc..... in C (2 Replies)
Discussion started by: tyckelvin1
2 Replies

6. Programming

Problem with array of pointers

Hi All, I am using the array of pointers and storing the address of string.This is a global list. So i am using extern to give the reference of this list to another file and using reading the data from this string. But list is being corrupted and string is missing some characters in... (2 Replies)
Discussion started by: lovevijay03
2 Replies

7. Shell Programming and Scripting

bash array problem

hi friends., i have two files yy.dat and mm.dat containing 110 elements in each if i read them into variables it is just showing only 4 elements instead of 110 elements My script is like this ################################## /bin/bash declare -a yy=(`cat yy.dat`) echo "No of values in... (1 Reply)
Discussion started by: yagnesh
1 Replies

8. Shell Programming and Scripting

Array problem

I am using /bin/ksh for this problem. I have created some arrays with variable names as the array names: cnt=1 { while read myline; do tempmeas="${meas%%;*}" cto="${meas#*;}" tempstream=$stream # wholemeas holds the name of the array # each array name... (0 Replies)
Discussion started by: ajgwin
0 Replies

9. Shell Programming and Scripting

problem with array=($(find ....)

hi, I get a *.dat files list in an array using: array=($(find . -name "*.dat")) the problem is that when a filename contains spaces, each space-separated token of the filename is in a different element of array. For instance if I have: x@x:~/tmp$ ls *.dat test1.dat test 2.dat ... (1 Reply)
Discussion started by: jul
1 Replies

10. Shell Programming and Scripting

array problem

Dear Experts, please help me out once again my array concepts is not very clear i have one text file like. 1|usa|hh 2|usa|ll 3|usa|vg 4|uk|nn 5|uk|bb 6|kuwait|mm 6|kuwait|jkj 7|dubai|hh i want to store the third fied of a text file in he array and after that it should give me some... (6 Replies)
Discussion started by: shary
6 Replies
Login or Register to Ask a Question