Skip to content

Instantly share code, notes, and snippets.

@timlegrand
timlegrand / prompts.sh
Created September 7, 2015 20:30
Demonstrate Bash prompts, usage and display
#!/bin/bash
read -p "Country? [number]: " country ;
read -p "Layout? [number]: " layout ;
echo "Keyboard: [$country/$layout]"
@timlegrand
timlegrand / Virtualenv Setup
Last active December 2, 2021 17:13
Setup Virtualenv
### Copy-paste until EOF to setup Virtualenv
curl -s https://bootstrap.pypa.io/get-pip.py | sudo python
pip install -U virtualenv
pip install -U virtualenvwrapper
cat >> ~/.bashrc <<EOF
# virtualenv
export WORKON_HOME=~/.virtualenvs
mkdir -p \$WORKON_HOME
source /usr/bin/virtualenvwrapper.sh || source ~/.local/bin/virtualenvwrapper.sh
@timlegrand
timlegrand / dont_source.sh
Last active September 29, 2021 14:12
Check Shell script is sub-shelled, not sourced
# If running interactively
if [ "$PS1" ]; then echo "This script cannot be sourced. Use \"./script.sh\" instead." ; return ; fi
@timlegrand
timlegrand / please_source.sh
Last active September 29, 2021 14:12
Check Shell script is source, not sub-shelled
# If not running interactively
if [ -z "$PS1" ]; then echo "This script must be sourced. Use \"source ./script.sh\" instead." ; exit ; fi
@timlegrand
timlegrand / rsync-with-resume-and-permissions
Last active November 21, 2017 21:43
rsync command for remote backup
#!/usr/bin/env sh
rsync -r -a --delete --append-verify --chown=username:groupname -v --progress --stats mydir username@server:/dest/path/
@timlegrand
timlegrand / writeabledict.py
Last active January 29, 2018 08:34
Simple class to behave like a dict with ability to save and load its data from file
# coding: utf-8
import pickle
import gzip
import collections
class WritableDict(collections.MutableMapping):
'''A dict whose key/value pairs can be saved to/load from file.
Implements the interface of a dict thnaks to Abstract Base Classes'''
# Chitchat (no matter who serves)
machine1# nc -l 12345
machine2# nc <ip> 12345
# Sending a file
machine1# nc -l 12345 < /tmp/fichiersource
machine2# nc <ip> 12345 > /tmp/fichierrecu
# Works in either direction
machine1# nc -l 12345 > /tmp/fichiersource
#!/bin/sh
task(){
start=$(python -c 'import time; print(time.time())')
echo "Running task with parameter $1" ; sleep 1 # to simulate work load
stop=$(python -c 'import time; print(time.time())')
processing_time=$(python -c "print(\"{:.2f}\".format(${stop} - ${start}))")
echo "process $2: $1 processed in ${processing_time} s"
}
@timlegrand
timlegrand / namespace-force-delete.sh
Last active May 9, 2019 07:55
Kubernetes force delete Terminating namespaces
#!/bin/sh
# Start kubectl proxy
jobs &>/dev/null
kubectl proxy 2>/dev/null &
new_job_started="$(jobs -n)"
if [ -n "$new_job_started" ];then
kubeproxy_pid=$!
fi
@timlegrand
timlegrand / benchmark.py
Created January 9, 2020 09:43
Python context decorator for benchmark timing
import contextlib
import time
@contextlib.contextmanager
def report_time(test):
t0 = time.time()
yield
print("Time needed for `%s' called: %.2fs" % (test, time.time() - t0))