Skip to content

Instantly share code, notes, and snippets.

View SomajitDey's full-sized avatar

Somajit SomajitDey

View GitHub Profile
@SomajitDey
SomajitDey / abs_path.bash
Last active September 26, 2021 16:11
Bash function to transform any Unix/Windows path to absolute Unix path
abs_path(){
# Transforms any path, including windows paths, given as parameter, to absolute path.
# Expands ~, ~-, \ etc.
# Exitcode: 0 (output to stdout) if path exists, 1 (outout to stderr) otherwise.
local path="${1}"
# Windows to Unix path [if operand is Unix path, then wslpath outputs to stderr]
local buffer=$(wslpath -u "${path}" 2>/dev/null); path="${buffer:-"${path}"}"
eval path="${path}" # Remove backslash, Tilde expansion etc.
[[ "${path}" != /* ]] && path="${PWD}/${path}"
@SomajitDey
SomajitDey / local_git_repo_cleanup.md
Last active September 26, 2021 16:10
Remove old commits and unnecessary objects form local (non-archive) repo to save on disk-space without affecting future updates: push/pulls

Remove old commits from local repo

Shallow fetch from self:

Keep only last n commits--

git fetch --depth=<n-1> . <branch>
@SomajitDey
SomajitDey / prompt.txt
Last active September 26, 2021 16:11
Bash and read -p prompts are sent to stderr...and more info
The readline prompt [read -p] is sent to stderr
The prompt for interactive bash is also sent to stderr. See POSIX mandate below
Source : https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
PS1 :: Each time an interactive shell is ready to read a command, the value of this
variable shall be subjected to parameter expansion and written to standard error.
NOTE (source- Bash reference manual):
@SomajitDey
SomajitDey / dump.bash
Last active March 26, 2021 20:00
Dump input stream at a given path
# This function dumps the input stream to the path given as parameter.
# If PATH gets unlinked by another (cooperative) process, it creates a new file at PATH and continues dump
# Provide a lockfile as the 2nd positional parameter. This is needed so that another process doesn't remove PATH
# when it is being written to.
stream_dump(){
declare -x path="${1}"
local lock="${2}"
local timeout="1" # Interval for polling $path
declare -x line
@SomajitDey
SomajitDey / fg_proc_list.bash
Last active September 26, 2021 16:10
Emulate job control signals from bash script
# First run the following 2 lines in your interactive bash attached to a controlling terminal
export bashpidfile="/tmp/bashpid" # File that stores the bash pid
export PROMPT_COMMAND='echo $$ > $bashpidfile' # Or, replace $$ with $BASHPID
# From another terminal do the following
### TPGID gives fg proc group on the tty the process is connected to, or -1 if the process is not connected to a tty
fgpg=$(ps --pid $(awk NR==1 $bashpidfile) -o tpgid=)
# Send signal SIG to all foreground processes to emulate user Ctrl- inputs
# Ctrl-C: INT, Ctrl-Z: TSTP, Ctrl-\: QUIT
@SomajitDey
SomajitDey / readline_cword.bash
Created April 3, 2021 03:21
Shell-function to give current word of Bash readline, i.e. "read -e". To be used with key-binding for custom TAB completion
### Source me
readline_cword(){
# Gives current word of Bash readline, viz. the word where the insertion point/cursor is currently at
set -- ${READLINE_LINE}
local length=${#1}
while ((length < READLINE_POINT)); do
shift
length+=${#1}
done
echo -n "${1}"
@SomajitDey
SomajitDey / freemium_port_forwarding_services.md
Last active June 11, 2024 23:06
A list of free or freemium services for exposing localhost to internet with a public ip
@SomajitDey
SomajitDey / ngrok_how_to.md
Last active October 14, 2021 18:22
Get free public URL / IP with ngrok
@SomajitDey
SomajitDey / streambin_pubsub_howto.md
Last active April 17, 2021 07:14
exploit streambin.pbedat.de with cURL for instant messaging to remote-access

How to use streambin for event-based workflow : pubsub (publish/subscribe) model

Push based workflow is more efficient than repeated pulls (polling). Thankfully, there is currently a free service for the same which is easy to use : https://streambin.pbedat.de/. There is also emitter.io and free, public MQTT brokers such as broker.hivemq.com (list).

  1. Create a unique channel-key - it may be the hash of your email id or the hash of your MAC address, or may even be the fingerprint of freshly generated private-public keypair.

  2. url="https://streambin.pbedat.de/streams/${channel_key}"
@SomajitDey
SomajitDey / multiline_read.bash
Created April 14, 2021 07:18
UI to input long logical lines that occupy multiple terminal lines through wrapping
while num=$(read -re -p "type: "|& tee /dev/tty|fold -w "$(tput cols)"|wc -l); do tput cuu $num;tput ed; done