Skip to content

Instantly share code, notes, and snippets.

View Jeff-Russ's full-sized avatar

Jeffrey Russ Jeff-Russ

View GitHub Profile

Study: Execute Shell Commands from Node.js

Study: .bashrc vs .bash_profile

On most *nix:

  • ~/.bash_profile
    • executed when type un/pw to login via console
    • executed when type un/pw to login via ssh
    • not executed when you type /bin/bash to start a new bash instance
    • not executed when open a new terminal instance unless mac os
@Jeff-Russ
Jeff-Russ / _ Shell Command: addpath .md
Last active May 26, 2016 19:37
Temporarily add a bunch of scripts to PATH to make directly executable

Shell Command: addpath

Temporarily add that a path to your $PATH and make contents executable as if built in commands.

@Jeff-Russ
Jeff-Russ / _ Demo: Sass:log.md
Last active May 26, 2016 19:34
Debug you complex Sass right to the browser!

Demo: Sass:log

Debug you complex Sass right to the browser!

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

Shell lib: Mostly String Manipulation Functions

@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: Multi-File Bash Scripting and Paths.md
Last active August 9, 2016 06:23
Study: including other bash scripts from relative paths and absolute paths

Bash Scripting:

Managing Pathnames in Multi-file Bash Projects

Paths can get somewhat muddled up when you execute a Bash script with dependencies. Each script, when executed, is executed at a certain location as demonstrated by echoing pwd from the script. This is the directory assumed if ever you try to source (aka .) from that script.

This location is subject to change as it traces back to the ORIGINAL caller. By "ORIGINAL" I don't mean the location script that called it, or even the location of the script that called the script that called it, I mean the working directory of the first to initiate the chain of calls, which might be the user in the terminal or the location of the clicked executable.

Thing to consider:

  1. The caller's location
@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