7.5. Shell Commands
(lsh/libc/shell.lsh)


This set of functions is designed to facilitate the writing of "shell scripts" in Lush. It includes functions that are more or less equivalent to common shell commands such as ls, cd, cp, mv, rm etc. Those commands manipulate and return lists of strings. Other functions are provided to manipulate those lists of strings.

7.5.0. String List Utilities
(lsh/libc/shell.lsh)




7.5.0.0. (glob regex l)
[DE] (lsh/libc/shell.lsh)


Return a list of the elements of l that match the regular expression regex . l must be a list of strings, and regex a string containing a valid Lush regular expression.

7.5.0.1. (split-words s)
[DE] (lsh/libc/shell.lsh)


split a string of words (space-separated strings) into a list or those words.

7.5.0.2. (merge-words l [c])
[DE] (lsh/libc/shell.lsh)


turns a list of strings into a single string composed of the concatenation of the elements of l interspesed with spaces. if a string is provided in c , it is interspesed instead of spaces.

7.5.0.3. (read-lines f)
[DE] (lsh/libc/shell.lsh)


read file f and return a list of strings, each of which is a line of the file.

7.5.0.4. (write-lines l)
[DE] (lsh/libc/shell.lsh)


write each string in list l as a line.

7.5.0.5. #Pexpr
(lsh/libc/shell.lsh)


equivalent to (write-lines expr )

7.5.1. File Information
(lsh/libc/shell.lsh)


The functions in this section provide a simple interface to the do-it-all function fileinfo . The all return NIL if the file passed as argument does not exist or is unaccessible. Some functions in this section return dates as strings. Those date strings can be compared with the usual comparison operators. (fileinfo filename )

7.5.1.0. (file-type f)
[DE] (lsh/libc/shell.lsh)


return the type of file f as an atom equal to 'reg for a regular file, 'dir for a directory, 'chr for character device, etc.

7.5.1.1. (file-size f)
[DE] (lsh/libc/shell.lsh)


return the size of file f in bytes

7.5.1.2. (file-mode f)
[DE] (lsh/libc/shell.lsh)


return the permissions of file f as an integer.

7.5.1.3. (file-uid f)
[DE] (lsh/libc/shell.lsh)


return the userid of the owner of file f

7.5.1.4. (file-gid f)
[DE] (lsh/libc/shell.lsh)


return the groupid of file f

7.5.1.5. (file-atime f)
[DE] (lsh/libc/shell.lsh)


return the last access time of file f as a string of the form "YYYY MM DD hh mm ss". The string can be turned into a list with split-words.

7.5.1.6. (file-mtime f)
[DE] (lsh/libc/shell.lsh)


return the last modification time of file f as a string of the form "YYYY MM DD hh mm ss". The string can be turned into a list with split-words.

7.5.1.7. (file-ctime f)
[DE] (lsh/libc/shell.lsh)


return the creation time of file f as a string of the form "YYYY MM DD hh mm ss". The string can be turned into a list with split-words.

7.5.1.8. (file-newer? f1 f2)
(lsh/libc/shell.lsh)


returns t if the modification time of file f1 is later than that of file f2 , or if f2 does not exist. Here is how this function can be use for a "make"-like function:

 (when (file-newer? src dst) (produce-dst-from-src src dst)) 


7.5.2. Filename Manipulations
(lsh/libc/shell.lsh)


a number of functions to manipulate file names, and paths are provided as part of the core interpreter, including basename, dirname, concat-fname, relative-fname, tmpname . The present set of function adds to that set. The present functions are not as portable as the the ones in the core interpreter, as they assume Unix-style or URL-style path constructs (slash-separated directories) (basename path [ suffix ]) (dirname path ) (concat-fname [ filename1 ] filename2 )

7.5.2.0. (filename-get-suffixes f)
[DE] (lsh/libc/shell.lsh)


returns all the suffix(es) of a file name, that is all the characters after the last slash and after (and not including) the the last dot. Return the empty string if no suffix is found. A trailing dot is not considered a suffix.

? (filename-get-suffixes "dirname/basename.suffix")
= "suffix"

? (filename-get-suffixes "dirname/basename.suffix1.suffix2")
= "suffix1.suffix2"

? (filename-get-suffixes "dirname.notasuffix/basename.suffix1.suffix2")
= "suffix1.suffix2"

? (filename-get-suffixes "dirname.notasuffix/basename")
= ""

? (filename-get-suffixes "dirname.notasuffix/basename.")
= ""



7.5.2.1. (filename-chop-suffixes f)
[DE] (lsh/libc/shell.lsh)


remove all the suffix(es) from a file name, that is all the characters after the last slash and starting at the first dot. A trailing dot is not considered a suffix.

? (filename-chop-suffixes "dirname/basename.suffix")
= "dirname/basename"

? (filename-chop-suffixes "dirname/basename.suffix1.suffix2")
= "dirname/basename"

? (filename-chop-suffixes "dirname.notasuffix/basename.suffix1.suffix2")
= "dirname.notasuffix/basename"

? (filename-chop-suffixes "dirname.notasuffix/basename")
= "dirname.notasuffix/basename"

? (filename-chop-suffixes "dirname.notasuffix/basename.")
= "dirname.notasuffix/basename."



7.5.2.2. (filename-get-suffix f)
[DE] (lsh/libc/shell.lsh)


returns the last suffix of a file name, that is all the characters after the last slash and after (and not including) the last dot. Return the empty string if no suffix is found. A trailing dot is not considered a suffix.

? (filename-get-suffix "dirname/basename.suffix")
= "suffix"

? (filename-get-suffix "dirname/basename.suffix1.suffix2")
= "suffix2"

? (filename-get-suffix "dirname.notasuffix/basename.suffix1.suffix2")
= "suffix2"

? (filename-get-suffix "dirname.notasuffix/basename")
= ""

? (filename-get-suffix "dirname.notasuffix/basename.")
= ""



7.5.2.3. (filename-chop-suffix f)
[DE] (lsh/libc/shell.lsh)


remove the last suffix from a file name, that is all the characters after (and including) the last dot. A trailing dot is not considered a suffix.

? (filename-chop-suffix "dirname/basename.suffix")
= "dirname/basename"

? (filename-chop-suffix "dirname/basename.suffix1.suffix2")
= "dirname/basename.suffix1"

? (filename-chop-suffix "dirname.notasuffix/basename.suffix1.suffix2")
= "dirname.notasuffix/basename.suffix1"

? (filename-chop-suffix "dirname.notasuffix/basename")
= "dirname.notasuffix/basename"

? (filename-chop-suffix "dirname.notasuffix/basename.")
= "dirname.notasuffix/basename."



7.5.3. Variables
(lsh/libc/shell.lsh)




7.5.3.0. shell-home
(lsh/libc/shell.lsh)


user home directory

7.5.3.1. shell-dirstack
(lsh/libc/shell.lsh)


directory stack manipulated by pushd and popd

7.5.3.2. shell-tmpdir
(lsh/libc/shell.lsh)


directory used by the function sh to store temporary files.

7.5.4. Directories
(lsh/libc/shell.lsh)




7.5.4.0. (pwd)
[DE] (lsh/libc/shell.lsh)


return current directory (equivalent to (chdir)).

7.5.4.1. (cd [d])
[DE] (lsh/libc/shell.lsh)


change current directory to d , or to user home if p is not present.

7.5.4.2. (pushd d)
[DE] (lsh/libc/shell.lsh)


Temporarily change current directory to d . Returning to the previous directory can be done with popd.

7.5.4.3. (popd)
[DE] (lsh/libc/shell.lsh)


return to the current directory before the last pushd.

7.5.5. Shell Commands
(lsh/libc/shell.lsh)


This set of functions is designed to facilitate the writing of "shell scripts" in Lush. It includes functions that are more or less equivalent to common shell commands such as ls, cd, cp, mv, rm etc. Those commands manipulate and return lists of strings. Other functions are provided to manipulate those lists of strings.

7.5.5.0. (rm f)
[DE] (lsh/libc/shell.lsh)


remove file f (be careful).

7.5.5.1. (cp from to)
[DE] (lsh/libc/shell.lsh)


copy file from into file to

7.5.5.2. (mv from to)
[DE] (lsh/libc/shell.lsh)


move file from from to to .

7.5.5.3. (ls d1 d2 ... dn)
[DE] (lsh/libc/shell.lsh)


return a list of all the files in d1 (in case-insensitive lexicographic order), followed by all the files in d2 , etc... Invisible files (".", "..", ".xxx", etc) are not included.

7.5.5.4. (ls-a d1 d2 ... dn)
[DE] (lsh/libc/shell.lsh)


return a list of all the files in d1 (in case-insensitive lexicographic order), followed by all the files in d2 , etc... Invisible files (".", "..", ".xxx", etc) are included.

7.5.5.5. (sh cmd [l])
[DE] (lsh/libc/shell.lsh)


run shell command cmd and return the standard output as a list of strings (one string per line). The optional argument l is a list of strings that will be written to a temporary file (one line per string) and fed to the standard input of command cmd

7.5.5.6. (sh-find dir pattern)
[DE] (lsh/libc/shell.lsh)


calls the standard unix command find to find all the files whose name fit the pattern pattern (this is a unix-style shell regex, not a Lush regex). Example:
 (sh-find "." "*.lsh")