Last active
August 29, 2015 14:16
-
-
Save cmattoon/fc74070f6438ec101797 to your computer and use it in GitHub Desktop.
Bash Script Default Parameter Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Triggers an error of params are empty. | |
username=${1:?"You must specify a username"} | |
password=${2:?"You must specify a password"} | |
echo "Setting password for ${username} to ${password}" | |
exit 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# One-liner to get the path to a binary, or exit with an error | |
ifconfig=${IFCONFIG:-$(which ifconfig)} || (echo "No binary found for ifconfig"; exit 1) || exit 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Examples of getting default variable values in Bash | |
value=${VARIABLE_NAME:-"Default value"} | |
username=${username:-"Guest"} | |
greeting=${GREETING:-Hello, ${username}} | |
echo "$greeting"; # 'Hello, Guest!' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# change_mac.sh | |
# Allow the user to specify the interface, but default to wlan0 | |
iface=${1:-"wlan0"} | |
echo "Enter iface name (default '${iface}'):" | |
read inpt | |
# If they entered something, use that instead | |
# (input validation omitted) | |
if [ ! -z "$inpt" ]; then | |
iface="$inpt" | |
fi | |
# Pass-through for ifconfig command | |
# Params: | |
# $1 - iface - The interface to use. Defaults to $iface. | |
# $2 - command - The command to run | |
function ifctrl() { | |
_iface=${1:-"$iface"} | |
_cmd=${2:-""} | |
if [ -z "$_cmd" ]; then | |
echo -e "\033[91m [!] Specify a command\033[0m" | |
exit 1; | |
fi | |
echo " [cmd] ifconfig {$_iface} {$_cmd}" | |
echo $(ifconfig "$_iface" "$_cmd") | |
} | |
# Prints a message to the screen | |
# Params: | |
# $1 - message - The string to display | |
# $2 - color - An ANSI color code | |
function printmsg() { | |
_msg=${1:-""} | |
_color=${2:-"92"} | |
echo -e "\033[${_color}m {$_msg}\033[0m" | |
} | |
# Puts the interface up | |
# Params: | |
# $1 - iface - The interface. Defaults to $iface | |
function ifup() { | |
_iface=${1:-"$iface"} | |
printmsg " [+] Putting {$_iface} up..." | |
echo $(ifctrl "$_iface" up) | |
} | |
# Puts the interface down | |
# Params: | |
# $1 - iface - The interface. Defaults to $iface | |
function ifdown() { | |
_iface=${1:-"$iface"} | |
printmsg " [+] Putting {$_iface} down..." | |
ifctrl "$_iface" down | |
} | |
# Sets the MAC address. | |
# Params: | |
# $1 - iface - The interface. Defaults to $iface | |
# $2 - mac - The MAC address to use. Defaults to random. | |
function set_mac() { | |
_iface=${1:-"$iface"} | |
_mac=${2:-"random"} | |
if [ "$_mac" == "random" ]; then | |
printmsg " [+] Generating random MAC address" | |
## Generate random mac | |
fi | |
printmsg " [+] Setting MAC to: {$_mac}" | |
echo $(ifctrl "$_ifaece" "hw ether {$_mac}") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Generates an htpasswd entry for a user. | |
# Args: | |
# $1 - username (default: current user) | |
# $2 - password (default: random) | |
username=${1:-$(whoami)} ## Replace with command output | |
passwd=${2:-$(openssl rand -base64 8)} ## Replace with command output | |
echo "Hello, ${username:-New User}" ## String replacement inside a string | |
echo "Creating htpasswd entry for ${username}" | |
result=$(${htpasswd:-$(which htpasswd)} -nb ${username} ${passwd}) | |
if [ "$2" != "$passwd" ]; then | |
echo "No password specified!" | |
echo "Generated random password: ${passwd}" | |
fi | |
echo "Add the following to htpasswd:" | |
echo "${result}" | |
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# filename: test.sh | |
# Generates an htpasswd entry for a user | |
# usage: ./test.sh | |
# If no args are specified, will create a random password for your current user. | |
# Args: | |
# $1 - username (default: current user) | |
# $2 - password (default: random) | |
# ENV vars: | |
# $HTPASSWD - Path to htpasswd binary | |
# $OPENSSL - Path to openssl binary | |
# $MIN_PWD_LEN - Min password length (default 8) | |
htpasswd=${HTPASSWD:-$(which htpasswd)} | |
openssl=${OPENSSL:-$(which openssl)} | |
username=${1:-$(whoami)} | |
password=${2:-$(openssl rand -base64 ${MIN_PWD_LEN:-8})} ## Nested statements work too | |
echo $($htpasswd -snb $username $password); | |
if [ -z "$2" ]; then | |
echo -e "Generated random password: \033[1m${password}\033[0m"; | |
fi | |
exit 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Generates a random password with openssl. | |
# Args: | |
# $1 - username (default: current user) | |
# $2 - password (default: random password) | |
# ENV vars: | |
# $HTPASSWD -- The location to the 'htpasswd' binary (run "sudo apt-get install apache2-utils" if missing) | |
# $OPENSSL -- The location to the 'openssl' binary | |
# $MIN_PWD_LEN -- The minimum random password length (default 8) | |
command="${HTPASSWD:-$(which htpasswd)} -nb ${1:-$(whoami)} ${2-$(${OPENSSL:-$(which openssl)} rand -base64 ${MIN_PWD_LEN-8})}"; | |
[ ! -z "$2" ] || echo -e "Generated random password: \033[1m$(echo $command | cut -d ' ' -f4)\033[0m"; | |
echo "Command: \033[1m${command}\033[0m"; | |
exit 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment