Skip to content

Instantly share code, notes, and snippets.

@swarminglogic
swarminglogic / watchfile.sh
Last active March 4, 2024 14:44
watchfile - monitor file(s) and execute a command when files are changed
#!/bin/bash
version=1.0.1
versionDate="2014-02-14"
function showHelp() {
echo "watchfile - monitor file(s)/command and perform action when changed
Possible ways of usage
----------------------------------------
@swarminglogic
swarminglogic / nicekill.sh
Last active November 7, 2023 00:03
nicekill: A bash script to terminate a process in the order of SIGTERM, SIGINT, SIGHUP, SIGKILL, giving each signal 2 seconds to enact.
#!/bin/bash
# $1: PID
# Returns true if a process exists with pid=PID
function isProcessAlive {
if [ `ps -p $1 | wc -l` -gt 1 ] ; then
return 0;
else
return 1;
fi
@swarminglogic
swarminglogic / getoeis.sh
Created August 15, 2013 14:44
Bash script to crawl the OEIS database, and get the number of entries for each integer. See http://swarminglogic.com/articles/2013_08_oeis for more information.
#!/bin/bash
[[ ! "$1" == "-" && ! "$1" == "+" ]] && echo "Invalid sign paramter [-|+]" && exit;
[[ "$1" == "-" ]] && suff="neg";
[[ "$1" == "+" ]] && suff="pos";
cntfile='cntfile_'$suff
[[ ! -e $cntfile ]] && echo '0' > $cntfile;
file='values_'$suff
@swarminglogic
swarminglogic / recordscreen.sh
Last active November 25, 2022 06:45
recordscreen: A bash script for timelapse recordinging two separate X displays, and creating a picture-in-picture overlay with one display
#!/bin/bash
if [ ! $# -eq 2 ] ; then
echo "Usage: ./recordscreen delay outputfolder"
exit
fi
if [ ! -d $outputFolder ] ; then
echo "Output folder does not exist"
exit
@swarminglogic
swarminglogic / SmartObjectPool.h
Last active June 26, 2022 14:18
SmartObjectPool: A RAII-style implementation of the object pool pattern that uses smart pointers to automatically return acquired objects to the pool when deleted.
#include <memory>
#include <stack>
#include <stdexcept>
template <class T, class D = std::default_delete<T>>
class SmartObjectPool
{
private:
struct ReturnToPool_Deleter {
explicit ReturnToPool_Deleter(std::weak_ptr<SmartObjectPool<T, D>* > pool)
@swarminglogic
swarminglogic / git-root
Last active June 14, 2019 08:44
git-root: finds git root directory without using git executable (POSIX compliant shell script)
#!/bin/sh
#$1: Path to child directory
git_root_recurse_parent() {
# Check if cwd is a git root directory
if [ -d .git/objects -a -d .git/refs -a -f .git/HEAD ] ; then
pwd
return 0
fi
@swarminglogic
swarminglogic / vlcopen.sh
Last active December 13, 2018 22:25
Download video URL (youtube-dl supported), and automatically open with VLC as soon as possible. Assign to global hotkey for easy use: (1. Copy URL. 2. Hit hotkey shortcut. 3. Wait a few seconds. 4. Enjoy in VLC)
#!/bin/bash
if [ $# -eq 1 ] ; then
path=$1
else
clipboard=$(xclip -selection clipboard -o)
if [ $(<<<$clipboard grep -P "^http") ] ; then
path=$clipboard
else
if [ -t 1 ] ; then
@swarminglogic
swarminglogic / gifcapture.sh
Last active June 5, 2018 20:22
gifcapture -- A linux tool to record a region of the screen and convert it to a well optimized gif.
#!/bin/bash
delay=0.2
title='default'
count=0
loop=1 #0 for infinite loop
# Set if you want to skip mouse selection
if [[ $# -eq 6 ]]
then
@swarminglogic
swarminglogic / ttic.sh
Last active December 28, 2017 11:22
Millisecond resolution tic / toc timer pair utility for linux terminal. Supports id-based tic/toc pairs.
#!/bin/bash
function showHelp {
version=0.0.1
versionDate="2014-07-07"
echo "$0 - tic/toc timer pair
Usage: $0 [id] Stores initial time (w/optional id marker)
$0 [-u|--unique] Creates and returns unique id
@swarminglogic
swarminglogic / enterpass.sh
Last active May 9, 2016 15:55
Fills out username/password. Triggered by global hotkey. Application agnostic. Determines best user/pass match from process information. Requiers userpass.sh (https://gist.github.com/swarminglogic/40922ce92e49aae3b2ca)
#!/bin/bash
# Use this with a global hotkey to the following: gksudo [FULLPATH]/enterpass.sh
if [[ $EUID -ne 0 ]] ; then
notify-send "This script must be run as root"
exit
fi
if ! command -v userpass > /dev/null ; then