Compare commits
22 Commits
b396b71421
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4b279310c6 | ||
|
|
4bf2e28323 | ||
|
|
15aa13e2ad | ||
|
|
0d7d0d8523 | ||
|
|
a1e88c0c40 | ||
|
|
e78af4a047 | ||
|
|
f8f627af4e | ||
|
|
29969ce7a7 | ||
|
|
519f63d849 | ||
|
|
b1ef0a7e53 | ||
|
|
3040c6546d | ||
|
|
b3abecfeac | ||
|
|
96447a30e9 | ||
|
|
757f50fa5a | ||
|
|
ab3e263c17 | ||
|
|
58144cd28b | ||
|
|
a31518bc73 | ||
| 178d7f3b21 | |||
| d1146a45d6 | |||
|
|
d9647ceab1 | ||
|
|
79034b2888 | ||
|
|
a44d979737 |
@@ -1,9 +1,27 @@
|
||||
# Useful BASH snippets.
|
||||
|
||||
## Useful References
|
||||
|
||||
https://devhints.io/bash
|
||||
https://sharats.me/posts/shell-script-best-practices/ - awesome template of Good Things (TM) ...
|
||||
... which then leads to https://news.ycombinator.com/item?id=33354286 - I need to read and consume this properly, so many interesting tool and usage suggestions.
|
||||
|
||||
## one_liners.bash
|
||||
|
||||
Collection of simple tricks not big enough to need a separate file.
|
||||
|
||||
## argument_parsing.bash
|
||||
|
||||
Handling command line argument / parameter parsing at the beginning of a script.
|
||||
|
||||
## array_append.bash
|
||||
|
||||
How to declare an array, append new elements, iterate over the array for processing.
|
||||
|
||||
## associative_array.bash
|
||||
|
||||
Numerical arrays are referenced using integers, and associative are referenced using strings. Associative Arrays can be handy when you want the key to have meaning.
|
||||
|
||||
## booleans.bash
|
||||
|
||||
BASH doesn't actually have booleans, but there's tricks to make variables behave enough like bools to be useful.
|
||||
@@ -24,6 +42,10 @@ Quick and easy script runtime output.
|
||||
|
||||
tmux wrapper for another script, e.g. to have top running in part of your terminal while another task runs in the button, then nicely clean up afterwards.
|
||||
|
||||
## tput_colours_etc.bash
|
||||
|
||||
Using tput command to modify visual output, for example text colours.
|
||||
|
||||
## usage_help.bash
|
||||
|
||||
Put your usage output in a function to make it easily callable from multiple places as required.
|
||||
|
||||
13
bash/array_append.bash
Normal file
13
bash/array_append.bash
Normal file
@@ -0,0 +1,13 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Declare a string array
|
||||
arrVar=("AC" "TV" "Mobile" "Fridge" "Oven" "Blender")
|
||||
|
||||
# Add new element at the end of the array
|
||||
arrVar+=("Dish Washer")
|
||||
|
||||
# Iterate the loop to read and print each array element
|
||||
for value in "${arrVar[@]}"
|
||||
do
|
||||
echo $value
|
||||
done
|
||||
41
bash/associative_array.bash
Normal file
41
bash/associative_array.bash
Normal file
@@ -0,0 +1,41 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# associative_array.bash. Derived from:
|
||||
# https://stackoverflow.com/questions/3112687/how-to-iterate-over-associative-arrays-in-bash
|
||||
|
||||
# Make an array and stuff it full of values
|
||||
declare -A array
|
||||
array[repo_one]=true
|
||||
array[repo_two]=true
|
||||
array[repo_three]=false
|
||||
|
||||
# The key is accessed using the exclamation point
|
||||
for i in "${!array[@]}"
|
||||
do
|
||||
echo "key: $i"
|
||||
if ${array[$i]}; then
|
||||
echo "$i is true"
|
||||
fi
|
||||
done
|
||||
|
||||
# Output will be:
|
||||
#
|
||||
# key: repo_one
|
||||
# repo_one is true
|
||||
# key: repo_two
|
||||
# repo_one is true
|
||||
# key: repo_three
|
||||
|
||||
# NOTE: Be care of -A versus -a in the array declaration. A -a makes an *indexed* array as opposed to an *associative* array.
|
||||
# But given that an associative array is actually a hash table, it doesn't store the pairs in the order you put them
|
||||
# into the array, they are instead stored in their hash value order. If order matters you need to use an indexed array.
|
||||
# The following shows how to use both an indexed array and an associative array together to get both, if that's what you need.
|
||||
# (Ref https://stackoverflow.com/questions/29161323/how-to-keep-associative-array-order)
|
||||
declare -a arrOrder ; declare -A arrData
|
||||
arrOrder+=( repo_one ) ; arrData[repo_one]=true
|
||||
arrOrder+=( repo_two ) ; arrData[repo_two]=true
|
||||
arrOrder+=( repo_three ) ; arrData[repo_three]=false
|
||||
for i in "${arrOrder[@]}"
|
||||
do
|
||||
echo "$i: ${arrData[$i]}"
|
||||
done
|
||||
@@ -1,8 +1,15 @@
|
||||
#!/bin/bash
|
||||
|
||||
LOGFILE="/my/log/file.log"
|
||||
exec > >(tee "$LOGFILE") 2>&1
|
||||
# This will tee both stout and sterr to terminal and logfile for the entirity of the script.
|
||||
# Useful if you cannot / don't want to wrap the script execution in a redirect at call time.
|
||||
# Stops you having to put '...| tee $LOGFILE 2>&1' on every other line of your script.
|
||||
#
|
||||
LOGFILE="/my/log/file.log"
|
||||
exec > >(tee "$LOGFILE") 2>&1
|
||||
|
||||
# If you have tput colouring, etc, in your scripts for pretty terminal output, then the following
|
||||
# version of the above will strip the control codes so they don't pollute the log file. Not required
|
||||
# if you'll always be looking at the log file with a smart enough viewer, but this isn't always the
|
||||
# case, or you might want additional machine parsing of logs.
|
||||
exec > >(tee >(sed -r "s/\x1b\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" | sed "s/\x0f//g" > $BUILDLOG)) 2>&1
|
||||
# First sed removes the setaf colour codes, second sed is required because despite all other attempts
|
||||
# there remains a leftover code from sgr0 resets.
|
||||
|
||||
12
bash/one_liners.bash
Normal file
12
bash/one_liners.bash
Normal file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# How to strip leading "./" in "find"
|
||||
find -type f -printf '%P\n'
|
||||
# From find manual: %P File's name with the name of the command line argument under which it was found removed.
|
||||
|
||||
# Get a grep command to not find itself
|
||||
<command> | grep '[p]attern'
|
||||
# will find instances of 'pattern' in the output of <command> but not the grep command itself.
|
||||
# Ref: https://stackoverflow.com/questions/20528894/how-does-ps-aux-grep-pattern-exclude-grep-itself
|
||||
# The pattern "[p]attern" will not match the literal string "[p]attern". Instead it will match strings
|
||||
# with exactly one char from the 1-character class "[p]", followed by "attern".
|
||||
@@ -11,8 +11,19 @@ tmux attach -t sessionname # jump into the acti
|
||||
tmux send-keys -t sessionname:0.0 'q' 'C-m' # tell 'top' in top pane to quit (C-m probably not needed here...)
|
||||
tmux kill-session -t sessionname # close tmux session
|
||||
|
||||
|
||||
# NOTE: need the following at the end of 'script.bs' to make it drop out, first two lines are optional but a good way to hold
|
||||
# the session open until user is ready to close it, last line is the kick back to this higher level tmux wrapper.
|
||||
#
|
||||
# echo "Press any key to close the window."
|
||||
# read -s -n1
|
||||
# tmux detach
|
||||
#
|
||||
# If you can also call script.bs NOT from the tmux wrapper, then only do the above if you're actually insidie a tmux.
|
||||
# This works because tmux handily sets the TMUX variable so you can trivially check.
|
||||
#
|
||||
# if [ "$TMUX" ]; then
|
||||
# echo "Press any key to close the window."
|
||||
# read -s -n1
|
||||
# tmux detach
|
||||
# fi
|
||||
53
bash/tput_colours_etc.bash
Normal file
53
bash/tput_colours_etc.bash
Normal file
@@ -0,0 +1,53 @@
|
||||
#!/bin/bash
|
||||
|
||||
# foreground color using ANSI escape
|
||||
|
||||
fgBlack=$(tput setaf 0) # black
|
||||
fgRed=$(tput setaf 1) # red
|
||||
fgGreen=$(tput setaf 2) # green
|
||||
fgYellow=$(tput setaf 3) # yellow
|
||||
fgBlue=$(tput setaf 4) # blue
|
||||
fgMagenta=$(tput setaf 5) # magenta
|
||||
fgCyan=$(tput setaf 6) # cyan
|
||||
fgWhite=$(tput setaf 7) # white
|
||||
|
||||
txReset=$(tput sgr0) # reset attributes
|
||||
|
||||
imma_variable="bar"
|
||||
|
||||
# Example 1: Output Foobar, with empty line before and after, in blue using echo.
|
||||
echo -e "\n${fgBlue}Foo${imma_variable}${txReset}"
|
||||
|
||||
# Example 2: Output Foobar, with empty line before and after, in red using printf.
|
||||
printf '\n%s%s%s%s\n\n' ${fgRed} 'Foo' ${imma_variable} ${txReset} # need extra newline as printf doesn't give one by default
|
||||
|
||||
# Example 3: Set output for multiple lines, then reset after you're done, i.e. not embedded in single output.
|
||||
echo -en "${fgCyan}" # set your colour, without a newline
|
||||
echo "foobar2" # output
|
||||
echo "foobar3" # multiple
|
||||
echo $imma_variable # things
|
||||
echo -en "${txReset}" # and finally reset
|
||||
|
||||
|
||||
# Some other settings I haven't tested - stole straight off Teh Intewebs
|
||||
|
||||
# background color using ANSI escape
|
||||
|
||||
bgBlack=$(tput setab 0) # black
|
||||
bgRed=$(tput setab 1) # red
|
||||
bgGreen=$(tput setab 2) # green
|
||||
bgYellow=$(tput setab 3) # yellow
|
||||
bgBlue=$(tput setab 4) # blue
|
||||
bgMagenta=$(tput setab 5) # magenta
|
||||
bgCyan=$(tput setab 6) # cyan
|
||||
bgWhite=$(tput setab 7) # white
|
||||
|
||||
# text editing options
|
||||
|
||||
txBold=$(tput bold) # bold
|
||||
txHalf=$(tput dim) # half-bright
|
||||
txUnderline=$(tput smul) # underline
|
||||
txEndUnder=$(tput rmul) # exit underline
|
||||
txReverse=$(tput rev) # reverse
|
||||
txStandout=$(tput smso) # standout
|
||||
txEndStand=$(tput rmso) # exit standout
|
||||
87
excel/network_ip_masks
Normal file
87
excel/network_ip_masks
Normal file
@@ -0,0 +1,87 @@
|
||||
A1 = IP address
|
||||
B1 = CIDR netmask
|
||||
|
||||
ref https://gist.github.com/f-steff/d2ef30bed5328f0e417d635d3b46e256
|
||||
|
||||
|
||||
Calculate netmask:
|
||||
==================
|
||||
|
||||
=bin2dec(mid(REPT("1",B1)&rept("0",32-B1),1,8))&"."&
|
||||
bin2dec(mid(REPT("1",B1)&rept("0",32-B1),9,8))&"."&
|
||||
bin2dec(mid(REPT("1",B1)&rept("0",32-B1),17,8))&"."&
|
||||
bin2dec(mid(REPT("1",B1)&rept("0",32-B1),25,8))
|
||||
|
||||
|
||||
Calculate start of IP range (i.e. 'network' address, FIRST HOST is +1 from this):
|
||||
=================================================================================
|
||||
|
||||
=BITAND(
|
||||
(LEFT(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160)))-1))
|
||||
,
|
||||
(bin2dec(mid(REPT("1",B1)&rept("0",32-B1),1,8)))
|
||||
)
|
||||
&"."&
|
||||
BITAND(
|
||||
(MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),1))+1,find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))-find(char(160),SUBSTITUTE(A1,".",CHAR(160),1))-1))
|
||||
,
|
||||
(bin2dec(mid(REPT("1",B1)&rept("0",32-B1),9,8)) )
|
||||
)
|
||||
&"."&
|
||||
BITAND(
|
||||
(MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))+1,find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))-find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))-1))
|
||||
,
|
||||
(bin2dec(mid(REPT("1",B1)&rept("0",32-B1),17,8)))
|
||||
)
|
||||
&"."&
|
||||
BITAND(
|
||||
(MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))+1,len(A2)-find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))))
|
||||
,
|
||||
bin2dec(mid(REPT("1",B1)&rept("0",32-B1),25,8))
|
||||
)
|
||||
|
||||
|
||||
Calculate end of IP range (i.e. 'broadcast' address, LAST HOST is -1 from this):
|
||||
================================================================================
|
||||
|
||||
=BITOR(
|
||||
(LEFT(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160)))-1))
|
||||
,
|
||||
(255-bin2dec(mid(REPT("1",B1)&rept("0",32-B1),1,8)))
|
||||
)
|
||||
&"."&
|
||||
BITOR(
|
||||
(MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),1))+1,find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))-find(char(160),SUBSTITUTE(A1,".",CHAR(160),1))-1))
|
||||
,
|
||||
(255-bin2dec(mid(REPT("1",B1)&rept("0",32-B1),9,8)) )
|
||||
)
|
||||
&"."&
|
||||
BITOR(
|
||||
(MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))+1,find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))-find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))-1))
|
||||
,
|
||||
(255-bin2dec(mid(REPT("1",B1)&rept("0",32-B1),17,8)))
|
||||
)
|
||||
&"."&
|
||||
BITOR(
|
||||
(MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))+1,len(A2)-find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))))
|
||||
,
|
||||
255-bin2dec(mid(REPT("1",B1)&rept("0",32-B1),25,8))
|
||||
)
|
||||
|
||||
|
||||
|
||||
Calculate number of hosts:
|
||||
==========================
|
||||
|
||||
=2^(32-B1)-1
|
||||
|
||||
|
||||
===================================================================================================================================
|
||||
(Does not follow on from above columns.)
|
||||
===================================================================================================================================
|
||||
|
||||
Convert traditional netmask in A1 to CIDR:
|
||||
|
||||
="/"&32-LEN(SUBSTITUTE(TEXT(DEC2BIN(MID(A1,1,FIND(".",A1)-1)),"00000000")&TEXT(DEC2BIN(MID(A1,1+FIND(".",A1),FIND(".",A1,FIND(".",A1)+1)-FIND(".",A1)-1)),"00000000")&TEXT(DEC2BIN(MID(A1,1+FIND(".",A1,FIND(".",A1)+1),FIND(".",A1,FIND(".",A1,FIND(".",A1)+1)+1)-FIND(".",A1,FIND(".",A1)+1)-1)),"00000000")&TEXT(DEC2BIN(RIGHT(A1,LEN(A1)-FIND(".",A1,FIND(".",A1,FIND(".",A1)+1)+1))),"00000000"),"1",""))
|
||||
|
||||
(ref https://www.reddit.com/r/excel/comments/9ivaal/simple_formula_ip_subnet_masks_to_cidr/)
|
||||
3
excel/sexy_currency_formatting
Normal file
3
excel/sexy_currency_formatting
Normal file
@@ -0,0 +1,3 @@
|
||||
[>=1000000]$#.0,,"M";[>=1000]$#,##0,"k";0
|
||||
|
||||
Enter the above as Custom Cell Formatting to get pretty output (BOE style) such as "$1.9M" or "$38k" instead of big integers.
|
||||
15
tmux/input_sync
Normal file
15
tmux/input_sync
Normal file
@@ -0,0 +1,15 @@
|
||||
# Example of a function you could place in .bashrc (or similar) showing synchronised input to multiple panes in tmux.
|
||||
|
||||
function multi_ssh
|
||||
{
|
||||
tmux new-session \; \
|
||||
split-window -h \; \
|
||||
select-pane -t 0 \; \
|
||||
send-keys 'ssh user1@server1' C-m \; \
|
||||
select-pane -t 1 \; \
|
||||
send-keys 'ssh user2@server2' C-m \; \
|
||||
setw synchronize-panes \;
|
||||
}
|
||||
|
||||
# This will give two panes, each one ssh'd into a different server (we've assumed keys setup for auth), and with anything
|
||||
# you now type being entered into BOTH panes simultaneously.
|
||||
Reference in New Issue
Block a user