From 472cad1edb73fa70169d8042eadc1ad8aacbf4e3 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Fri, 13 Oct 2023 11:39:06 +1100 Subject: [PATCH] fix: Copy pipe usage --- copy | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/copy b/copy index 334d285..436e089 100644 --- a/copy +++ b/copy @@ -1,32 +1,28 @@ #!/bin/bash -# Copy file contents to clipboard +# Copy file contents or command output to clipboard # Usage: copy -# ... | copy +# ... | copy # # Author: Nathan Woodburn copy() { - if [ $# -eq 0 ]; then - echo "No arguments specified." - echo "Usage: copy " - return 1 - fi - if tty -s; then file="$1" - if [ ! -e "$file" ]; then + if [ -n "$file" ] && [ ! -e "$file" ]; then echo "$file: No such file or directory" >&2 return 1 fi xclip -selection clipboard < "$file" - else - file_name="$1" - xclip -selection clipboard < "$file_name" + xclip -selection clipboard fi } -# Call the copy function with the provided arguments -copy "$@" +# Call the copy function with the provided arguments or from a pipe +if [ -t 0 ]; then + copy "$@" +else + copy +fi