Skip to content

Instantly share code, notes, and snippets.

View YakDriver's full-sized avatar
🕳️
🌑

Dirk Avery YakDriver

🕳️
🌑
View GitHub Profile
@YakDriver
YakDriver / aws_profile_manager.sh
Created January 22, 2018 16:47
Manage an AWS profile instance given environment variables
# Given environment variables of INSTANCE_PROFILE and INSTANCE_ROLE, this will create the profile,
# swap out the role, remove the role, add the role to make sure at the end, INSTANCE_PROFILE contains
# one and only one role, INSTANCE_ROLE
if aws iam get-instance-profile --instance-profile-name "${INSTANCE_PROFILE}" ; then
echo "Profile already exists. Checking instance profile..."
OLD_ROLE="$(aws iam get-instance-profile --instance-profile-name $INSTANCE_PROFILE | ./jq.dms -r '.InstanceProfile.Roles[0].RoleName')"
if [ "${OLD_ROLE}" = "null" ] || [ -z "${OLD_ROLE}" ] ; then
OLD_ROLE=none
fi
if [ "${INSTANCE_ROLE}" = "${OLD_ROLE}" ] ; then
@YakDriver
YakDriver / poll_progress.sh
Created February 1, 2018 21:46
One liner that blocks while it polls for existence of a file while displaying progress based on size of a log file, assuming final size is known
#!/bin/bash
# One liner that blocks while it polls for existence of a file while displaying progress based on size of a log file, assuming final size is known
# displays "0% done" through "100% done" but never goes over 100% even if log file gets bigger than LOG_DONE_SIZE
LOG_DONE_SIZE=57046
while [ ! -f /tmp/SETUP_COMPLETE_SIGNAL ]; do echo "scale=0; $(($(wc -c < /var/log/thefile.log)<$LOG_DONE_SIZE?$(wc -c < /var/log/thefile.log}):$LOG_DONE_SIZE))*100/$LOG_DONE_SIZE" | bc | awk '{printf "%d%% done", $0}' ; sleep 10 ; done
@YakDriver
YakDriver / random_string.ps1
Created February 1, 2018 23:37
Powershell random string of characters
# 4-length string, upper & lower alpha
$RAND=-join ((65..90) + (97..122) | Get-Random -Count 4 | % {[char]$_})
@YakDriver
YakDriver / Retry-TestCommand.ps1
Created February 6, 2018 18:39
Retry-TestCommand
function Retry-TestCommand
{
param (
[Parameter(Mandatory=$true)][string]$Test,
[Parameter(Mandatory=$false)][hashtable]$Args = @{},
[Parameter(Mandatory=$false)][string]$TestProperty,
[Parameter(Mandatory=$false)][int]$Tries = 5,
[Parameter(Mandatory=$false)][int]$SecondsDelay = 2,
[Parameter(Mandatory=$false)][switch]$ExpectNull
)
@YakDriver
YakDriver / globals.sh
Last active February 9, 2018 19:49
Bash Basics: Global variables
#!/bin/bash
# Shows that global scope variables can be modified in a function.
#
# OUTPUT:
#
# 0
# 5
#
@YakDriver
YakDriver / increment.sh
Last active February 9, 2018 19:47
Bash Basics: Increment variables
#!/bin/bash
# 3 correct and 1 incorrect way to increment bash variables using "let" and arithmetic expansion.
#
# OUTPUT:
#
# 0
# 0+1
# 1
# 2
@YakDriver
YakDriver / arguments.sh
Created February 9, 2018 20:10
Bash Basics: Function arguments
#!/bin/bash
# Demonstrates the use of shift and $@ mainly. Function arguments can also be accessed with $1, $2, $3, etc.
#
# ##fun1
# aaa
# ##fun2
# bbb
# ##fun3
# (# of arguments: 3)
@YakDriver
YakDriver / try_catch.sh
Last active February 12, 2018 15:44
Bash Basics: Sorta try/catch/finally block
#!/bin/bash
# Shows the right and wrong ways of try/catch/finally blocks in shell scripting.
#
# OUTPUT:
#
# *Try* 1 (FAIL) ---------
# aaa
# bbb
# ./try_catch.sh: line 34: thisisabadcommand: command not found
@YakDriver
YakDriver / std.sh
Last active February 12, 2018 15:42
Bash Basics: Redirecting STDOUT and STDERR
#!/bin/bash
# Shows how STDOUT and STDERR can be redirected to /dev/null, which makes it go away, but could be redirected anywhere
#
# OUTPUT
#
# 1 - STDOUT redir, but not STDERR
# ./return_var.sh: line 13: thisisbad: command not found
# 2 - STDOUT redir, but not STDERR
# 3 - STDERR redir, but not STDOUT
@YakDriver
YakDriver / array_io.sh
Last active February 15, 2018 20:59
Bash Basics: Reading AND Writing Arrays
#!/bin/bash
# It's suprisingly hard to find example of both reading AND writing arrays to files that are compatible with each other.
# This is a mated pair of ways to read and write, respectively.
#
# OUTPUT:
#
# 0
# A line of text
# new_arr[1]: A line of text