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 | |
# This script uses traps to create try/catch/finally functionality in shell scripts. | |
# | |
# OUTPUT: | |
# | |
# Hello! We're reporting live from script | |
# ./try_catch2.sh: line 23: badcommand: command not found | |
# ./try_catch2.sh: line 23: exiting with status 127 | |
# It's the end of the line |
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 | |
# 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 |
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 | |
# 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 |
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 | |
# 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 |
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 | |
# 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) |
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 | |
# 3 correct and 1 incorrect way to increment bash variables using "let" and arithmetic expansion. | |
# | |
# OUTPUT: | |
# | |
# 0 | |
# 0+1 | |
# 1 | |
# 2 |
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 | |
# Shows that global scope variables can be modified in a function. | |
# | |
# OUTPUT: | |
# | |
# 0 | |
# 5 | |
# |
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
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 | |
) |
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
# 4-length string, upper & lower alpha | |
$RAND=-join ((65..90) + (97..122) | Get-Random -Count 4 | % {[char]$_}) |
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 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 |