Skip to content

Instantly share code, notes, and snippets.

@mattpotts
Last active September 7, 2017 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattpotts/8061681c7a535a6a9abed77beeb70c61 to your computer and use it in GitHub Desktop.
Save mattpotts/8061681c7a535a6a9abed77beeb70c61 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Copy specific characters from a string to the clipboard
# Enter empty character index to exit
# Read password
echo -n "Password: "
read -s password
echo
# Until enough characters have been copied..
while true; do
# Read character index
echo -n "Index: "
read index
if [[ -z $index ]]; then # Empty input
echo "Bye"
exit
elif ! [[ $index =~ ^[0-9]+$ ]]; then # Non numeric input
echo "Enter a number"
echo
elif [[ $index < 1 ]]; then # Zero input
echo "Enter a number 1-${#password}"
echo
# elif [[ ${index} > ${#password} ]]; then # Index doesn't exist: doesn't work!
# echo "Enter a number 1-${#password}"
# echo
else # Copy character
echo "${password:$index-1:1}" | pbcopy
echo "Copied!"
echo
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment