To store output of ls command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To store output of ls command
# 8  
Old 06-02-2010
if u want to store into a array arr with some sample
Code:
[root@temp]# arr=(`ls g*`)
[root@temp]# echo ${arr[*]}
get_opts.sh g.sh

# 9  
Old 06-02-2010
Hi,
Please see the below code
Code:
$ cat run.sh
arr=(`ls $1`)
echo ${arr[*]}

when i tried to execute the below command
Code:
$ . ./run.sh scr*
script1.sh

but the expected output will hold the below files

Code:
script1.sh  script2.sh  script3.sh

.

Please correct me if i did any wrong in the above code
# 10  
Old 06-02-2010
do just one change
Code:
$ cat run.sh
arr=(`ls $@`)
echo ${arr[*]}

# 11  
Old 06-02-2010
Code:
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 ----------

Code:
$ cat run.sh
arr=(`ls $@`) --> {`ls $@`}
echo ${arr[*]}

change the barackets this will also work. Smilie
# 12  
Old 06-02-2010
Hi,

I am getting below error msg

Code:
$ . ./run1.sh scr*
bash: arr[*]: bad array subscript

Below is the code.

Code:
$ cat run1.sh
arr={`ls $@`}
echo ${arr[*]}

Pls let me know for any corrections
# 13  
Old 06-02-2010
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.

Code:
$ 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.
# 14  
Old 06-02-2010
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.

Code:
$ vi run.sh
arr={`ls $@`}
echo ${#arr[*]}

i got the o/p as below
Code:
$ . ./run.sh scr*
1

Please update me why the above script returns 1 as o/p instead of 3.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python - store output of command to a variable

I am trying to store output of python command in variable. Could you please help how I can do that ? For example I am executing the following command - "CentOS" in server_desc The output would be True or False I would like to store the output in a variable say outPut and use condition... (4 Replies)
Discussion started by: atanubanerji
4 Replies

2. Shell Programming and Scripting

ksh : need to store the output of a awk command to a array

I have awk command : awk -F ' ' '{ print $NF }' log filename And it gives the output as below: 06:00:00 parameters: SDS (2) no no no no doc=4000000000). information: (6 Replies)
Discussion started by: ramprabhum
6 Replies

3. Shell Programming and Scripting

how to store output to a variable

I need some help: 1) I have a out put from a shell script, the out put looks like this: Attempting privilege escalation using sudo ... List backups for CLTST: Start date Status Ret. Class Label -------------------- ------------ ------------ ... (2 Replies)
Discussion started by: samk
2 Replies

4. Shell Programming and Scripting

How to store output of command to an array

Hello Guys, I am trying to store output of command to an array and then want to print, but it showing an error that "bad substitution". I am not getting why it's happening. Can anyone help me? #!/usr/bin/sh hr=0 min=0 i=1 last $1 | grep -w `date "+%b"` | grep -v '\(0:.*\)' | grep -vw sshd... (8 Replies)
Discussion started by: kasparov
8 Replies

5. Shell Programming and Scripting

store sqlplus output in variable

hi how can i store sqlplus output to a variable in sh script (not bash) Thanks MM (1 Reply)
Discussion started by: murtymvvs
1 Replies

6. Shell Programming and Scripting

store the output of "find" command in a variable?

I intend to find the path/full location of a file(filename given by user thru "read filenme") using "find" or any other command and then store it's output in a variable for some other processing. But struggling to put all things together (i.e finding the fully qualified location of that file and... (4 Replies)
Discussion started by: punitpa
4 Replies

7. UNIX for Dummies Questions & Answers

trouble using read to store values in variables from command output

I know there are caveats about using read in pipelines because read is treated by a subshell. I know this but I can't think of any way to accomplish this regardless, I'm still a rookie. I hope somebody will be able to interpret what it is that I'm trying to accomplish and correct me. ... (2 Replies)
Discussion started by: ProGrammar
2 Replies

8. Shell Programming and Scripting

To store the output in a variable

Hi, I am getting the following error while executing the script. Please can someone throw some light where is the problem. Many thanks. ./check: temp: not found The directory related to SEP instance 4 does not exist. The script is as follows. SEP_APP="/scp/sepx/app... (2 Replies)
Discussion started by: Sudhakar333
2 Replies

9. Shell Programming and Scripting

Store output and Echo

I will admit I am a newbie but I am trying to write some simple scripts Situation: I have a list of IP Addresses that I want to once or 2 times a day store the average ping response time in a database (mysql) I am part way there but not all the way there I have the following cat ./slow... (2 Replies)
Discussion started by: meyerder
2 Replies

10. Shell Programming and Scripting

store output to a file and read from it

Hello all, I need to run snoop command for a period of time (a day) and extract remote host column from it to find out who is accessing my server. When I run the following on the command line it works snoop -port 22 | awk '{print $3}' but when I do snoop -port 22 | awk '{print $3}' | while... (2 Replies)
Discussion started by: afadaghi
2 Replies
Login or Register to Ask a Question