Storing filenames in an array in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Storing filenames in an array in shell script
# 1  
Old 05-28-2013
Storing filenames in an array in shell script

hi,

i am writing a shell script in which i read a line in a variable.

Code:
FNAME="s1.txt s2.txt s3.txt s4.txt s5.txt"

i want to create a array and store single file names in a array..
so the array should contain

Code:
arr[0]="s1.txt"
arr[1]="s2.txt"
arr[2]="s3.txt"
arr[3]="s4.txt"
arr[4]="s5.txt"

how to do this?
# 2  
Old 05-28-2013
Code:
$ FNAME="s1.txt s2.txt s3.txt s4.txt s5.txt"
$ FARRAY=($FNAME)
$ echo ${FARRAY[0]}
s1.txt
$ echo ${FARRAY[1]}
s2.txt

so long as your filenames don't have any whitespace.
This User Gave Thanks to Scott For This Post:
# 3  
Old 05-28-2013
It depends which shell you are using. In ksh you might need to declare an array first with:-
Code:
$ set -A FARRARY

I think bash may allow use of arrays by default, but I am happy to be corrected.



Robin
Liverpool/Blackburn
UK
# 4  
Old 05-28-2013
removed

Last edited by Jotne; 05-28-2013 at 09:11 AM.. Reason: Removed due to error
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with the shell script after storing the directory in a variable

FTP is connecting to the server but i am getting an error - Enter if the env is dev or test or prod: test Please enter the id no : xxxxxxx Connected to xxxx 220 (vsFTPd 2.2.2) 331 Please specify the password. 230 Login successful. ?Invalid command ?Invalid command ?Invalid command... (3 Replies)
Discussion started by: chandraprakash
3 Replies

2. Shell Programming and Scripting

Storing the Linux command output to an array in perl script

Hi I am trying to store the output of a command into an array in perl script. I am able to store but the problem is i am unable to print the array line with one line space. i mean i inserted the \n in loop ...but not getting the result. I have written like this #!/usr/bin/perl @a =... (2 Replies)
Discussion started by: kumar85shiv
2 Replies

3. Programming

C; storing strings in an array

I am trying to get userinput from stdin and store the lines in an array. If i do this: using a char **list to store strings allocate memory to it #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { char *prog = argv; char **linelist; int... (5 Replies)
Discussion started by: tornow
5 Replies

4. Shell Programming and Scripting

sed not storing output in shell script

Hello, Following my learning of shell scripting.. I got stuck yet again. If I execute this command on terminal: $ sed "s/off/on/g" file > fileAUX I successfully change the text off to on from file to fileAUX. But the same command is not working inside a shell script. I tested and... (2 Replies)
Discussion started by: quinestor
2 Replies

5. Shell Programming and Scripting

Problems with storing oracle sqlplus query output shell script

Hello everyone, I have a RHEL 5 system and have been trying to get a batch of 3-4 scripts each in a separate variables and they are not working as expected. I tried using following syntax which I saw a lot of people on this site use and should really work, though for some reason it doesn't... (3 Replies)
Discussion started by: rockf1bull
3 Replies

6. Shell Programming and Scripting

Storing cutted filenames into variable

Hi, I'd like to write a script which works on various files file1.jpg, file2.jpg ..... These files are splitted and their names are something like file_.XXX. I'd like to merge them and convert them at some moment again in file.XXX also file1_1.jpg file1_2.jpg ... >file1.pdf file2_1.txt... (2 Replies)
Discussion started by: bsco
2 Replies

7. Shell Programming and Scripting

Storing the values in text file using while loop in shell script

Hi Frdz while read line do name=`echo $line | cut -d' ' -f 1 ` password=`echo $line | cut -d`-` -f 2` name > logfile.txt password > logfile.txt done < list.txt When it is run, am getting last values in list.txt file only,it is not storing lall the list entry values. How can i... (5 Replies)
Discussion started by: KiranKumarKarre
5 Replies

8. Shell Programming and Scripting

storing variables in array.Please help

Hi All, I need some help with arrays. I need to take input from the user for hostname, username and password until he enters .(dot) or any other character and store the values in the variable array. I would further connect to the hostname using username and passwd and copy files from server to... (7 Replies)
Discussion started by: nua7
7 Replies

9. Shell Programming and Scripting

How to list filenames with spaces in shell script

Hi, I want to list all the files matching in a directory directory given below Here one of the folder has a space in the path. /MAS02/RMS/WBDev/Krishna/Krishna Mohan/FMSplitByKeyAfterChanges1000075383.fileman.*.txt.out.1000075383.0 The following works from command line... (1 Reply)
Discussion started by: hikrishn
1 Replies

10. UNIX for Dummies Questions & Answers

Storing pointer array in C

All .. I am having a pointer array . And trying to store the addess into that pointer array . please see below the problem i faced code: int cnt1; char *t_array; char *f_array; for(cnt1=0; cnt1<1000; cnt1++) { t_array =... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies
Login or Register to Ask a Question