How to store output of command to an array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to store output of command to an array
# 1  
Old 11-13-2011
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?
Code:
#!/usr/bin/sh
hr=0
min=0
i=1
last $1 | grep -w `date "+%b"` | grep -v '\(0[2-9]:.*\)' | grep -vw sshd | cut -
c 66- | tr -d "[ ]\(\)" | grep -v '[a-z].*' | while read line
do
        array[ $i ]="$line"
        i=$i+1
done
#print an array
for i in "${array[@]}"
do
    echo $i
done

Thanking You,
# 2  
Old 11-13-2011
This is the same problem you were having before. Using bash and processing piped output with a while read leaves any variables set or created inside of the loop unchanged or undefined following the execution of the while.

The bad substitution is probably the result of the array not being defined, though when I execute your code under bash it fails silently.

If you really must do the processing in script then you're probably going to need to redirect the output to a tmp file and read the lines into an array after the fact.
# 3  
Old 11-13-2011
Thanks Agama,

I am trying to do without any temp file. Is that possible in that case? I would really appreciate your help.

Thanking You,
# 4  
Old 11-13-2011
Thinking on it a bit, this should work without the tmp file:

Code:
#!/usr/bin/sh
hr=0
min=0
i=1
typeset -a array
o=$( last $1 | grep -w `date "+%b"` | grep -v '\(0[2-9]:.*\)' | grep -vw sshd | cut -c 66- | tr -d "[ ]\(\)" | grep -v '[a-z].*' )
array=($o)
echo ${array[0]}

for i in "${array[@]}"
do
    echo $i
done

exit

# 5  
Old 11-13-2011
Unforrunatly it's not working. I had tried before. It showing error "syntax error at line 6: `o=$' unexpected".
# 6  
Old 11-13-2011
@kasparov: you should tell what OS and shell you are running.

In any case, your script should works fine if /usr/bin/sh is substituted by /bin/ksh93 which unlike bash makes the right choice when selecting what element of a pipeline not to run in a sub shell.
# 7  
Old 11-13-2011
You could try the obsolete form with back quotes, but jlliagre has hit it spot on: what O/S and what version of the shell? Today many Linux implementations point /bin/sh to bash, but there's a chance it's not bash especially if it is generating an error with o=$(command).

Code:
o=`pipeline`

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

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. 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

4. Shell Programming and Scripting

Storing command output in an array

Hi, I want keep/save one command's output in an array and later want to iterate over the array one by one for some processing. instead of doing like below- for str in `cat /etc/passwd | awk -F: '$3 >100 {print $1}' | uniq` want to store- my_array = `cat /etc/passwd | awk -F: '$3 >100 {print... (4 Replies)
Discussion started by: sanzee007
4 Replies

5. Shell Programming and Scripting

Store the output values in array

Hi, How to store the values in array from output result, EG: I have the result like this, ps, google, 1.txt, 1 sam, google, 2.txt, 2 These are the four values followed by comma in two sets. I need to store these values set by set. One set contains four values followed by comma. ... (2 Replies)
Discussion started by: KarthikPS
2 Replies

6. Shell Programming and Scripting

Store all the passed arguments in an array and display the array

Hi I want to write a script which store all the parameters passed to the script into an array. Once it is stored I want scan through the array and and delete those files for last month present inside the directory. The files in directory is appneded with YYYY_MM_DD. I want to know how can I... (3 Replies)
Discussion started by: dgmm
3 Replies

7. Shell Programming and Scripting

awk - Pre-populating an array from system command output

So, here's a scenario that requires the same logic as what I'm working on: Suppose that you have a directory containing files named after users. For awk's purposes, the filename is a single field-- something parse-friendly, like john_smith. Now, let's say that I'd like to populate an array in... (2 Replies)
Discussion started by: treesloth
2 Replies

8. Shell Programming and Scripting

To store output of ls command

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... (13 Replies)
Discussion started by: Sekar1
13 Replies

9. Shell Programming and Scripting

How to store contents of a command in array of variables in shell script?

I want to store contents of command dir in array of variables For eg: dir contents are command d2 demovi~ file inven java new untitled folder d1 demovi er1 filename inven~ myfiles ubuntu desktop xmms ----------------------------------- I... (3 Replies)
Discussion started by: netresearch
3 Replies

10. 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
Login or Register to Ask a Question