I'm trying to make a little program in Haskell. What I need to do is to check if a bash command has been executed successfully by the Haskell interpreter. Let's say in "pseudocode":
$import System
$if( system "ls" ) has been succesfully run
$then doStuff
How would you write this piece of code in Haskell?
variable in Linux. “$?” is a variable that holds the return value of the last executed command. “echo $?” displays 0 if the last command has been successfully executed and displays a non-zero value if some error has occurred.
Get the version of bash I am running, type: echo "${BASH_VERSION}" Check my bash version on Linux by running: bash --version. To display bash shell version press Ctrl + x Ctrl + v.
Checking Exit Status of Command command to get the status of executed command. for exmaple, if you have executed one command called “ df -h “, then you want to get the exit status of this command, just type the following command: $ echo $? From the above outputs, you can see that a number 0 is returned.
You can do this:
import System
main = do
result <- system "ls"
case result of
ExitSuccess ->
putStrLn "Ran successfully"
ExitFailure code ->
putStrLn $ "Failed with exit code " ++ show code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With