36
votes
Share your useful shell scripts!
Disclaimer: Don't run scripts offered to you by randos unless you trust them or review it yourself
I use this constantly, it just plays music by file name, specifically matching *NAME*
with case-insensitivity. Requires bash 4.something.
# play -ln SONGS ...
# -l don't shuffle
# -n dry run
mpv_args="--no-audio-display --no-resume-playback \
--msg-level=all=status --term-osd-bar"
shopt -s globstar nullglob nocaseglob
shuffle=true
dry=false
while [[ "$1" == -* ]]; do
if [[ "$1" == "-l" ]]; then
shuffle=false
elif [[ "$1" == "-n" ]]; then
dry=true
fi
shift 1
done
if [[ "$shuffle" == true ]]; then
mpv_args="--shuffle $mpv_args"
fi
songs=()
while [[ "$#" != 0 ]]; do
songs+=( ~/music/**/**/*"$1"*.* ) # change this to match your music directory layout
shift 1 # could probably use find instead
done
if [[ "$dry" == true ]]; then
if [[ "$shuffle" == true ]]; then
printf "Shuffle mode is on\n"
fi
for song in "${songs[@]}"; do
printf "$song\n"
done
exit
fi
if [[ ${#songs[@]} != 0 ]]; then
mpv $mpv_args "${songs[@]}"
fi
I make no claims to the quality of this but it works!
I have been on fish for a few years too, and it makes writing shell scripts much better. I have a bunch of scripts, but I think one of my favorite/most used is my
network
command. Basically just a quick overview of your network connection. It does require a little program I wrote to make it pretty, but that would be really easy to remove. Alsojq
to show the GeoLocation.Huh, today I learned my terminal supports the ANSI blink code (
\e[5m
). Time to make a really annoying MOTD using this newfound knowledge.Psst, wrap your code in triple backticks.
```
echo hello world
```
formats as
This can be done in shell fairly easily too:
base64 /dev/urandom | head -c 16
. At least I think that's an equivalent, I'm not too sure of these things.103 day old edit: see Emerald_Knight's comment below for a 'URL safe' version :)
Also, a version for OpenBSD (a little long, there's no
-c
for head):I'm way late to the party on this, but I just wanted to clarify on this topic:
The Python
secrets.token_urlsafe()
function and the Linuxbase64
command have slightly differing character sets. Both use base64 encoded bytes, but the Linuxbase64
uses the standard 62 alphanumeric characters with+
and/
making up the final 2 characters, whereas the Pythonsecrets.token_urlsafe()
also uses the standard 62 alphanumeric characters but uses-
and_
as the final 2 characters instead. This is important because the Python solution is inherently safe for inserting directly into a URL as a token (hence, the function is calledtoken_urlsafe()
), whereas the pure shell solution uses the base64 standard that pretty much everyone else uses which ensures interoperability.They're basically equivalent, but just different enough that it can matter. For any system where you're just using it as a password and the website doesn't disallow those particular special characters, they may as well be considered identical.
If, however, you're concerned about them being different, it's a simple matter of extending your existing solution:
This is a rather old thread, but I have a few helpful bash aliases I have set up that I thought I'd share, with some annotations to explain them:
The
saferm
one is really useful for me in production environments where symlinks are set up inside of individual release directories and point to shared configuration files or multimedia assets. Withoutsaferm
, you have to ensure that you remove all symlinks manually before invokingrm -rf
on the released git tag, which is obviously error prone. It also ensures that I don't have to unlink each symlink by hardcoding them individually inside of a script, instead finding all of them dynamically for me.Most of these are just there to reduce the mental overhead associated with remembering what the exact commands and sequences of those commands are and the arguments and options I need to pass to them, and to reduce the amount of repetition involved in typing them out. When not a massive headache, I also like to try to make my aliases composable through piping.
I'm a fairly inexperienced shell scripter, to be honest, but I try to improve what I have over time. Even my worst scripts and aliases still make my workload a lot easier to manage :)
I just found a new one that's really helpful:
This alias takes advantage of
git diff
functionality to directly compare two directory structures for any differences, regardless of whether or not they're tracked by git. The drawbacks of this are that 1. it requiresgit
, which not everyone needs, and that 2. files that shouldn't really be compared, e.g. those under.git/
, will end up being diff'd. Still, I found it pretty handy :)I run Herbstluftwm as my window manager, and like to keep the color scheme in sync with what I have defined in my Xresources file. I wrote this little nugget to pull the colors from xrdb and define local variables which can be used throughout my autostart script.
There's probably a better way, but this does the job for me.
Hey, does anyone have a script that I can invoke like this?
I use SSH port forwarding a lot and would love a reliable script that lets me open an SSH session only for the duration of another command.
Cool, will this kill my SSH session when bash is done? (assume the new command doesn't fork a background process or something)
awesome, thanks!