Skip to content

Commit

Permalink
Create script for copying file contents
Browse files Browse the repository at this point in the history
  • Loading branch information
lindhe committed Aug 12, 2023
1 parent 54a4fa3 commit 4272679
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# Scripts

These are misc scripts I've made over the years to help with different things.

It's migrated from [lindhe/dotfiles](https://github.com/lindhe/dotfiles) and I couldn't be bothered getting the history too.
So the old stuff is in the history over there.
5 changes: 5 additions & 0 deletions terminal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Terminal

Terminal helper programs.

* `copy.sh`: Copy the contents of a file to the clipboard.
51 changes: 51 additions & 0 deletions terminal/copy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash
# Copy the contents of a file to the clipboard.

set -euo pipefail

stderr() {
echo "${@}" 1>&2
}

fail() {
stderr "${1}"
stderr ""
stderr "Exiting …"
exit "${2:-1}"
}

if [[ $# -ne 1 ]]; then
stderr ""
stderr "USAGE:"
stderr " ${0} <file>"
stderr ""
exit 0
fi

if [[ "$(uname -r)" =~ .*microsoft.* ]]; then
declare -r IS_WSL=true
else
declare -r IS_WSL=false
fi

if [[ "${IS_WSL}" == "false" ]]; then
missing_dependencies=false
declare -r dependencies=(
xclip
)
for dep in "${dependencies[@]}"; do
if ! command -v "${dep}" &> /dev/null; then
stderr "❌ ERROR: Missing dependency ${dep}"
missing_dependencies=true
fi
done
if ${missing_dependencies}; then
fail 'Please install the missing dependencies!'
fi
fi

if [[ "${IS_WSL}" == "true" ]]; then
/mnt/c/Windows/System32/clip.exe < "${1}"
else
xclip -selection clipboard < "${1}"
fi

0 comments on commit 4272679

Please sign in to comment.