Hi,
I need to pass a run time input to unix script which tracks the list of files in the directory under certain pattern. The files which are matched to the given pattern then need to be stored in the array. Requesint you all to please suggest me the solution.
I gave the below command in the sh file and when i tried to execute i got only the last entry in the filelist. pls suggest me the possible solution.
purpose of storing these file name in the array:
i have a file with list of strings to be searched in above retrived files.
ls scr* >> filelist
ls
dead.letter err_1705101841.txt
error1.file err_1705101842.txt
practice_a err_1805101940.log
pyramid.sh err_1805101942.log
pyramid1.sh pyramid2.sh
ls err* >> ls_store.sh
cat ls_store.sh
It works fine..
Hi,
Please try to check by passing input paramter at run time.
Thanks
frans
June 2, 2010, 2:36am
4
Assuming filenames don't contain any spaces:
ARRAY=($(ls err* | tr '\n' ' '))
Hi,
Pasted the code for your reference.
$ cat run.sh
echo $1
ARRAY=($(ls $1 | tr '\n' ' '))
When i tried to execute,
$ . ./run.sh scr*
I got the below response only.
script1.sh
When i tried to execute the cmd directly, i got below update.
$ ls scr*
script1.sh script2.sh script3.sh
Please correct and update, why script1.sh file was alone printed when we pass runtime parameter as scr*
Thanks in advance
#!/usr/bin/ksh
ls
will nt this works.?
u get all ur file displayed
when u do ls scr* (if their is a file in ur directory that will get displayed)
Hi ..
I need to store the files which are matched to my input pattern. I don'tt want to store all files.
posix
June 2, 2010, 3:30am
8
if u want to store into a array arr with some sample
[root@temp]# arr=(`ls g*`)
[root@temp]# echo ${arr[*]}
get_opts.sh g.sh
Hi,
Please see the below code
$ cat run.sh
arr=(`ls $1`)
echo ${arr[*]}
when i tried to execute the below command
$ . ./run.sh scr*
script1.sh
but the expected output will hold the below files
script1.sh script2.sh script3.sh
.
Please correct me if i did any wrong in the above code
posix
June 2, 2010, 4:03am
10
do just one change
$ cat run.sh
arr=(`ls $@`)
echo ${arr[*]}
ls
iarr={`ls $1`}
echo ${arr[*]}
it works for me.
instead of ur run.ksh i m using ls to get the files out. and changing the normal barackets to circular ones ...
---------- Post updated at 03:06 AM ---------- Previous update was at 03:03 AM ----------
$ cat run.sh
arr=(`ls $@`) --> {`ls $@`}
echo ${arr[*]}
change the barackets this will also work.
Sekar1
June 2, 2010, 4:45am
12
Hi,
I am getting below error msg
$ . ./run1.sh scr*
bash: arr[*]: bad array subscript
Below is the code.
$ cat run1.sh
arr={`ls $@`}
echo ${arr[*]}
Pls let me know for any corrections
hi posix,
I tried your method, and its showing me
execute permission denied.!
I have give 755 permission to all files in my directory...
---------- Post updated at 04:29 AM ---------- Previous update was at 04:09 AM ----------
If u just need to store the file names in another file then u can try this option.
$ cat run.sh
read name
ls $name >> file_name.txt
Make sure while entering the file pattern you put a .
Example: err
In this case file_name.txt will have all the files matching the patter.
Sekar1
June 2, 2010, 5:32am
14
i tried to check how many files got retrived for the below code. It displays as 1. but there are 3 files in the dir which matches to the pattern.
$ vi run.sh
arr={`ls $@`}
echo ${#arr[*]}
i got the o/p as below
$ . ./run.sh scr*
1
Please update me why the above script returns 1 as o/p instead of 3.