forked from caiorss/Functional-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem_io_modules.org
More file actions
839 lines (618 loc) · 18 KB
/
Copy pathsystem_io_modules.org
File metadata and controls
839 lines (618 loc) · 18 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
#+INCLUDE: ../theme/style.org
#+TITLE: System IO Modules
* System IO Modules
** Overview
** System.IO
*** Overview
*System.IO*
Standard File Handlers:
- stdin
- stdout
- stderr
IOMode
- ReadMode
- WriteMode
- AppendMode
- ReadWriteMode
*** Read and Write File
#+BEGIN_SRC haskell
> :t putStrLn
putStrLn :: String -> IO ()
>
-- Bind operator
--
> :t (>>=)
(>>=) :: Monad m => m a -> (a -> m b) -> m b
>
-- Reverse bind operator
--
--
> :t (=<<)
(=<<) :: Monad m => (a -> m b) -> m a -> m b
>
> :t readFile "/etc/lsb-release"
readFile "/etc/lsb-release" :: IO String
>
> readFile "/etc/lsb-release"
"DISTRIB_ID=Ubuntu\nDISTRIB_RELEASE=15.04 ..."
> readFile "/etc/lsb-release" >>= putStrLn
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=15.04
DISTRIB_CODENAME=vivid
DISTRIB_DESCRIPTION="Ubuntu 15.04"
> putStrLn =<< readFile "/etc/lsb-release"
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=15.04
DISTRIB_CODENAME=vivid
DISTRIB_DESCRIPTION="Ubuntu 15.04"
-- This code must be loaded from a file:
--
showFile fileName = do
content <- readFile fileName
putStrLn content
-- This code can be pasted in the repl.
--
> let showFile3 fileName = do { content <- readFile fileName ; putStrLn content }
>
> :t showFile3
showFile3 :: FilePath -> IO ()
> showFile "/etc/lsb-release"
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=15.04
DISTRIB_CODENAME=vivid
DISTRIB_DESCRIPTION="Ubuntu 15.04"
-- Dessugarized.
--
> let showFile2 fileName = readFile fileName >>= \content -> putStrLn content
>
> showFile2 "/etc/lsb-release"
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=15.04
DISTRIB_CODENAME=vivid
DISTRIB_DESCRIPTION="Ubuntu 15.04"
#+END_SRC
*** Explore System.IO
#+BEGIN_SRC haskell
> import qualified System.IO as SIO
>
-- Type tab to list the module
--
> SIO.
>
Display all 106 possibilities? (y or n)
SIO.AbsoluteSeek SIO.hPutChar
SIO.AppendMode SIO.hPutStr
SIO.BlockBuffering SIO.hPutStrLn
SIO.BufferMode SIO.hReady
SIO.CRLF SIO.hSeek
> :t SIO.readFile
SIO.readFile :: FilePath -> IO String
>
-- File handler / File descriptor (Unix File descriptor)
--
> let fd = SIO.openFile "/etc/lsb-release" SIO.ReadMode
> :t SIO.openFile
SIO.openFile :: FilePath -> SIO.IOMode -> IO SIO.Handle
>
> fd
{handle: /etc/lsb-release}
> :t fd
fd :: IO SIO.Handle
>
-- Return a line from file descritor
--
> :t SIO.hGetLine
SIO.hGetLine :: SIO.Handle -> IO String
>
> fd >>= SIO.hGetLine
"DISTRIB_ID=Ubuntu"
> fd >>= SIO.hGetLine
"DISTRIB_ID=Ubuntu"
>
#+END_SRC
*** Read three Lines from a file.
#+BEGIN_SRC haskell
import qualified System.IO as SIO
readThreeLines fileName = do
fd <- SIO.openFile fileName SIO.ReadMode
line1 <- SIO.hGetLine fd
line2 <- SIO.hGetLine fd
line3 <- SIO.hGetLine fd
SIO.hClose fd
return [line1, line2, line3]
-- --------------------------------
- ---------------------------------
> :paste
> readThreeLines "/etc/lsb-release"
["DISTRIB_ID=Ubuntu","DISTRIB_RELEASE=15.04","DISTRIB_CODENAME=vivid"]
>
> :t readThreeLines "/etc/lsb-release"
readThreeLines "/etc/lsb-release" :: IO [String]
#+END_SRC
*** Test the module System.IO
#+BEGIN_SRC haskell
import qualified System.IO as SIO
> let fd = SIO.openFile "/etc/lsb-release" SIO.ReadMode
> :t fd
fd :: IO SIO.Handle
>
> fd >>= SIO.hGetContents
"DISTRIB_ID=Ubuntu\nDISTRIB_RELEASE=15.04\nDISTRIB_CODENAME ... ... "
>
> fd >>= SIO.hGetContents >>= putStrLn
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=15.04
DISTRIB_CODENAME=vivid
DISTRIB_DESCRIPTION="Ubuntu 15.04"
let readLine fd = SIO.hGetLine fd >>= \line -> return (line, fd)
> :t readLine
readLine :: SIO.Handle -> IO (String, SIO.Handle)
>
> fd >>= readLine
("DISTRIB_ID=Ubuntu",{handle: /etc/lsb-release})
>
> fd >>= readLine
("DISTRIB_ID=Ubuntu",{handle: /etc/lsb-release})
>
> let readTwoLines fd = readLine fd >>= \(line1, fd) -> readLine fd >>= \(line2, fd) -> return ([line1, line2], fd)
> fd >>= readTwoLines
(["DISTRIB_ID=Ubuntu","DISTRIB_RELEASE=15.04"],{handle: /etc/lsb-release})
>
#+END_SRC
*** Read all lines of a file
#+BEGIN_SRC haskell
import qualified System.IO as SIO
fdtest = SIO.openFile "/etc/lsb-release" SIO.ReadMode
readLineState :: SIO.Handle -> IO (String, SIO.Handle)
readLineState fd = do
line <- SIO.hGetLine fd
return (line, fd)
readLinesAux :: SIO.Handle -> [String] -> IO [String]
readLinesAux fd lines = do
(lin, fdnext) <- readLineState fd
isEOF <- SIO.hIsEOF fdnext
if isEOF
then return (reverse lines)
else readLinesAux fdnext (lin:lines)
readLines :: SIO.Handle -> IO [String]
readLines fd = readLinesAux fd []
readLinesFromFile :: String -> IO [String]
readLinesFromFile fileName = do
handle <- SIO.openFile fileName SIO.ReadMode
lines <- readLines handle
SIO.hClose handle
return lines
makeReader :: (SIO.Handle -> IO out) -> (SIO.Handle -> IO (out, SIO.Handle))
makeReader reader fd = do
out <- reader fd
return (out, fd)
applyFileReaderAux :: (SIO.Handle -> IO (out, SIO.Handle)) -> SIO.Handle -> [out] -> IO [out]
applyFileReaderAux fileReader fd acc = do
(output, fdnext) <- fileReader fd
isEOF <- SIO.hIsEOF fdnext
if isEOF
then return (reverse acc)
else applyFileReaderAux fileReader fdnext (output:acc)
applyFileReader :: (SIO.Handle -> IO (out, SIO.Handle)) -> SIO.Handle -> IO [out]
applyFileReader fileReader fd = applyFileReaderAux fileReader fd []
#+END_SRC
Repl tests:
#+BEGIN_SRC haskell
> :t fdtest
fdtest :: IO SIO.Handle
>
> :t readLineState
readLineState :: SIO.Handle -> IO (String, SIO.Handle)
>
> fdtest >>= readLineState
("DISTRIB_ID=Ubuntu",{handle: /etc/lsb-release})
>
> readLineState =<< fdtest
("DISTRIB_ID=Ubuntu",{handle: /etc/lsb-release})
>
> fdtest >>= \fd -> readLinesAux fd []
["DISTRIB_ID=Ubuntu","DISTRIB_RELEASE=15.04","DISTRIB_CODENAME=vivid"]
>
> :t fdtest >>= \fd -> readLinesAux fd []
fdtest >>= \fd -> readLinesAux fd [] :: IO [String]
>
> fdtest >>= readLines
["DISTRIB_ID=Ubuntu","DISTRIB_RELEASE=15.04","DISTRIB_CODENAME=vivid"]
>
> readLinesFromFile "/etc/lsb-release"
["DISTRIB_ID=Ubuntu","DISTRIB_RELEASE=15.04","DISTRIB_CODENAME=vivid"]
>
> :t SIO.withFile
SIO.withFile
:: FilePath -> SIO.IOMode -> (SIO.Handle -> IO r) -> IO r
>
> SIO.withFile "/etc/lsb-release" SIO.ReadMode readLines
["DISTRIB_ID=Ubuntu","DISTRIB_RELEASE=15.04","DISTRIB_CODENAME=vivid"]
>
> :t SIO.withFile "/etc/lsb-release" SIO.ReadMode readLines
SIO.withFile "/etc/lsb-release" SIO.ReadMode readLines
:: IO [String]
-- ------------------------
>
> fd <- SIO.openFile "/etc/lsb-release" SIO.ReadMode
> :t fd
fd :: SIO.Handle
>
> fd
{handle: /etc/lsb-release}
>
> :t readLineState
readLineState :: SIO.Handle -> IO (String, SIO.Handle)
> applyFileReaderAux readLineState fd []
["DISTRIB_ID=Ubuntu","DISTRIB_RELEASE=15.04","DISTRIB_CODENAME=vivid"]
>
> let charReader = makeReader SIO.hGetChar
> :t charReader
charReader :: SIO.Handle -> IO (Char, SIO.Handle)
>
> applyFileReader charReader fd
"DISTRIB_ID=Ubuntu\nDISTRIB_RELEASE=15.04\nDISTRIB_CODENAME= ..."
>
> fd <- SIO.openFile "/etc/lsb-release" SIO.ReadMode
> applyFileReader (makeReader SIO.hGetLine) fd
["DISTRIB_ID=Ubuntu","DISTRIB_RELEASE=15.04","DISTRIB_CODENAME=vivid"]
>
> applyFileReader (makeReader SIO.hGetLine) fd
*** Exception: /etc/lsb-release: hGetLine: end of file
>
#+END_SRC
** System.Environment
[[https://hackage.haskell.org/package/base-4.8.2.0/docs/System-Environment.html][Module Documentation]]
Miscellaneous information about the system environment.
#+BEGIN_SRC haskell
> import qualified System.Environment as E
>
-- returns a list of the program's command line arguments
-- (not including the program name).
--
> E.getArgs
[]
> :t E.getArgs
E.getArgs :: IO [String]
>
-- Computation getProgName returns the name of the program as it was invoked.
--
> :t E.getProgName
E.getProgName :: IO String
>
> E.getProgName
"<interactive>"
>
-- Returns the absolute pathname of the current executable.
-- Note that for scripts and interactive sessions, this is
-- the path to the interpreter (e.g. ghci.)
> :t E.getExecutablePath
E.getExecutablePath :: IO FilePath
>
> E.getExecutablePath
"/usr/lib/ghc/lib/ghc"
>
> path <- E.getExecutablePath
> :t path
path :: FilePath
> path
"/usr/lib/ghc/lib/ghc"
>
-- Computation getEnv var returns the value of the environment
-- variable var. For the inverse, POSIX users can use putEnv.
--
--
> :t E.getEnv
E.getEnv :: String -> IO String
>
> E.getEnv "HOME"
"/home/tux"
>
> E.getEnv "PATH"
"/home/tux/.cask/bin:/opt/bin:/home/tux/.opam/4.02.1/bin:/home/tux/bin ..."
> E.getEnv "DONTEXISTS"
*** Exception: DONTEXISTS: getEnv: does not exist (no environment variable)
>
-- Return the value of the environment variable var,
-- or Nothing if there is no such value.
--
> :t E.lookupEnv
E.lookupEnv :: String -> IO (Maybe String)
>
> E.lookupEnv "JDK_HOME"
Just "/opt/java"
>
> E.lookupEnv "JDK_HOMEasdasd"
Nothing
>
-- getEnvironment retrieves the entire environment as a list of (key,value) pairs.
> :t E.getEnvironment
E.getEnvironment :: IO [(String, String)]
>
> E.getEnvironment >>= \lst -> return (take 10 lst)
[("USER","tux"),("LANGUAGE","en_US"),("LC_TIME","pt_BR.UTF-8"),("COMP_WORDBREAKS"," \t\n\"'><;|&(:"),("XDG_SEAT","seat0"),("SSH_AGENT_PID","15614"),("XDG_SESSION_TYPE","x11"),("SHLVL","1"),("HOME","/home/tux"),("QT4_IM_MODULE","xim")]
>
> :t mapM_
mapM_ :: Monad m => (a -> m b) -> [a] -> m ()
>
> :t E.getEnvironment >>= \rows -> mapM_ (\row -> putStrLn (show row)) rows
E.getEnvironment >>= \rows -> mapM_ (\row -> putStrLn (show row)) rows
:: IO ()
>
> E.getEnvironment >>= \rows -> mapM_ (\row -> putStrLn (show row)) rows
("USER","tux")
("LANGUAGE","en_US")
("LC_TIME","pt_BR.UTF-8")
("XDG_SEAT","seat0")
("SSH_AGENT_PID","15614")
("XDG_SESSION_TYPE","x11")
("SHLVL","1")
...
#+END_SRC
** System.Directory
- [[https://hackage.haskell.org/package/directory-1.2.5.1/docs/System-Directory.html][Module Documentation]]
#+BEGIN_SRC haskell
> import qualified System.Directory as D
>
-- createDirectory dir creates a new directory dir which is initially
-- empty, or as near to empty as the operating system allows.
> D.createDirectory "/tmp/testdir"
> D.createDirectory "/tmp/testdir"
*** Exception: /tmp/testdir: createDirectory: already exists (File exists)
>
{-
createDirectoryIfMissing Source
:: Bool Create its parents too?
-> FilePath The path to the directory you want to make
-> IO ()
-}
> D.createDirectoryIfMissing True "/tmp/tree1/1/2/3/4"
>
> D.createDirectoryIfMissing True "/tmp/tree1/1/2/3/4"
>
> D.createDirectoryIfMissing True "/tree1/1/2/3/4"
*** Exception: /tree1: createDirectory: permission denied (Permission denied)
>
-- removeDirectory dir removes an existing directory dir.
> D.removeDirectory "/tmp/testdir"
> D.removeDirectory "/tmp/testdir"
*** Exception: /tmp/testdir: removeDirectory: does not exist (No such file or directory)
>
-- Similar to listDirectory, but always includes the special entries
-- (. and ..). (This applies to Windows as well.)
> :t D.getDirectoryContents "/boot"
D.getDirectoryContents "/boot" :: IO [FilePath]
>
> fmap (take 2) (D.getDirectoryContents "/boot" )
["initrd.img-3.19.0-37-generic","config-3.19.0-21-generic"]
>
> take 2 <$> D.getDirectoryContents "/boot"
["initrd.img-3.19.0-37-generic","config-3.19.0-21-generic"]
>
> D.getDirectoryContents "/boot" >>= mapM_ putStrLn
initrd.img-3.19.0-37-generic
config-3.19.0-21-generic
System.map-3.19.0-39-generic
abi-3.19.0-18-generic
...
> D.getDirectoryContents "/boot" >>= \rows -> return (take 10 rows) >>= mapM_ putStrLn
initrd.img-3.19.0-37-generic
config-3.19.0-21-generic
System.map-3.19.0-39-generic
...
>
> let displayDir path = D.getDirectoryContents path >>= \rows -> return (take 10 rows) >>= mapM_ putStrLn
>
> displayDir "/boot"
initrd.img-3.19.0-37-generic
config-3.19.0-21-generic
System.map-3.19.0-39-generic
...
-- Obtain the current working directory as an absolute path.
--
> :t D.getCurrentDirectory
D.getCurrentDirectory :: IO FilePath
>
> D.getCurrentDirectory
"/home/tux"
>
-- Change the working directory to the given path.
--
> :t D.setCurrentDirectory
D.setCurrentDirectory :: FilePath -> IO ()
>
-- Returns the current user's home direct
--
> D.getHomeDirectory
"/home/tux"
>
> D.getUserDocumentsDirectory
"/home/tux"
>
> D.getTemporaryDirectory
"/tmp"
>
-- copyFile old new copies the existing file from old to new. If the
-- new file already exists, it is atomically replaced by the old
-- file. Neither path may refer to an existing directory. The
-- permissions of old are copied to new, if possible.
> :t D.copyFile
D.copyFile :: FilePath -> FilePath -> IO ()
>
>
> D.copyFile "/etc/lsb-release" "/tmp/test.txt"
>
> readFile "/tmp/test.txt" >>= putStrLn
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=15.04
DISTRIB_CODENAME=vivid
DISTRIB_DESCRIPTION="Ubuntu 15.04"
{-
removeFile file removes the directory entry for an existing file file,
where file is not itself a directory. The implementation may specify
additional constraints which must be satisfied before a file can be
removed (e.g. the file may not be in use by other processes).
-}
> :t D.removeFile
D.removeFile :: FilePath -> IO ()
>
> D.removeFile "/tmp/test.txt"
> D.removeFile "/tmp/test.txt"
*** Exception: /tmp/test.txt: removeLink: does not exist (No such file or directory)
>
{-
Given an executable file name, searches for such file in the
directories listed in system PATH. The returned value is the path to
the found executable or Nothing if an executable with the given name
was not found. For example (findExecutable "ghc") gives you the path
to GHC.
-}
> D.findExecutable "java"
Just "/opt/bin/java"
>
> D.findExecutable "javac"
Just "/opt/java/bin/javac"
> D.findExecutable "javaasdas"
Nothing
>
{-
Given a file name, searches for the file and returns a list of all
occurences that are executable.
-}
> D.findExecutables "java"
["/opt/bin/java","/opt/bin/java","/usr/bin/java","/opt/java/bin/java"]
>
> D.findExecutables "ghci"
["/usr/bin/ghci"]
>
{-
Given a file name, searches for the file on the given paths and
returns a list of all occurences that are executable.
-}
> :t D.findFile
D.findFile :: [FilePath] -> String -> IO (Maybe FilePath)
>
> D.findFiles ["/opt/bin", "/usr/bin"] "java"
["/opt/bin/java","/usr/bin/java"]
>
> D.findFiles ["/opt/bin", "/usr/bin"] "jasadva"
[]
-- The operation doesFileExist returns True if the argument file
-- exists and is not a directory, and False otherwise.
> D.doesFileExist "/etc/fstab"
True
>
> D.doesFileExist "/etc/fstaASDb"
False
>
{-
The operation doesDirectoryExist returns True if the argument file
exists and is either a directory or a symbolic link to a directory,
and False otherwise.
-}
>
> D.doesDirectoryExist "/etc"
True
> D.doesDirectoryExist "/etc/Dummy"
False
>
-- The getPermissions operation returns the permissions for the file or directory.
--
> D.getPermissions "/etc/fstab"
Permissions {readable = True, writable = False, executable = False, searchable = False}
>
#+END_SRC
** System.Info
- [[https://hackage.haskell.org/package/base-4.8.2.0/docs/System-Info.html][Module Documentation]]
Information about the characteristics of the host system lucky enough
to run your program.
#+BEGIN_SRC haskell
> import qualified System.Info as I
>
> I.os
"linux"
>
> I.arch
"i386"
>
> I.compilerName
"ghc"
>
> I.compilerVersion
Version {versionBranch = [7,6], versionTags = []}
>
> :t I.compilerVersion
I.compilerVersion :: Data.Version.Version
>
#+END_SRC
** System.Process
- [[https://hackage.haskell.org/package/process-1.4.2.0/docs/System-Process.html][Module Documentation]]
#+BEGIN_SRC haskell
> import qualified System.Process as P
>
{-
Creates a new process to run the specified command with the given
arguments, and wait for it to finish. If the command returns a
non-zero exit code, an exception is raised.
-}
> P.callProcess "date" []
Ter Fev 23 12:29:42 BRT 2016
> :t P.callProcess "date" []
P.callProcess "date" [] :: IO ()
>
> P.callProcess "dadsfte" []
,*** Exception: dadsfte: callProcess: runInteractiveProcess: exec: does not exist (No such file or directory)
>
> P.callProcess "uname" ["-a"]
Linux tuxhorse 3.19.0-39-generic #44-Ubuntu SMP Tue Dec 1 14:38:23 UTC 2015 i686 i686 i686 GNU/Linux
>
{-
Creates a new process to run the specified shell command. If the
command returns a non-zero exit code, an exception is raised.
-}
> P.callCommand "uname -a"
Linux tuxhorse 3.19.0-39-generic #44-Ubuntu SMP Tue Dec 1 14:38:23 UTC 2015 i686 i686 i686 GNU/Linux
>
> :t P.callCommand
P.callCommand :: String -> IO ()
>
{-
readProcess forks an external process, reads its standard output
strictly, blocking until the process terminates, and returns the
output string. The external process inherits the standard error.
readProcess Source
:: FilePath Filename of the executable (see RawCommand for details)
-> [String] any arguments
-> String standard input
-> IO String stdout
-}
> :t P.readProcess
P.readProcess :: FilePath -> [String] -> String -> IO String
>
> P.readProcess "uname" ["-r"] ""
"3.19.0-39-generic\n"
>
> kernelVersion <- P.readProcess "uname" ["-r"] ""
> kernelVersion
"3.19.0-39-generic\n"
> :t kernelVersion
kernelVersion :: String
>
{-
readProcessWithExitCode Source
:: FilePath Filename of the executable (see RawCommand for details)
-> [String] any arguments
-> String standard input
-> IO (ExitCode, String, String) exitcode, stdout, stderr
-}
>
> P.readProcessWithExitCode "uname" ["-a"] ""
(ExitSuccess,"Linux tuxhorse 3.19.0-39-generic #44-Ubuntu SMP Tue Dec 1 14:38:23 UTC 2015 i686 i686 i686 GNU/Linux\n","")
>
> P.readProcessWithExitCode "uname" ["-x"] ""
(ExitFailure 1,"","uname: invalid option -- 'x'\nTry 'uname --help' for more information.\n")
>
> P.readProcessWithExitCode "unasdame" ["-x"] ""
*** Exception: unasdame: readCreateProcessWithExitCode: runInteractiveProcess: exec: does not exist (No such file or directory)
>
#+END_SRC