shelltools.lisp

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)
         while line do (format stream "~a~%" 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)
         while line do (push (string-trim '(#\Space #\Newline #\Tab) line) my-list)))
    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))
        (my-command (if (consp command) (car command) command)))
    (unless (pgrep my-check)
      (shell (format nil "~a &" my-command)))))

(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-result
    (format nil "units -t '~a' '~a'" from to))))

(defun killall (program &key (signal "15"))
  (shell
   (format nil "killall -s ~a '~a'" signal program)))

(defun play (music)
  (killall "play")
  (shell (format nil "play '~a' > /dev/null 2>&1 &" music)))

;;; 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))
         (status (caddr (multiple-value-list (shell ssh-command)))))
    (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


Full name:


E-mail:


Message:


"/>