scripts/copy.sh

29 lines
539 B
Bash
Raw Normal View History

2023-10-13 11:35:53 +11:00
#!/bin/bash
2023-10-13 11:39:06 +11:00
# Copy file contents or command output to clipboard
2023-10-13 11:35:53 +11:00
# Usage: copy <file>
2023-10-13 11:39:06 +11:00
# ... | copy
2023-10-13 11:35:53 +11:00
#
# Author: Nathan Woodburn
copy() {
if tty -s; then
file="$1"
2023-10-13 11:39:06 +11:00
if [ -n "$file" ] && [ ! -e "$file" ]; then
2023-10-13 11:35:53 +11:00
echo "$file: No such file or directory" >&2
return 1
fi
xclip -selection clipboard < "$file"
else
2023-10-13 11:39:06 +11:00
xclip -selection clipboard
2023-10-13 11:35:53 +11:00
fi
}
2023-10-13 11:39:06 +11:00
# Call the copy function with the provided arguments or from a pipe
if [ -t 0 ]; then
copy "$@"
else
copy
fi