simple bash stdin question

Need help with C, C++, perl, python, etc?

simple bash stdin question

Postby utrrrongeeb » 2006-06-20 09:55

I'm new to shell scripts, and I haven't got around to reading about them, but I need to write a script for warning everyone about power failures from the UPS. (recently at http://forums.debian.net/viewtopic.php?t=6944 ) What I want to know how to do is reading a chunk of data from stdin from a pipe. It sounds like it might involve the read command, but I'm not sure exactly how it should be done. Please respond if you can tell me how to do this.
utrrrongeeb formerly lecaro
Art #429775 on 'Etch' 4.0r0
User avatar
utrrrongeeb
 
Posts: 255
Joined: 2006-05-14 09:48
Location: Eastern Canada

Postby ajdlinux » 2006-06-20 10:02

From a pipe is difficult. It is much easier in Python or similar than bash.
ajdlinux
 
Posts: 2480
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

Postby lacek » 2006-06-20 12:45

For a very stupid implementation, use this:
Code: Select all
while true; do
        read LINE || break
        echo "--> $LINE"; # Do your handling here
done

It will read lines from the standard input, and print the readed lines. You'll probably want to replace the echo line with your handling code. You cat pipe anything into this script.
lacek
Moderator Team Member
 
Posts: 769
Joined: 2004-03-11 18:49
Location: Budapest, Hungary

Postby utrrrongeeb » 2006-06-20 18:07

Thanks for responding; What C command can handle a chunk of stdin from a pipe? I know C slightly better than bash; my main language is Java, which is useless for shell scripts.
Also, what are the main flaws with the 'very stupid implementation'?
utrrrongeeb formerly lecaro
Art #429775 on 'Etch' 4.0r0
User avatar
utrrrongeeb
 
Posts: 255
Joined: 2006-05-14 09:48
Location: Eastern Canada

Postby ajdlinux » 2006-06-20 20:57

For C you simply open stdin as a file.

In Python:

import sys
stdin = sys.stdin
stdout = sys.stdout
stderr = sys.stderr

Then each is simply an open file.

In bash you have to use 'read'.
ajdlinux
 
Posts: 2480
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

Postby utrrrongeeb » 2006-06-21 09:56

Ok, thanks for posting! I don't know much Python, but I have Dive into Python on the Windows hard drive, so I'll read that a bit. How do you execute a shell command from a Python script?
utrrrongeeb formerly lecaro
Art #429775 on 'Etch' 4.0r0
User avatar
utrrrongeeb
 
Posts: 255
Joined: 2006-05-14 09:48
Location: Eastern Canada

Postby ajdlinux » 2006-06-21 10:05

import os # load the os module that gives all these cool functions, only do it once
os.system("command")

If you want to capture the output:

x = os.popen("command")
print x.read()
x.write("this will be sent to the program which i opened")
x.close() # an open process is simply an open file, you can read and write and close it like normal

As you can see there is a reason why I don't like doing complex shell scripts anymore.
ajdlinux
 
Posts: 2480
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

Postby utrrrongeeb » 2006-06-22 09:41

Ok, thanks for the answer! I'll have to do more skimming through Dive Into Python, as I don't know Python, but I should have something useable in a few days.
utrrrongeeb formerly lecaro
Art #429775 on 'Etch' 4.0r0
User avatar
utrrrongeeb
 
Posts: 255
Joined: 2006-05-14 09:48
Location: Eastern Canada

Postby utrrrongeeb » 2006-06-24 09:43

I'm changing problems now. The script I'm trying to write has the job of receiving stdin from a pipe, calling wall with that input, and then putting an alert on everyone's X desktop with the same message. I don't know Python well at all, but here's what I have at the moment:
Code: Select all
#!/usr/bin/python
import sys
import os

li = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,50]
msgstr = sys.stdin.read(1024)
os.system("echo \""+msgstr+"\" | wall")
for itr in li:
  os.system("zenity --display=:%d.0 --error --text=%s " % (itr,msgstr))


The reading part and the wall part work, but not the alert dialog part.
Could someone please explain what to do differently, if it is possible to do this?
utrrrongeeb formerly lecaro
Art #429775 on 'Etch' 4.0r0
User avatar
utrrrongeeb
 
Posts: 255
Joined: 2006-05-14 09:48
Location: Eastern Canada

Postby ajdlinux » 2006-06-24 10:25

Why would you have so many X desktops running? You can probe for running X instances by looking for the file /tmp/.X<n>-lock where <n> is the display number.
ajdlinux
 
Posts: 2480
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

Postby utrrrongeeb » 2006-06-24 17:04

Thanks for responding; I don't know Python well, and when I ran gdmflexiserver, the new desktop said it was :20.0, so I was trying not to miss any. There has to be some easier way to do this that I'm missing...
utrrrongeeb formerly lecaro
Art #429775 on 'Etch' 4.0r0
User avatar
utrrrongeeb
 
Posts: 255
Joined: 2006-05-14 09:48
Location: Eastern Canada

Postby utrrrongeeb » 2006-06-26 09:58

It turns out that running zenity --display=:0.0 --info --text="Test" from a tty doesn't work. How can I give an alert to everyone logged on to X?
utrrrongeeb formerly lecaro
Art #429775 on 'Etch' 4.0r0
User avatar
utrrrongeeb
 
Posts: 255
Joined: 2006-05-14 09:48
Location: Eastern Canada

Postby utrrrongeeb » 2006-06-28 17:58

I'm considering writing a program that runs when you login to X, that checks a file for modifications and alerts you if anything is added. However, any advice on better methods to accomplish the goal here (a method to alert everyone using X of an event from the command line), please post!
utrrrongeeb formerly lecaro
Art #429775 on 'Etch' 4.0r0
User avatar
utrrrongeeb
 
Posts: 255
Joined: 2006-05-14 09:48
Location: Eastern Canada

Postby parasti » 2006-06-28 18:19

lecaro wrote:I'm considering writing a program that runs when you login to X, that checks a file for modifications and alerts you if anything is added.

Take a look at xmessage (or gmessage for "funny characters") and its '-file' option. Not quite what you're looking for but a few lines of code could fix that. For some idea how to run it at log-on, check out the xsession examples from the debian-reference package. (Could be in /usr/share/doc/Debian/reference/examples/ if it's installed.)
parasti
 

Postby utrrrongeeb » 2006-06-28 22:55

Ok, it sounds like I didn't clarify it enough, so I'll elaborate:
I was thinking of writing a program that runs when you login to X. It runs in the background throughout your X session. Every second, it checks a text file for updates. If an update is present, it runs Zenity/dialog/xdialog/gdialog/cdialog/xmessage/gmessage with the recent appendations (probably not a word) to the file.
utrrrongeeb formerly lecaro
Art #429775 on 'Etch' 4.0r0
User avatar
utrrrongeeb
 
Posts: 255
Joined: 2006-05-14 09:48
Location: Eastern Canada


Return to Programming

Who is online

Users browsing this forum: No registered users and 4 guests

fashionable