Created
June 6, 2022 09:48
-
-
Save shaybensasson/b72276672a20ea235dbb5cab08c6b08f to your computer and use it in GitHub Desktop.
Some useful bash functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# A script with functions to add to .bashrc | |
# Uploads a file to transfer.sh | |
# see https://transfer.sh/ | |
# usage: transfer hello.txt | |
# requirements: curl | |
transfer() { | |
if [ $# -eq 0 ]; then | |
echo "No arguments specified.\nUsage:\n transfer <file|directory>\n ... | transfer <file_name>" >&2 | |
return 1 | |
fi | |
if tty -s; then | |
file="$1" | |
file_name=$(basename "$file") | |
if [ ! -e "$file" ]; then | |
echo "$file: No such file or directory" >&2 | |
return 1 | |
fi | |
if [ -d "$file" ]; then | |
file_name="$file_name.zip" , | |
(cd "$file" && zip -r -q - .) | curl --progress-bar --upload-file "-" "https://transfer.sh/$file_name" | tee /dev/null, | |
else cat "$file" | curl --progress-bar --upload-file "-" "https://transfer.sh/$file_name" | tee /dev/null; fi | |
else | |
file_name=$1 | |
curl --progress-bar --upload-file "-" "https://transfer.sh/$file_name" | tee /dev/null | |
fi | |
} | |
#custom function, opens a process outside the process, no logs | |
# https://unix.stackexchange.com/a/524176/244364 | |
function open_detached () { | |
nohup xdg-open "$*" > /dev/null 2>&1 | |
} | |
#custom function, loads .env file env vars | |
#https://gist.github.com/mihow/9c7f559807069a03e302605691f85572#gistcomment-2721971 | |
function load_env () { | |
set -o allexport; source "$*"; set +o allexport | |
} | |
#include first line in grep | |
#https://stackoverflow.com/a/9969881/1640414 | |
function grep1() { awk -v pattern="${1:?pattern is empty}" 'NR==1 || $0~pattern' "${2:-/dev/stdin}"; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment