Capture Output from an "Interactive" Command?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Capture Output from an "Interactive" Command?
# 1  
Old 10-10-2012
Capture Output from an "Interactive" Command?

Hello All,

I was wondering how to capture the output of an "interactive" command that is run...? I think "interactive" is the
word I'm looking for. For example, commands like "top" and "iftop"...

The One I'm trying to work with is "iftop". When you run "iftop" from the command line it begins a screen in the
terminal that shows the ethernet interfaces and their kbps, Tx, Rx, etc... I know "top" has an option that instead of top
updating the screen once every so many seconds it outputs one screen and the command exits.

I didn't see any Options like that for "iftop" though.

After you run the command, then hit Control-C/or 'Q' to exit the command it shows the following on the screen:
Code:
interface: eth0
IP address is: 192.168.5.101
MAC address is: 55:55:**:**:**:**

Is there anyway to capture that output (above)...?
I tried the usual way, and also tried redirecting stderr to stdout but it still didn't have anything in the variable after
it completed the command.

Here's what I tried that hasn't worked yet:
(tried each one below, and not all at the same time obviously...)
Code:
iftop_OUTPUT=$(iftop)
iftop_OUTPUT=$(iftop 2>&1)
echo -ne "$iftop_OUTPUT\n"

# Also tried printing to a file...
iftop &> /print/to/output/file/iftop_output.txt
iftop_OUTPUT=$(cat /print/to/output/file/iftop_output.txt)

I thought that since just setting the command to a variable, and since that variable was empty when I 'echoed' it, that it was
actually printing that to stderr but that hadn't worked either...

Anyone know if this is possible?
Any thoughts would be much appreciated!


Thanks in Advance,
Matt

---------- Post updated at 04:52 PM ---------- Previous update was at 04:38 PM ----------

**UPDATE: Smilie*Good News!!!
----------------------------------------------------------------------------------

So playing around a bit more with this I just tried using the "tee"command. This seemed to actually save some data from the command
but it mostly looked like jibberish...

Also, I couldn't just run the iftop command and either redirect or using tee and have the command just stop so I tried adding "pkill" in there
to stop the command right away since I didn't know the PID.

Here's the command I just tried:
Code:
iftop 2>&1 | tee ./myOutput.txt | pkill iftop

And here is the contents of "myOutput.txt" File:
Code:
interface: eth1
IP address is: 192.168.5.101
MAC address is: 55:**:**:**:**:**
^[[?1049h^[[1;24r^[(B^[[m^[[4l^[[?7h^[[?1h^[=

The Output is good enough for what I need to do. But I just wanted to ask and make sure that doing it this
way wouldn't run into a problem at anytime...?

Thanks,
Matt
# 2  
Old 10-10-2012
Look at the col command and this link shows how to pipe the tee output through sed to remove control codes:
exec - bash tee remove color - Stack Overflow
This User Gave Thanks to spacebar For This Post:
# 3  
Old 10-11-2012
Hey spacebar, thanks for the reply!

Cool thanks, I'll check that link out...
Luckily, I just recently had to do the same thing (i.e. removing the control codes), so I should be ok there...

Thanks Again,
Matt
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Redirecting output to new file for command "perldoc perllocal"

Hi, I have to redirect output of the command "perldoc perllocal" to new file which contains all the perl module installed. Currently using perldoc perllocal >> mod_data This does not contain all perl modules installed locally on machine, and each character is doubled. Please... (3 Replies)
Discussion started by: asak
3 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

Screen output is blocked by "| tee" command

BACK STORY: I have a script build.py . (It's for creating the ISO file for a special edition of Swift Linux.) This build.py script executes the mintConstructor.py script that I use to modify the Regular Swift Linux ISO to get the special edition Swift Linux ISO. The lines of the script that... (2 Replies)
Discussion started by: swiftlinux
2 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. UNIX for Dummies Questions & Answers

BASH: Interactive "cp" (and "mv") in a loop

Hi. I have a copy-file script (and a move-file script) that I recently tried to make interactive. I tested the former on three files from a text list, and watched to see what would happen. As the cp command was in a while/do/done loop, there was no pause for input: it wrote the file from the... (2 Replies)
Discussion started by: SilversleevesX
2 Replies

6. Shell Programming and Scripting

Retrieve RAM memory size from "top" command output

Hi, I am trying to get the system RAM size from "top" command's output by the following but it is not working. top | sed "s/^Mem.**\(*\), *//" (10 Replies)
Discussion started by: royalibrahim
10 Replies

7. Shell Programming and Scripting

"Join" or "Merge" more than 2 files into single output based on common key (column)

Hi All, I have working (Perl) code to combine 2 input files into a single output file using the join function that works to a point, but has the following limitations: 1. I am restrained to 2 input files only. 2. Only the "matched" fields are written out to the "matched" output file and... (1 Reply)
Discussion started by: Katabatic
1 Replies

8. UNIX for Dummies Questions & Answers

Explanation of "total" field in "ls -l" command output

When I do a listing in one particular directory (ls -al) I get: total 43456 drwxrwxrwx 2 root root 4096 drwxrwxrwx 3 root root 4096 -rwxrwxr-x 1 nobody nobody 3701594 -rwxrwxr-x 1 nobody nobody 3108510 -rwxrwxr-x 1 nobody nobody 3070580 -rwxrwxr-x 1 nobody nobody 3099733 -rwxrwxr-x 1... (1 Reply)
Discussion started by: proactiveaditya
1 Replies

9. UNIX for Dummies Questions & Answers

Interactive "Enter" command

Good morning I've searched the Forum but couldn't find what I'm searching for. I'm executing a, for example pepe.sh within a script (hello.sh). pepe.sh executes and expects an "enter". How could i told it within hello.sh? Thanks in advance for your help. Regards (1 Reply)
Discussion started by: Felix2511
1 Replies

10. Debian

Debian: doubt in "top" %CPU and "sar" output

Hi All, I am running my application on a dual cpu debian linux 3.0 (2.4.19 kernel). For my application: <sar -U ALL> CPU %user %nice %system %idle ... 10:58:04 0 153.10 0.00 38.76 0.00 10:58:04 1 3.88 0.00 4.26 ... (0 Replies)
Discussion started by: jaduks
0 Replies
Login or Register to Ask a Question