Wednesday, January 6, 2016

To the Clipboard! In Bash

#!/usr/bin/env bash

###     Copyrighted +A.M.Danischewski (c)2016+  
### You may reuse this software without limit provided 
###         this notice remain in tact. 
###
### File: clip.bsh
### Requires: xsel 
###  Description: This script copies something to the clipboard, I 
###  recommend a simple alias, such as: $> alias clip='clip.bsh' 
###  Then you can:    
###     $> echo "hi there" | clip 
###     $> clip myblogpost.html
###     $> clip <<< "little message" 
###  _-=  Enjoy!  _-=
  
declare cliptxt="${1}"
declare rdlnk=$(readlink /proc/$$/fd/0)
declare nl=$(echo -en "\x01") 
if grep -Eq "^pipe:|deleted" <<< "${rdlnk}"; then 
 [[ ! -z "${cliptxt}" ]] && cliptxt=""
 while IFS= read -d $'\n' -r piped_input || break; do cliptxt="${cliptxt}${piped_input}${nl}"; done  
elif [[ -f "${rdlnk}" ]]; then   ## Its a redirected file
 cliptxt=$(< "${rdlnk}") 
elif [[ -e "${cliptxt}" ]]; then ## Its a filename
 cliptxt=$(< "${cliptxt}")       ## File name proper no redirect
else 
 echo " *** Something is wrong, input not read by ${0}." 
fi 
echo -En "${cliptxt}" | tr "${nl}" "\n" | xsel -ib
exit 0 

No comments:

Post a Comment