Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

simple bash stdin question

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
User avatar
utrrrongeeb
Posts: 254
Joined: 2006-05-14 09:48
Location: Eastern Canada

simple bash stdin question

#1 Post by utrrrongeeb »

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

ajdlinux
Posts: 2452
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

#2 Post by ajdlinux »

From a pipe is difficult. It is much easier in Python or similar than bash.

lacek
Posts: 764
Joined: 2004-03-11 18:49
Location: Budapest, Hungary
Contact:

#3 Post by lacek »

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.

User avatar
utrrrongeeb
Posts: 254
Joined: 2006-05-14 09:48
Location: Eastern Canada

#4 Post by utrrrongeeb »

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

ajdlinux
Posts: 2452
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

#5 Post by ajdlinux »

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'.

User avatar
utrrrongeeb
Posts: 254
Joined: 2006-05-14 09:48
Location: Eastern Canada

#6 Post by utrrrongeeb »

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

ajdlinux
Posts: 2452
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

#7 Post by ajdlinux »

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.

User avatar
utrrrongeeb
Posts: 254
Joined: 2006-05-14 09:48
Location: Eastern Canada

#8 Post by utrrrongeeb »

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: 254
Joined: 2006-05-14 09:48
Location: Eastern Canada

#9 Post by utrrrongeeb »

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

ajdlinux
Posts: 2452
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

#10 Post by ajdlinux »

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.

User avatar
utrrrongeeb
Posts: 254
Joined: 2006-05-14 09:48
Location: Eastern Canada

#11 Post by utrrrongeeb »

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: 254
Joined: 2006-05-14 09:48
Location: Eastern Canada

#12 Post by utrrrongeeb »

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: 254
Joined: 2006-05-14 09:48
Location: Eastern Canada

#13 Post by utrrrongeeb »

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

parasti

#14 Post by parasti »

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.)

User avatar
utrrrongeeb
Posts: 254
Joined: 2006-05-14 09:48
Location: Eastern Canada

#15 Post by utrrrongeeb »

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

Post Reply