Last active
December 18, 2015 05:39
-
-
Save JamesTheBard/5734065 to your computer and use it in GitHub Desktop.
Clipboard copy script for files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Display some help text | |
usage() { | |
echo "clipcp - Copy the contents of a text file to the clipboard"; | |
echo "Usage: clipcp FILE"; | |
} | |
# 'Cat' the file and pipe it into the xsel program which throws whatever | |
# it gets onto the clipboard. | |
copy_file_to_clipboard() { | |
cat $1 | xsel --clipboard --input; | |
} | |
# Quick convenience function to echo things to STDERR. The magic part is | |
# the 1>&2 part which redirects STDOUT to STDERR | |
echoerr() { | |
echo "$1" 1>&2; | |
} | |
# Loop through all of the options that given to the program when it's | |
# called (like -h). Get really angry if any option is given that isn't | |
# -h and exit with error. If it _is_ -h, display the help text and then | |
# exit normally without getting angry. | |
while getopts ":h" opt; do | |
case $opt in | |
h) | |
usage | |
exit 0 | |
;; | |
\?) | |
echoerr "fcopy: Option '-$OPTARG' not recognized, quitting." | |
exit 1 | |
;; | |
esac | |
done | |
# Basically, get rid of all of the options that were parsed by the while | |
# loop. This should only leave us with the file we want to copy assuming | |
# that there weren't two files tacked on to the end. | |
shift $(( OPTIND - 1 )) | |
# Improve legibility by taking the first remaining (and hopefully only) | |
# remaining option and place it into $INPUT_FILE | |
INPUT_FILE=$1 | |
# If no file was give (basically, clipcp was called by itself), display | |
# the help text. | |
if [ -z $INPUT_FILE ] | |
then | |
usage | |
# Check to see if $INPUT_FILE actually exists. If it does, copy it to | |
# the clipboard using the copy_file_to_clipboard function above. | |
elif [ -e $INPUT_FILE ] | |
then | |
copy_file_to_clipboard $INPUT_FILE | |
# If we've progressed to this point, it's usually because the file does | |
# not exist. Get angry and return the contents of $INPUT_FILE to the | |
# user via the echoerr function and exit abnormally. | |
else | |
echoerr "fcopy: cannot stat '$INPUT_FILE': No such file." | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment