shelltools.lisp
This file contains functions mostly related to shell commands. It is automatically updated and included in this page upon generation of the website.
;; (shell-1st-result "messages -q ~/Maildir")
defun shell-1st-result (command)
("Returns the first line of a shell command"
ignore-errors (with-open-stream (s (ext:make-pipe-input-stream command))
(string-trim '(#\Space #\Tab #\Newline) (read-line s)))))
(
defun shell-to-string (command)
(with-output-to-string (stream)
(with-open-stream (s (run-shell-command command :output :stream))
(loop for line = (read-line s nil nil)
(do (format stream "~a~%" line)))))
while line
defun shell-lines2list (command)
(let ((my-list '()))
(with-open-stream
(
(s (ext:make-pipe-input-stream command))loop for line = (read-line s nil nil)
(do (push (string-trim '(#\Space #\Newline #\Tab) line) my-list)))
while line
my-list))
defun shell-1st-line (command)
(with-open-stream
(
(s (ext:make-pipe-input-stream command))read-line s nil nil)))
(
defun touch (file)
(close (open file :if-does-not-exist :create)))
(
defun which (program)
(if (not (shell (format nil "which ~a > /dev/null 2>&1" program)))
(t nil))
;; (defun which (program)
;; (let* ((PATH (remove-duplicates (regexp:regexp-split ":" (getenv "PATH"))))
;; (PATH (mapcar 'slash-add PATH)))
;; PATH))
;; (directory (concatenate 'string (car (which "ls")) "*"))
defun delete-backup-files nil
(mapcar #'delete-file (directory "./*~")))
(
defun pgrep (command &key (options ""))
(let ((result (shell-1st-result (format nil "pgrep ~a \"~a\"" options command))))
(if result result nil)))
(
defun run-if-not-running (command)
(let ((my-check (if (consp command) (cadr command) command))
(if (consp command) (car command) command)))
(my-command (unless (pgrep my-check)
(format nil "~a &" my-command)))))
(shell (
defun sudo (command)
(let ((run-command (format nil "sudo ~a" command)))
(values (shell run-command)
(
run-command)))
defun units-value (from to)
(read-from-string
(
(shell-1st-resultformat nil "units -t '~a' '~a'" from to))))
(
defun killall (program &key (signal "15"))
(
(shellformat nil "killall -s ~a '~a'" signal program)))
(
defun play (music)
("play")
(killall format nil "play '~a' > /dev/null 2>&1 &" music)))
(shell (
;;; SSH tools
defun ssh-remote-command (command &key host (port 22))
("Simply executes a SSH command and returns the status"
let* ((ssh-command (format nil "ssh -p ~a ~a \"~a\"" port host command))
(caddr (multiple-value-list (shell ssh-command)))))
(status (values status ssh-command))) (
Leave Your Comment or Contact GNU.Support
Contact GNU.Support now. There is a simple rule at GNU.Support: if we can help you, we do, whenever and wherever necessary, and it's the way we've been doing business since 2002, and the only way we know