The following are some dumps of script bits that are handy to me – often the kind of thing that requires trawling man pages for the little piece of relevant magic. Not all of these are generally useful to visitors, but feel free to use anything you find interesting.
Note that WordPress’s Gutenberg editor seems to screw up bits when pasting, which I’ve tried to fix up. However, there may be errors.
Some miscellaneous Bash stanzas.
# Extract MX hosts from a domain's zone...
MX_HOSTS=$(host -t mx gmail.com|awk '{ print $7 }'|sed -e 's/.$//')
# Get a timestamp and add 24 hours to it...
Add one day to the current date (86400 seconds)
NOW_UNIX=$(( $(date +"%s") + 86400 ))
NOW_DATE=$(date --date="@$NOW_UNIX" +"%_d %B %Y")
# Extract a database field into a shell variable. e.g. a email address
SOME_EMAIL=$(mysql --batch --skip-column-names -e "select email_address from some_users where id=1234 and can_contact='Y'" my_db)
An Awk script to generate form data.
# This program takes the output of ./formfind.pl (which extracts the form # elements from HTML), and generates form-data suitable for using with the # --data option of curl. # # The values are, where needed, url-encoded so that they remain valid when used # as a command line parameter in bash. # # In this example, we're using awk to generate two time windows (t and d), each # with a start and end date that are dynamically generated. Form fields from # formfind.pl are populated with these values ready to give to curl. BEGIN { # Generate ordinal values for characters. for (i = 0; i <= 255; i++) { ord[sprintf("%c", i)] = i } # Set file separator and generate start/end dates for # filling out form fields. FS="\"" now = systime() tstart = strftime("%_d %B %Y", now, 1) tend = strftime("%_d %B %Y", now + 86400, 1) dstart = strftime("%_d %B %Y", now + (86400 * 2), 1) dend = strftime("%_d %B %Y", now + (86400 * 3), 1) form = "some_param=some_value" } # Convert non alphanumerics to their URL-escaped equivalent. # See: https://www.rosettacode.org/wiki/URL_encoding#AWK function escape(str, c, len, res) { len = length(str) res = "" for (i = 1; i <= len; i++) { c = substr(str, i, 1); if (c ~ /[0-9A-Za-z]/) res = res c else res = res "%" sprintf("%02X", ord[c]) } return res } /POST to URL/ { url = $2 } /\(HIDDEN\)/ { form = form "&" $2 "=" $4 } /\(TEXT\)/ { if($2 == "t_start_date") value = tstart; else if($2 == "t_end_date") value = tend; else if($2 == "d_start_date") value = dstart; else if($2 == "d_end_date") value = dend; else value = $4; form = form "&" $2 "=" escape(value) } /\(CHECKBOX\)/ { form = form "&" $2 "=" $4 } /Textarea: / { form = form "&" $2 "=" escape("Some content for a textarea field") } END { print form }
A script to query MySQL or MariaDB tables and columns.
This script (which I call ‘tables’) will list table names, list columns for tables, and find tables by column name. It relies on the awk script that’s listed below the bash script. I name this table_search.awk. You may need to edit the path to this in the bash script.
#!/bin/bash # Bash script 'tables' allows listing and searching of tables and fields DB=mydatabase if [ "$1" == "" ]; then mysql --batch -e 'show tables' ${DB} echo -e "\nUse ${0} to list columns.\n" >&2 echo -e "\nUse ${0} column to find tables with column.\n" >&2 exit fi if [ "$1" == "column" ]; then mysqldump \ --compact \ --no-data \ --no-create-db \ --disable-keys \ ${DB} | awk -f ~/bin/table_search.awk -v name=$2 exit fi TABLES=$(mysql --batch -e 'show tables' ${DB}|grep ${1}) echo -e "\nLooking in database '${DB }' for ${1}\nFound tables:\n${TABLES}\n" for t in ${TABLES}; do echo -e "\nTable ${t}" mysql -e "show columns from ${t}" ${DB} done
The awk script below is referenced in the above bash script, so you may need to edit it’s path depending on where you’re running it. Probably would be better as a here document.
# Awk script 'table_search.awk', referenced by 'tables' bash script.
match($0, "CREATE TABLE `(.+)`", m) { table = m[1]; next }
/\/\*/ || /KEY/ { next }
$1 !~ name { next }
match($0, "`(.+)`", m) { print m[1] "\t" table }