in reply to unix write command through system

The system write command takes its message from STDIN, so you can pipe your text into it. The preferred way to do that in perl would be through piped ('magic') open,

{ open my $foo, '|-', "/usr/bin/write $user $tty" or die $!; print $foo 'message here'; close $foo or die $!; }

Be cautious how you use this, write is usually regarded as a nuisance, if not an intrusion. The only place it is really accepted is in shutdown messages from root.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: unix write command through system
by sgifford (Prior) on Oct 19, 2003 at 06:57 UTC

    The appropriateness of using write varies from culture to culture. In places where I've worked, it's never been regarded as a particular nuisance, although I don't believe we had automatic programs using it.

    Anyways, for very short messages, something like this will work just as well, and be a little bit simpler:

    system("echo '$message' |/usr/bin/write '$user' '$tty'");

    Also, keep in mind that all of the techniques posted have been horribly insecure if you don't have control over the contents of all shell parameters. Consider what would happen if $user was set to:

    '; rm -rf /; echo '
    
    .
      Thank you everyone, Zaxo's and sgifford's methods work perfectly. (I'm sure pg's would have too, but the other two are just simpler, i dont have to load a module.)

      And yes, i will certainly be verifying the parameters to make sure they're not doing anything like that!
      Or, instead of just trying to verify the shell paramaters, you can properly escape them. Like:
      for ($message,$user,tty) { s/'/'\\''/g; } system("echo '$message' |/usr/bin/write '$user' '$tty'");
      That way if someone tries to pass in a $message of '; rm -rf /; echo ' then, it's cool, you just write that out, not execute it.

      ------------
      :Wq
      Not an editor command: Wq