Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a bash command has been executed successfully

Tags:

haskell

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?

like image 721
Zeta Avatar asked Feb 13 '12 17:02

Zeta


People also ask

How do I know if my previous command was successful?

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.

How do I know if bash is working?

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.

What is the command to check execution status?

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.


1 Answers

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
like image 122
dflemstr Avatar answered Oct 02 '22 19:10

dflemstr