Post your bash scripts, oneliners and aliases

Post your bash scripts, oneliners and aliases.
zsh hipsters don't need to apply.

chsh -s /usr/bin/fish

>using the superior shell is being a hipster

this is to grab torrent files with transmision daemon without needing the gtk version, just put this on a file and make an open with
#!/bin/sh
/usr/bin/transmission-remote --add "$1"

create torrents
#!/bin/sh
/usr/bin/transmission-create -o "$1" -t "udp://tracker.openbittorrent.com:80" -t "udp://open.demonii.com:1337" "$2"

QEMU script to install new distros
#!/bin/bash
# Author: svchost
# Date: 08.27.2016
# Purpose: QEMU loading
# License: GPLv3

qemu-system-x86_64 -m 2G -hda $1 -cdrom $2 -boot d

update and clean the system
#!/bin/bash
# Author: svchost
# Date: 05/08/2016
# Purpose: Clean and update Devuan
# License: GPLv3

sudo apt-get autoclean; sudo apt-get clean; sudo apt-get autoremove; sudo apt-get update; sudo apt-get upgrade; sudo apt-get dist-upgrade

bind it to the super key, use ratpoison, and youll have a quick and easy window switch
#!/bin/bash
# Author: svchost
# Date: 05.08.2016
# Purpose: dmenu window switch
# License: GPLv3
ratpoison -c "select `ratpoison -c "windows %t" | dmenu -nf gray -nb black -sf black -sb gray -b -l 20`"

>zsh hipsters don't need to apply
Could someone explain me this? What's wrong with zsh? It feels a lot better that bash to me. I tried to switch back to bash some days ago and I noticed how tons of useful features and arguments completions are missing or needs to be enabled manually.

>licensing the list of arguments you pass to a program
JUST

#!/bin/sh -e

if [ $# -eq 6 ]
then
ffmpeg -y -ss $2 -i "$1" -vf scale=-2:$4 -an -sn -c:v libvpx -threads 4 -quality best -b:v $5k -pass 1 -f webm -t $3 /dev/null
ffmpeg -ss $2 -i "$1" -vf scale=-2:$4 -an -sn -c:v libvpx -threads 4 -quality best -b:v $5k -pass 2 -t $3 "$6-$4p-$5k.webm"
else
echo "usage: $(basename "$0") FILE START LENGTH RESOLUTION BITRATE NAME" >&2
false
fi

Yes I know, it's not a bash script.

So say I have a long command I always type out to open a program, something like:

[TerdFerguson@Nebuchadnezzar ~]$ bash Builds/somefile/bin/someprogram.sh

I want to be able to type in a short word that automatically does that. I think alias is what I want to do but how do I go about it? Tried some stuff already in .bashrc file but it doesn't want to work. For extra info, I'm using arch with i3.

I think it's just like most stuff on here, bash is all he's ever used used, so he dislikes and belittles the alternatives. It's just how things work in here.

What's the significance of the $ in "-ss $2" and "-vf scale=-2:$4" and "-b:v $5k"

I've used ffmpeg to create webms and grab frames into a file, but I'm not at all proficient with automation/scripts in bash, or really any terminal for that matter.

That tends to be how things are everywhere. If some person uses ____ product from ____ manufacturer, they will find ways to justify their choice even if they basically choose it at random to begin with. Kind of like how idiots obsess over Ford vs Dodge based on their parents owning a Ford or based on them buying one or the other for their first vehicle. People like to identify themselves with something, and then use that to argue with others who are "different".

Read the usage line. Everything is explained.

Well that's true, though I think it's more pronounced here since the overall tone is often more confrontational (on internet in general and on Sup Forums in particular).

What is that?

write the script in a file with the #!/bin/bash in the first line, make it executable, put it in your bin directory

>What is that?
My script to compress webm for Sup Forums. I could use for anything, but I compress webm only for Sup Forums.

makes me feel like I am an actual programmer ;^)

two little scripts I put in my .xbindkeys file and bind it to print and scroll lock keys respectively:

take screenshot
import -window root png:$HOME/xwz_$(date "+%Y-%m-%d-%H:%M:%S").png

record desktop
killall -INT avconv 2>/dev/null || avconv -f x11grab -r 25 -s 1280x720 -i :0.0 $HOME/output.webm &

recording the desktop is shitty though, haven't fixed the video quality

#!/bin/sh

tmp=`mktemp`
list_explicit=`mktemp`
pacman -Qe > $list_explicit
list_groups=`mktemp`
pacman -Qg base base-devel | cut -d " " -f2 > $list_groups

while read line
do
grep -vw $line $list_explicit > $tmp
mv $tmp $list_explicit
done < $list_groups

less $list_explicit
rm $list_explicit $list_groups

compress/extract multiple subdirectories
#!/bin/bash

case $1 in
-h | "" )
echo "Usage: bulktar.sh "
echo " -c: compress subdirectories into separate .tgz files"
echo " -x: extract .tgz files"
exit
;;
-c )
for i in */ ; do
echo "Compressing '$i' ..."
tar -pzcvf "${i%/}.tar.gz" "$i"
done
;;
-x )
for i in *.tar.gz ; do
echo "Extracting '$i' ..."
tar -zxvf "$i"
done
;;
esac


gif to webm
#!/bin/bash

for i in *.gif ; do
ffmpeg -i "$i" -pix_fmt yuv320p -c:v libvpx -crf 12 -b:v 500K "$(basename "${i/.gif}").webm" && rm $i
done


fix permissions (644 for files/755 for folders)
#!/bin/bash

read -r -p "You sure? $1 [y/N] " response

if [[ $response =~ ^([yY])$ ]]
then
find $1 \
\( -type f -exec chmod 644 {} \; \) , \
\( -type d -exec chmod 755 {} \; \)
else
echo "Aborted..."
exit 0
fi

I use mksh.

A few aliases that help me save keystrokes:

alias up="cd ../"
alias upp="cd ../../"
alias uppp="cd ../../../"
alias upppp = "cd ../../../../"

alias pls="sudo"

>alias pls="sudo"
I love you

>fix permissions
You should do it like that.
chmod -R a-st,go=u-w "$1"
is probably better

learning brash expansion rules and learning the order of expansions will open up the world. bash4 or zsh especially have so many niceties these days it's great

- quote your variables
- use && instead of ; since it doesn't make sense to run the other programs when the previous failed

nice

>killall -INT avconv 2>/dev/null ||
That's genious. Saves me lots of enable/disable hotkeys. Thanks for the idea.

quote variables, use $() instead of ``
nice

Is it possible to use "chmod -R" to force all subdirectories to always have the x bit set whenever the r bit is set -- without adding any x bits to regular files?

I've always had to use "find" for that because I have different rules I want to apply to directories versus regular files, regarding the x bit.

I understand your problem. But except if you did a mistake all the directories must have the x bit set. So you don't really need a script for that.

>use $() instead of ``
Why? `` is more legible for me.

It's old, deprecated syntax which is sadly still around. It's also better when you want to nest things. Check the bash hackers wiki for more info.

>quote your variables
I guess you mean "%t"? k
>use && instead of ; since it doesn't make sense to run the other programs when the previous failed
roger that

thanks, glad to know is useful

Post the alias?
Also bashrc gets sourced only on startup
Or you need to reload it with source ~/.bashrc