Skip to content

Instantly share code, notes, and snippets.

@hopeseekr
Created October 22, 2020 16:11
Show Gist options
  • Save hopeseekr/460700d166487dcd11d2dbd5f36b8077 to your computer and use it in GitHub Desktop.
Save hopeseekr/460700d166487dcd11d2dbd5f36b8077 to your computer and use it in GitHub Desktop.
Grab the text from an existing file -or- user input in BASH
#!/bin/bash
#############################################################
# Grab the text from an existing file -or- user input... #
# #
# Copyright © 2020 Theodore R. Smith #
# License: Creative Commons Attribution v4.0 International #
# From: https://github.com/hopeseekr/BashScripts/ #
# @see https://stackoverflow.com/a/64486155/430062 #
#############################################################
function grabDocument()
{
if [ ! -z "$1" ] && [ -f "$1" ]; then
echo $(<"$1")
else
echo "" >&2
echo "Please type/paste in bash script you wish to be run when NetworkManager connects to '${HOTSPOT}'." >&2
echo "Press CTRL+D when finished." >&2
echo "You should start with '#!/bin/bash'..." >&2
echo "" >&2
# Read user input until CTRL+D.
# @see https://stackoverflow.com/a/38811806/430062
readarray -t user_input
# Output as a newline-dilemeted string.
# @see https://stackoverflow.com/a/15692004/430062
printf '%s\n' "${user_input[@]}"
fi
}
SCRIPT=$(grabScript "$2")
# Preserve white spaces and newlines.
# @see https://stackoverflow.com/a/18018422/430062
echo "$SCRIPT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment