radrow
radrow

Reputation: 7139

Execute readProcess by giving full command

readProcess in Haskell takes command name and its args separately. I'd like to execute command as I have typed it exactly into shell, eg. "ls -la". The main problem is that I have no idea how to execute command with some pipes etc, so

./someprogram -s 2> /dev/stdin | grep string

seems impossible. I cannot simply pass it into command name, because it throws exception then. So how can I do it?

Upvotes: 0

Views: 94

Answers (1)

leftaroundabout
leftaroundabout

Reputation: 120711

These are not features you can generally expect the OS or a process library to offer you. They're features of the shell language (i.e. sh/dash/bash/zsh etc., whatever you use). Thus you have to call the shell as the process, with the entire command as the argument:

Prelude System.Process> readProcess "bash" ["-c", "lsusb | grep 'Real'"] ""
"Bus 001 Device 003: ID 0bda:57b5 Realtek Semiconductor Corp. \n"

Of course this won't work on a system that doesn't have Bash installed, like Windows.

A nicer approach would be to not invoke any shell pipes and grep, but do the searching in Haskell itself. The turtle library makes this easy:

Prelude Turtle> :set -XOverloadedStrings
Prelude Turtle> stdout . grep (has "Real") $ inproc "lsusb" [] empty
Bus 001 Device 003: ID 0bda:57b5 Realtek Semiconductor Corp. 

Upvotes: 3

Related Questions