Retrieving output interactively


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Retrieving output interactively
# 1  
Old 03-21-2006
Retrieving output interactively

Hi. I have a situation where I need to constantly read a command's output in order to loop through it to determine something that is happening over our system. The command td <filename> interactively prints 'filename' as text is written to it and this is redirected to standard output. I have been trying to loop through this output interactively within a shell to catch any new messages to the files but without success. I have tried with:
Code:
#! /bin/ksh
while read line
do
        echo $line
done < td filename

exit 0

but it complains with
Code:
./test.sh[5]: syntax error at line 5 : `filename' unexpected

what I want to, is feed while with the output of the command interactively, which I believe will in turn run for ever (which I expect and want) thus achieving what I want. But as you can see, it doesn't work like this. I have also tried with for with not success.

If anyone could come with something similar to what I want, please advice. It will be very appreciated.

Thanks!
# 2  
Old 03-21-2006
give the output of td filename to while

as

`td filename`
# 3  
Old 03-21-2006
matrixmadhan, thanks for your prompt reply. I have done what you suggested, but apparently the shell hangs there and does nothing (no output or anything). I even tried redirecting echo's to a file and the file doesn't even gets created, which I believe that it hangs before it goes into the loop.
# 4  
Old 03-21-2006
Quote:
The command td <filename> interactively prints 'filename' as text is written to it and this is redirected to standard output.
then your process is not hanging, blocking on a read.

try executing
td filename
alone in the shell, then you would be clear with the input required and the output you target.
# 5  
Old 03-21-2006
Hi matrixmadhan. Thanks again. I dont understand at all your statemnet about 'blocking on a read', could you please ellaborate more on that ?

Here is an example of what I am trying to do.
I executed on a terminal
Code:
$ td test

See how it waits to show you there are no more 'trace to debug' on the file test. I then send a message from another terminal, that writes to this file.
Code:
$ echo "hello" > test
$ echo "world" > test

Then, in my first terminal I see
Code:
$ td test
hello
world

td just seem to do something like "I am watching this file for you, write to it, and I will show you what did you write". I am trying to do the same thing to catch any new message written to some file.
# 6  
Old 03-21-2006
I just see that td is just a wrapped tail -f under the covers. So, this actually make things easier, since I just want to, as new lines are added to this output, verify them to apply my criteria based on what I found on these. Still, if I do `tail -f file` while just seem to hang there.
# 7  
Old 03-21-2006
td filename | while read line ; do

has a fair shot at doing what you want. But it depends on how td is written. It may be line buffered only when writting to a tty device. When writing to a pipe, it may be fully buffered. Use "man setvbuf" if you don't understand buffering.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Command works interactively but not in bash script

The below command works in the terminal interactively but not as part of a bash script. I though maybe I needed to escape the "$dir" so it isn't interpreted literally, but that's not it. Thank you :). interactively in terminal dir=/path/to new=$(ls "$dir"/*.csv -tr | tail -n 1) && echo... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Traverse through list of servers using ssh non-interactively.

I have 300 servers listed in servers.txt I motto is to check if my id "user1" has sudo privileges on the 3000 servers. I m using sudo -l to check if I have privileges or not. If wish to check this either non-interactively; if not; interactively. Below is the script I wrote: ... (5 Replies)
Discussion started by: mohtashims
5 Replies

3. Solaris

Edit non-global zone IP address - non-interactively

Hi there I have a global zone with 178 zones on it :-) The entire box is moving to an entirely new network and I need to write a script that will configure the zones config file in /etc/zones for each of them changing the IP address - but non-interactively The line I need to change would... (5 Replies)
Discussion started by: hcclnoodles
5 Replies

4. Shell Programming and Scripting

Is it possible to create a here document by running a script interactively?

hi, i have script which installs around 20 packages. during installation of each package script will be prompted for user input. i need to run the script in non-interactive by using here document. is it possible to generate here document by running the script in interactive mode on Solaris?... (1 Reply)
Discussion started by: snreddy_gopu
1 Replies

5. HP-UX

how to run top or glance non-interactively using ssh

Hello Am trying to run top /glance non-interactively using ssh from redhat to hp-ux hosts but getting following errors.. ======================= ssh msi 'top -n 1' Sorry, I need to know a more specific terminal type than "unknown". ssh msi top -d1 -n20 > /tmp/top.out Sorry, I need... (3 Replies)
Discussion started by: delphys
3 Replies

6. Shell Programming and Scripting

input username and password interactively

Hi guys, I am using Solaris 10 and we install IBM OnDemand server there. there's a command called arsload that we use to load document and query document too. I need to write a script to query document but when querying document, this command prompt for username then password. My question is... (3 Replies)
Discussion started by: markthien
3 Replies

7. Shell Programming and Scripting

How to remove a cron job interactively?

My application has r.cron file which removes the cron job automatically during un-installation (pkgrm). But I want to take user input to decide on the deletion of cron job. I tried to put the code (to prompt user) in r.cron itself. But it is not waiting for user input. Where can I put that code? We... (0 Replies)
Discussion started by: durbam2002
0 Replies

8. UNIX for Advanced & Expert Users

Copy interactively - shell input

Hello, I am making a script to copy files interactively from one directory to another using the "i" option because I dont want to overwrite the files. cp -i *.html ./../otherdir/ cp: do you want to overwrite (y/n) ? I used cp -i *.html ./../otherdir/ ans='n' read $ans ... (4 Replies)
Discussion started by: telecomics
4 Replies

9. Shell Programming and Scripting

how can the search and replace can be done interactively

hi, To search and replace a string in multiple files i am using following command: find . -name '*.txt' -print0 |xargs -0 perl -pi -e 's/find_string/replace_string/g' I want to be prompted while replacing the string. how this can be done. thanks for every help. (2 Replies)
Discussion started by: aks__
2 Replies

10. UNIX for Dummies Questions & Answers

Zip and password protect non-interactively

I'm wondering if there is a way to zip a file and password protect it non-interactively. zip -e will prompt for a password but I don't want a prompt. This needs to be done automatically as part of a shell script. I'm using the zip command because the will be unzipped by a Windows machine. ... (1 Reply)
Discussion started by: savage66
1 Replies
Login or Register to Ask a Question