LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-13-2011, 11:55 AM   #1
kasparov29
LQ Newbie
 
Registered: Oct 2011
Posts: 22

Rep: Reputation: Disabled
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,
 
Old 11-13-2011, 03:45 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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

Last edited by David the H.; 11-13-2011 at 03:50 PM. Reason: as stated
 
1 members found this post helpful.
Old 11-13-2011, 05:50 PM   #3
kasparov29
LQ Newbie
 
Registered: Oct 2011
Posts: 22

Original Poster
Rep: Reputation: Disabled
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.
 
Old 11-14-2011, 10:16 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 11-14-2011, 02:26 PM   #5
kasparov29
LQ Newbie
 
Registered: Oct 2011
Posts: 22

Original Poster
Rep: Reputation: Disabled
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.

Last edited by kasparov29; 11-14-2011 at 02:30 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to store the output of command with pipes diwaker1984 Programming 3 01-20-2011 11:54 PM
Store multi-line output into an array in a Linux bash script steven.c.banks Linux - General 5 12-05-2010 02:08 PM
How can I receive command output and store it to a variable athlonshi Programming 5 04-04-2010 08:09 AM
Store multi-line output into an array in a Linux bash script steven.c.banks Linux - General 2 12-17-2009 09:10 AM
store output of cmd in array? h/w Programming 6 10-09-2003 08:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration