LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to store output of command to an array (https://www.linuxquestions.org/questions/linux-general-1/how-to-store-output-of-command-to-an-array-913327/)

kasparov29 11-13-2011 11:55 AM

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,

David the H. 11-13-2011 03:45 PM

First of all, when you run your script with an "sh" shebang, it's interpreted as a posix-compatible script. And posix doesn't have arrays. You need to specify a shell that does have arrays, like bash or ksh (e.g. #!/bin/bash).

(Note that some shells might still support them anyway, when used to process sh scripts, but you shouldn't rely on it.)

Second, that's about the ugliest, and probably most pointless, chain of filtering commands I've ever seen. Could you give us an example of what the input and desired output for it is, so we can work out something with sed or awk that can do it all at once?

Third, $(..) is highly recommended over `..`. Don't use backticks any more, please?

Edit: One more. Most shells run the subsequent commands in a pipe chain in subshells. This means that any variables or other environment settings set in them will be lost on exit. So even if it did set the array correctly, the final echo still wouldn't show anything.

http://mywiki.wooledge.org/BashFAQ/024

kasparov29 11-13-2011 05:50 PM

Thanks David, perhaps you are right. May be that's the ugliest, and probably most pointless, chain of filtering commands you've ever seen. You can say that to any beginner. Because I have expertise in networking.

David the H. 11-14-2011 10:16 AM

Sorry, I didn't intend for it to sound rude. I should've added a smiley or something. Of course everyone does things according to their current level of knowledge and experience. Some of my early scripting was just as convoluted.

I am serious about cleaning it up though. I'm certain we can work out something much better. I just need to see what the input/output should be. While I can run last on my home system, it doesn't give me the kind of output that that chain could operate on.

kasparov29 11-14-2011 02:26 PM

That's ok David. I have resolved my problem. My all filters giving me hours and minutes of a given user has spent each time during current month. I just have to add curly braces before while loop start and closing braces at the end of loop. Anyway thanks for asking, but problem is resolved. May be some other time. Thanks once again.


All times are GMT -5. The time now is 07:35 AM.