hsshellscript-3.1.0: Haskell for Unix shell scripting tasks

Safe HaskellSafe-Infered

HsShellScript.Misc

Synopsis

Documentation

zerosSource

Arguments

:: Int

How many characters to fill up

-> Int

Value to represent as a string

-> String

String representation of the value, using the specified number of characters

Format an Int with leading zeros. If the string representation of the Inŧ is longer than the number of characters to fill up, this produces as many characters as needed.

chompSource

Arguments

:: String

String to be chomped

-> String

Same string, except for no newline characters at the end

Remove trailing newlines. This is silimar to perl's chomp procedure.

lazy_contentsSource

Arguments

:: String

Either the name of a file, or "-"

-> IO String

The lazily read contents of the file or stdin.

Get contents of a file or of stdin. This is a simple frontend to hGetContents. A file name of "-" designates stdin. The contents are read lazily as the string is evaluated.

(The handle which we read from will be in semi-closed state. Once all input has read, it is closed automatically (Haskell Library Report 11.2.1). Therefore we don't need to return it).

lazy_contents path = do
    h   <- if path == "-" then return stdin else openFile path ReadMode
    hGetContents h

contentsSource

Arguments

:: String

either the name of a file, or "-" for stdin

-> IO String

the contents of the file or of standard input

Get contents of a file or of stdin eagerly. This is the same as lazy_contents, except for the contents being read immediately.

path_existsSource

Arguments

:: String

Path

-> IO Bool

Whether the path exists in the file system

Test for the existence of a path. This is the disjunction of Directory.doesDirectoryExist and Directory.doesFileExist. For an dangling symlink, this will return False.

path_exists'Source

Arguments

:: String

Path

-> IO Bool

Whether the path exists in the file system

Test for the existence of a path. This uses System.Posix.Files.getFileStatus to determine whether the path exists in any form in the file system. For a dangling symlink, the result is True.

is_dirSource

Arguments

:: String

Path

-> IO Bool

Whether the path exists and points to a directory.

Test if path points to a directory. This will return True for a symlink pointing to a directory. It's a shortcut for Directory.doesDirectoryExist.

is_fileSource

Arguments

:: String

Path

-> IO Bool

Whether the path exists and points to a file.

Test if path points to a file. This is a shortcut for Directory.doesFileExist.

getFileStatus'Source

Arguments

:: FilePath

Path of the file, whose status is to be queried

-> IO FileStatus

Status of the file

This is the System.Posix.Files.getFileStatus function from the GHC libraries, with improved error reporting. The GHC function doesn't include the file name in the IOError when the call fails, making error messages much less useful. getFileStatus' rectifies this.

See getFileStatus.

fileAccess' :: FilePath -> Bool -> Bool -> Bool -> IO BoolSource

This is the System.Posix.Files.fileAccess function from the GHC libraries, with improved error reporting. The GHC function doesn't include the file name in the IOError when the call fails, making error messages much less useful. fileAccess' rectifies this.

See fileAccess.

temp_fileSource

Arguments

:: Int

Number of random characters to intersperse. Must be large enough, such that most combinations can't already exist.

-> String

Prefix for the path to generate.

-> String

Suffix for the path to generate.

-> IO FilePath

Path of the created file.

Create a temporary file. This will create a new, empty file, with a path which did not previously exist in the file system. The path consists of the specified prefix, a sequence of random characters (digits and letters), and the specified suffix. The file is created with read-write permissions for the user, and no permissons for the group and others. The ownership is set to the effective user ID of the process. The group ownership is set either to the effective group ID of the process or to the group ID of the parent directory (depending on filesystem type and mount options on Linux - see open(2) for details).

See tmp_file, temp_dir, with_temp_file.

temp_dirSource

Arguments

:: Int

Number of random characters to intersperse. Must be large enough, such that most combinations can't already exist.

-> String

Prefix for the path to generate.

-> String

Suffix for the path to generate.

-> IO FilePath

Generated path.

Create a temporary directory. This will create a new directory, with a path which did not previously exist in the file system. The path consists of the specified prefix, a sequence of random characters (digits and letters), and the specified suffix. The directory is normally created with read-write-execute permissions for the user, and no permissons for the group and others. But this may be further restricted by the process's umask in the usual way.

The newly created directory will be owned by the effective uid of the process. If the directory containing the it has the set group id bit set, or if the filesystem is mounted with BSD group semantics, the new directory will inherit the group ownership from its parent; otherwise it will be owned by the effective gid of the process. (See mkdir(2))

See tmp_dir, temp_file, with_temp_dir.

tmp_fileSource

Arguments

:: String

Prefix for the path to generate.

-> IO FilePath

Path of the created file.

Create a temporary file. This will create a new, empty file, with read-write permissions for the user, and no permissons for the group and others. The path consists of the specified prefix, a dot, and six random characters (digits and letters).

tmp_file prefix = temp_file 6 (prefix ++ ".") ""

See temp_file, tmp_dir, with_tmp_file.

tmp_dirSource

Arguments

:: String

Prefix for the path to generate.

-> IO FilePath

Path of the created directory.

Create a temporary directory. This will create a new directory, with read-write-execute permissions for the user (unless further restricted by the process's umask), and no permissons for the group and others. The path consists of the specified prefix, a dot, and six random characters (digits and letters).

tmp_dir prefix = temp_dir 6 (prefix ++ ".") ""

See temp_dir, tmp_file, with_tmp_dir.

with_temp_fileSource

Arguments

:: Int

Number of random characters to intersperse. Must be large enough, such that most combinations can't already exist.

-> String

Prefix for the path to generate.

-> String

Suffix for the path to generate.

-> (Handle -> IO a)

Action to perform.

-> IO a

Returns the value returned by the action.

Create and open a temporary file, perform some action with it, and delete it afterwards. This is a front end to the temp_file function. The file and its path are created in the same way. The IO action is passed a handle of the new file. When it finishes - normally or with an exception - the file is deleted.

See temp_file, with_tmp_file, with_temp_dir.

with_temp_dirSource

Arguments

:: Int

Number of random characters to intersperse. Must be large enough, such that most combinations can't already exist.

-> String

Prefix for the path to generate.

-> String

Suffix for the path to generate.

-> (FilePath -> IO a)

Action to perform.

-> IO a

Returns the value returned by the action.

Create a temporary directory, perform some action with it, and delete it afterwards. This is a front end to the temp_dir function. The directory and its path are created in the same way. The IO action is passed the path of the new directory. When it finishes - normally or with an exception - the directory is deleted.

The action must clean up any files it creates inside the directory by itself. with_temp_dir doesn't delete any files inside, so the directory could be removed. If the directory isn't empty, an IOError results (with the path filled in). When the action throws an exception, and the temporary directory cannot be removed, then the exception is passed through, rather than replacing it with the IOError. (This is because it's probably exactly because of that exception that the directory isn't empty and can't be removed).

See temp_dir, with_tmp_dir, with_temp_file.

with_tmp_fileSource

Arguments

:: String

Prefix for the path to generate.

-> (Handle -> IO a)

Action to perform.

-> IO a

Returns the value returned by the action.

Create and open a temporary file, perform some action with it, and delete it afterwards. This is a front end to the tmp_file function. The file and its path are created in the same way. The IO action is passed a handle of the new file. When it finishes - normally or with an exception - the file is deleted.

See tmp_file, with_temp_file, with_tmp_dir.

with_tmp_dirSource

Arguments

:: String

Prefix for the path to generate.

-> (FilePath -> IO a)

Action to perform.

-> IO a

Returns the value returned by the action.

Create a temporary directory, perform some action with it, and delete it afterwards. This is a front end to the tmp_dir function. The directory and its path are created in the same way. The IO action is passed the path of the new directory. When it finishes - normally or with an exception - the directory is deleted.

The action must clean up any files it creates inside the directory by itself. with_temp_dir doesn't delete any files inside, so the directory could be removed. If the directory isn't empty, an IOError results (with the path filled in). When the action throws an exception, and the temporary directory cannot be removed, then the exception is passed through, rather than replacing it with the IOError. (This is because it's probably exactly because of that exception that the directory isn't empty and can't be removed).

with_tmp_dir prefix io = with_temp_dir 6 (prefix ++ ".") "" io

See tmp_dir, with_temp_dir, with_tmp_file.

temp_pathSource

Arguments

:: Int

Number of random characters to intersperse. Must be large enough, such that most combinations can't already exist.

-> String

Prefix for the path to generate.

-> String

Suffix for the path to generate.

-> IO FilePath

Generated path.

Create a temporary path. This will generate a path which does not yet exist in the file system. It consists of the specified prefix, a sequence of random characters (digits and letters), and the specified suffix.

Avoid relying on the generated path not to exist in the file system. Or else you'll get a potential race condition, since some other process might create the path after temp_path, before you use it. This is a security risk. The global random number generator (Random.randomRIO) is used to generate the random characters. These might not be that random after all, and could potentially be guessed. Rather use temp_file or temp_dir.

See temp_file, temp_dir.

untilIO :: Monad m => m b -> (b -> m Bool) -> m bSource

data Mntent Source

One entry of mount information. This is the same as struct mntent from <mntent.h>. A list of these is returned by the functions which read mount information.

See read_mounts, read_mtab, read_fstab.

Constructors

Mntent 

Fields

mnt_fsname :: String

Device file ("name of mounted file system")

mnt_dir :: String

Mount point

mnt_type :: String

Which kind of file system ("see mntent.h")

mnt_opts :: String

Mount options ("see mntent.h")

mnt_freq :: Int

Dump frequency in days

mnt_passno :: Int

"Pass number on parallel fsck"

read_mountsSource

Arguments

:: String

File to read (typically /etc/mtab or /etc/fstab)

-> IO [Mntent]

Mount information in that file

Read mount information. This is a front end to the setmntent(3), getmntent(3), endmntent(3) system library functions.

When the setmntent call fails, the errno value is converted to an IOError and thrown.

See read_mtab, read_fstab.

read_mtab :: IO [Mntent]Source

Get the currently mounted file systems.

read_mtab = read_mounts "/etc/mtab"

See read_mounts.

read_fstab :: IO [Mntent]Source

Get the system wide file system table.

read_fstab = read_mounts "/etc/fstab"

See read_mounts.

with_wdSource

Arguments

:: FilePath

New working directory

-> IO a

Action to run

-> IO a 

Change the working directory temporarily. This executes the specified IO action with a new working directory, and restores it afterwards (exception-safely).

globSource

Arguments

:: String

Pattern

-> IO [String]

Sorted list of matching paths

This is an interface to the POSIX glob function, which does wildcard expansion in paths. The list of matched paths is returned. It's empty for no match (rather than the original pattern). In case anything goes wrong (such as permission denied), an IOError is thrown.

This does not do tilde expansion, which is done (among many unwanted other things) by wordexp. The only flag used for the call to glob is GLOB_ERR.

The behaviour in case of non-existing path components is inconsistent in the GNU version of the underlying glob function. glob /doesnt_exist/foo will return the empty list, whereas glob /doesnt_exist/* causes a No such file or directory IOError.

See man pages glob(3) and wordexp(3).