Skip to content

Instantly share code, notes, and snippets.

View Jeff-Russ's full-sized avatar

Jeffrey Russ Jeff-Russ

View GitHub Profile

Study: Bash fails: splitting strings

Shell lib: Mostly String Manipulation Functions

Shell lib: String I/O Functions


fancyprompt for bash

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.

Study: Bash Scope Test

Observe bash's scoping in action with variables declared:

  1. inside and outside of functions,
  2. in various orders and
  3. with and without the local keyword
@Jeff-Russ
Jeff-Russ / check_command.sh
Last active May 28, 2016 01:29
Safely get (re-return) or see (echo) the return code from command or 127 if not found... and more
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
@Jeff-Russ
Jeff-Russ / ask_yn.sh
Last active May 28, 2016 19:11
Gen-purpose [y/n] "ask" prompt function for Bash based on http://djm.me/ask
#!/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.
@Jeff-Russ
Jeff-Russ / Study: Bash Variable Quoting.md
Last active May 29, 2016 08:06
Study: Bash Variable Quoting

Study: Bash Variable Quoting

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

Run (almost) Any Language From a Bash Script

You probably know that when you make a bash script you give it a shebang like one of these:

#!/bin/sh
#!/bin/bash
#!/usr/local/bin/bash 
#!/usr/bin/env bash
@Jeff-Russ
Jeff-Russ / recursive_reduce_fraction.rb
Last active June 1, 2016 19:48
Example of Recursion in Ruby: Simplifying a Fraction
#!/usr/bin/env ruby
# Example of Recursion in Ruby: Simplifying a Fraction
########## non-recursive version: #########
def findGcd (a, b)
while b != 0 do

JS Objects vs Ruby Hashes

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: