Skip to content

Instantly share code, notes, and snippets.

Avatar
🕳️
🌑

Dirk Avery YakDriver

🕳️
🌑
View GitHub Profile
@YakDriver
YakDriver / aws_profile_manager.sh
Created Jan 22, 2018
Manage an AWS profile instance given environment variables
View aws_profile_manager.sh
# 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 Feb 1, 2018
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
View poll_progress.sh
#!/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 Feb 1, 2018
Powershell random string of characters
View random_string.ps1
# 4-length string, upper & lower alpha
$RAND=-join ((65..90) + (97..122) | Get-Random -Count 4 | % {[char]$_})
View Retry-TestCommand.ps1
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 Feb 9, 2018
Bash Basics: Global variables
View globals.sh
#!/bin/bash
# Shows that global scope variables can be modified in a function.
#
# OUTPUT:
#
# 0
# 5
#
@YakDriver
YakDriver / increment.sh
Last active Feb 9, 2018
Bash Basics: Increment variables
View increment.sh
#!/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 Feb 9, 2018
Bash Basics: Function arguments
View arguments.sh
#!/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 Feb 12, 2018
Bash Basics: Sorta try/catch/finally block
View try_catch.sh
#!/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 Feb 12, 2018
Bash Basics: Redirecting STDOUT and STDERR
View std.sh
#!/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 Feb 15, 2018
Bash Basics: Reading AND Writing Arrays
View array_io.sh
#!/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