Accessing arrays in shell scripts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Accessing arrays in shell scripts
# 1  
Old 09-16-2008
Accessing arrays in shell scripts

Hi All,

I have an array in my script.

For example,
array=(file1.xml,file1-summary.xml,file2.xml,file2-summary.xml,file3.xml,file3-summary.xml);


I am accessing the elements of the array by using the following code.

len=${#array[*]};

while [ $j -lt $len ]; do
echo "${array[$j]}"
done

I want to print the only files like file1.xml,file2.xml etc.

I don't want to print file1-summary.xml,file2-summary.xml etc.

For that I am uisng the following piece of code.

len=${#array[*]};

while [ $j -lt $len ]; do
if [ ${array[$j]} != *.summary.xml ]then;
echo "${array[$j]}"
fi
let j++;
done

But this code prints all the files including the files like file1-summary.xml etc.

Can anyone help me on this....

Thanks in advance...
Anand.
# 2  
Old 09-16-2008

Separate the elements with spaces and/or newlines, not commas:

Code:
array=(file1.xml file1-summary.xml file2.xml
 file2-summary.xml file3.xml file3-summary.xml)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Accessing secured server using shell script

Hi, I was trying to connect secure server using SFTP, but end up in error. Im looking for a shell script which will connect my secured FTP server For EG: hostname = example.com username = fed password = pass port = 993 Destination = web Push some tar file into web folder which is in... (3 Replies)
Discussion started by: Paulwintech
3 Replies

2. Shell Programming and Scripting

Arrays in Shell Scripts

I have defined an array like this: set -A MYARRAY MYARRAY=file1 MYARRAY=file2 MYARRAY=file3 MYARRAY=file4 i=0 while } ]] do echo "File Name $i :" ${MYARRAY} i=`expr $i + 1 ` echo "value of i=" $i done This works perfectly and shows... (4 Replies)
Discussion started by: Hangman2
4 Replies

3. AIX

Accessing Oracle DB via Shell possible ?

At work we are using AIX 4.2 for the client. On this system, there is Oracle and kron, Bash and C shell. Is it possible to access Oracle via the shell to create/update tables ? There is this hourly data that we accumulate on the AIX and we manually copy the infos to Excel for stats. I thought... (2 Replies)
Discussion started by: Browser_ice
2 Replies

4. Shell Programming and Scripting

Accessing shell arrays in awk

I am trying to pass a shell array I created to match on values amongst other things. I have a sample: #! /bin/ksh i=0 for id in `cat id.lst` do IDs=$id (( i=i+1 )) done cat sqloutput.txt | awk -F, -v ids=$ID '{ # Do stuff }' But it looks as if I cannot do this.... (1 Reply)
Discussion started by: Zombywoof
1 Replies

5. Shell Programming and Scripting

Accessing awk array from shell

Hi, i want awk to read a file and place it's content into two arrays. When trying to read these arrays with a "for a in ${source_path} "-Loop it gives the right result. But when trying to access directly (/bin/echo ${source_path}) it doesn't work. I read "all "the awk threads in this forum and... (6 Replies)
Discussion started by: bateman23
6 Replies

6. Shell Programming and Scripting

Accessing aliases within a shell script

I am not able to access the aliases in my environment within a Python script. If I pass the alias to os.system(), I get a message saying "sh: x: not found". I've tried sourcing my .bashrc file in the script, but this does not work. I would perfer not to source my or any rc file because this... (9 Replies)
Discussion started by: cooldude
9 Replies

7. Shell Programming and Scripting

Problem in accessing variables outside shell

Hi, I have a shell script wherein i am doing some file operations and storing the data in some variables. I am exporting these variables as i need to use them outside shell. Then within the shell i am launching GDB session hoping that i will be able to access the exported variables in the GDB... (2 Replies)
Discussion started by: jsantosh
2 Replies

8. Shell Programming and Scripting

Accessing the parameters of a shell script

Hi, I have one situation. I am developing a shell script to which parameters will be passed from a Web based User Interface using some Business Process(BP).There are some 6 parameters for which user will enter the values in UI. These values will be passed to script by BP in the form -... (2 Replies)
Discussion started by: The Observer
2 Replies

9. Shell Programming and Scripting

Accessing Shell Variables in awk or sed

Hello, I wonder if it is possible to pass and use variables from shell environment into sed or awk. I am trying to achieve something similar to the following using sed or awk: var=some_regular_expression grep "$var" filename # Will extract lines from filename The following code,... (3 Replies)
Discussion started by: nasersh
3 Replies

10. UNIX for Dummies Questions & Answers

accessing shell script variable in file

Hi, I have a shell script in which there is a file conn_$temp where $temp has the pid of the shell script. in this shell script i have an embedded awk script that must read the file while ((getline < "conn_$temp") > 0) However due to the "$temp" in the file name, the awk script is... (6 Replies)
Discussion started by: HIMANI
6 Replies
Login or Register to Ask a Question