Useful listing of days in a month to populate directories or run scripts for backing up getting the correct dates of days in a month and year. YEAR=2020 MONTH=2 for DAY in $(DAYS=`cal $MONTH $YEAR | awk ‘NF {DAYS = $NF}; END {print DAYS}’` && seq -f ‘%02G’ $DAYS) ;do DATE=”$YEAR-$MONTH-$DAY” echo $DATE…
Tag: Bash
Snapshot backup using cp -al and rsync
This script requires genuine cp -al capable gnu and rsync commands together with a hardlink capable FS + OS. #!/bin/bash [ $# -ne 2 ] && echo “Incorrect usage : $0 <source path> <target path>” && exit 128 ; SOURCEFOLDER=$1 TARGETFOLDER=$2 SF_LEN=${#SOURCEFOLDER}-1 TF_LEN=${#TARGETFOLDER}-1 #echo “Last character in source folder is ${SOURCEFOLDER:SF_LEN}” if […
Wait for jobs in the background and measure time taken
SECONDS=0;sleep 5 & sleep 6 & sleep 3 & sleep 7 & wait ; elapsedseconds=$SECONDS ; echo $elapsedseconds nJoy 😉
test Graylog GELF UDP input from Bash
for i in {1..100} ; do echo '{"version": "1.1","host":"david.org","short_message":"A short message that helps you identify what is going on","full_message":"Backtrace here\n\nmore stuff","level":1,"_user_id":9001,"_some_info":"foo","_some_env_var":"bar"}' | nc -w 1 -u graylog.mydomain.com 12201 ; done nJoy 😉
Script to verify that a path is synchronized across multiple machines via ssh
This is a script I wrote for work to look through a number of remote servers via ssh (shared keys or include a .pem (id_rsa) file to compare a paths and all it’s subfolders. The result is a report of which files are out of synch and if duplicates found they are listed separately. #!/bin/bash…
Bash test if port is open no external tools
Assume you do not have curl/telnet/netcat (or nc does not support -z as in later versions ?? why did they remove it ?? ) the following will work on any recent Bash version. (echo > /dev/tcp/skinner/22) >/dev/null 2>&1 && echo “Port is open” || echo “Port is closed” nJoy 😉
Using document here in Bash script for sending a message to all who is loaded.
#!/bin/bash wall <<zzz23EndOfMessagezzz23 E-mail your noontime orders for pizza to the system administrator. (Add an extra dollar for anchovy or mushroom topping.) # Additional message text goes here. # Note: 'wall' prints comment lines. zzz23EndOfMessagezzz23 # Could have been done more efficiently by # wall <message-file # However, embedding the message template in a…
Sample parameter validation script.sh
#!/bin/bash echo "`basename $0` Tool" [[ $# != 3 ]] && echo "Invalid number of arguments" && echo "Sample Call : create_ftp.sh <numeric_partner_id> <username_alphanum> <password_alphanum>" && exit 128 #number re re='^[0-9]+$' if [[ $1 =~ $re ]] ; then PARTNERID=$1 echo "Partner id set to $1" else echo "Invalid param #1 should be numeric" echo…
Decoding parameters in Bash script (1 space delimited)
This is a small script that takes a bash command and decodes the parameters in non sequential order except definers. #!/bin/bash if [ "$#" -ne 1 ] || ! [ -d "$1" ]; then echo "Usage: $0 " >&2 exit 1 fi while [[ $# > 1 ]] do key=”$1″ echo $key shift case $key…
Replacing a piece of XML with awk
This script searches for an initial tag and and closing one and replaces the content. # !/bin/bash awk ‘ BEGIN {pr = 1;one = 0} /<Name>OPENING<\/Name>/ {pr = 0;} { if (pr) print } { if (!pr && !one) {print (“\t\t <Name>OPENING</Name> \n \t\t\t <Value>false</Value> \n \t\t<Type>STOPHERE</Type> \n ” ); one =1 ;}} /<Type>STOPHERE<\/Type>/ {pr…