Skip to content

Instantly share code, notes, and snippets.

@mlf4aiur
mlf4aiur / bash_skeleton.sh
Last active November 17, 2016 09:10
Bash script skeleton
#!/bin/bash
app_path="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
app_name="$(basename "${BASH_SOURCE[0]}")"
pid_file="${app_path}/run.pid"
VERSION="${app_name} ver. 0.9.0"
function help () {
cat <<EOF
usage: ${app_name} [-h] [-v]
@mlf4aiur
mlf4aiur / json_loads_dump.py
Created February 28, 2014 06:06
json loads dump
graphite_index = json.loads(open('index.json').read())
result = json.dumps(instances, sort_keys=True, indent=4, encoding='utf-8')
@mlf4aiur
mlf4aiur / save-mysql-query-results-into-a-text-or-csv-file.md
Created February 27, 2014 06:32
Save MySQL query results into a text or CSV file

Save MySQL query results into a text or CSV file

Given a query such as

SELECT order_id,product_name,qty FROM orders

which returns three columns of data, the results can be placed into the file /tmo/orders.txt using the query:

SELECT order_id,product_name,qty FROM orders

@mlf4aiur
mlf4aiur / auto_init_instance_var.py
Created May 28, 2013 06:42
Automatically Initializing Instance Variables from _ _init_ _ Arguments
# Recipe 6.18. Automatically Initializing Instance Variables from _ _init_ _ Arguments
# Solution
# You can "factor out" the attribute-assignment task to an auxiliary function:
def set_attributes_from_dict(d):
self = d.pop('self')
for n, v in d.iteritems():
setattr(self, n, v)
# Now, the typical boilerplate code for an _ _init_ _ method such as:
@mlf4aiur
mlf4aiur / httpdate2unixtimestamp.awk
Created May 28, 2013 06:35
Convert http date to unix timestamp AWK function
function httpdate2unixtimestamp(date, array, time_string, timestamp) {
# input: [02/Dec/2011:04:02:42 -0500]
# output: 1322816562
# date is argument
# array, time_string, timestamp is local variable
split(date, array, "[]/: []")
if (array[3]=="Jan") month="01"
else if (array[3]=="Feb") month="02"
else if (array[3]=="Mar") month="03"
else if (array[3]=="Apr") month="04"
@mlf4aiur
mlf4aiur / associative_arrays.sh
Created May 28, 2013 06:32
Bash associative arrays example
# Associative Arrays
declare -A characters
AA="abcdabcaba"
for ((x=0; x<${#AA}; x++))
do
tmp=${AA:$x:1}
((characters[$tmp]++))
done