Skip to content

Instantly share code, notes, and snippets.

@jimmyadaro
Created September 16, 2019 21:27
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 jimmyadaro/4958af0dddf410c4f48387ac8ced6728 to your computer and use it in GitHub Desktop.
Save jimmyadaro/4958af0dddf410c4f48387ac8ced6728 to your computer and use it in GitHub Desktop.
Concat function return with string for "read -p"
# DESCRIPTION: Simple example of "read -p" and it's usage when trying to print a simple concat from a function and a user input
# Here we'll assume "$folder" comes from a previously-checked "getopts options" loop
# (given as an argument for our script/command option, like "ourscript -f MyFolderName")
##################################################################
##################################################################
##################################################################
# Print a styled "important:" text
# This will be the usual "echo"-ed text that we need to concat for "read -p [message]"
info_message() {
declare the_message="$1"
echo -e "'IMPORTANT:' $the_message"
}
######### Concat given string from function "echo" return #########
# Execute "info_message()" passing a concatenated string as $1
# Note the escaped backslashes on ${folder} for this string
folder_info=$(info_message "\"${folder}\" does not exist, do you want to create it? [Y/n]");
# Just use "$folder_info" string-type as a variable
# In this case, "info_message()" will "echo" the whole string
read -p "${folder_info}" yn
##################################################################
# Simple prompt response check...
case $yn in
# Create the folder
[Yy]* )
sudo mkdir $some_path/$folder/
info_message "Folder created successfully";
break ;;
# Do not create the folder
[Nn]* )
break;;
# Other response than [Yy] or [Nn]
* )
info_message "Please answer Yes (y) or No (n)";;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment