A fancy 'are you sure' style shell prompt
fancyprompt take at least two args, the prompt message and produces an 'are you
sure' style prompt with a timeout of 10 sec and option to cancel with 'c'.
The first arg is always the message as a string (accepts \n \t etc). And the
remaining args are function names that are called depending on the result.
getreturn() { | |
command -v "$*" >/dev/null 2>&1 || { | |
echo >&2 | |
return 127; | |
} | |
typeset cmnd="$*" | |
typeset ret_code | |
eval $cmnd >/dev/null 2>&1 | |
ret_code=$? | |
return $ret_code |
#!/bin/bash | |
ask_yn() { | |
# Adapted from http://djm.me/ask by Jeff-Russ | |
if [ "${2:-}" = "Y" ]; then prompt="Y/n"; default=Y | |
elif [ "${2:-}" = "N" ]; then prompt="y/N"; default=N | |
else prompt="y/n"; default= | |
fi | |
while true; do # keeps repeating the question until it gets a valid answer. |
Bash is designed to automatically reinterpret where one argument ends and a new one begins based on whitespace unless the string is surrounded by double quotes. It's because Bash is so string-centric that you can call a script or function like this:
some_func John Doe "age 24"
and all of those argument will be interpreted as string literals, not variable name or command names. It's because all you can pass is strings that you can omit the quotes. Even when you pass a variable, it needs to be expanded to a string via $
before being passed. If you omit the quotes around "age 24", however, the arg count will increase to four.
Inside the function you have a similar situation. Even if the last arg is passed in with double quotes you can run into
#!/usr/bin/env ruby | |
# Example of Recursion in Ruby: Simplifying a Fraction | |
########## non-recursive version: ######### | |
def findGcd (a, b) | |
while b != 0 do |
Ruby has iterators baked right in for all of it's containers:
whatever.each do |variable|
# code
end
You can do this with Ruby arrays, hashes, objects, etc but in Javascript you don't have this most of the time. You have it for arrays but when you want something like an associative array your go-to is a Javascript object literal you pretend is an associate array by just not putting methods in: