Scripts To Rice Your Setup
Introduction
Hey *,
after some positive feedback about my last part and some time, here is the second one. This time we are working with sound…
Author Assigned Level: Hacker
Community Assigned Level:
- Newbie
- Wannabe
- Hacker
- Wizard
- Guru
0 voters
Required Skills
- Linux basics
- Bash basics
Background
I already had the idea to my second script years ago. Some time after playing the Portal games.
Back then I was mainly using Windows, for some reason o_O.
But now to the idea. I really like the sounds the turrets are making.
I am talking about those cute fuckers.
But why not use those sounds and let my pc play them for me? There surely is a time where it fits.
But first I need the sounds, so I googled and found the wiki page which provides all the sound files of the turrets. And even better I found in a forum a zip with all sounds included.
First Implementation
So, under Windows, it was obvious to open up the controls and just change the sounds to the ones I just downloaded and extracted. And we are done.
Now some years later, I came up with this idea again. But this time while using Linux. The problem: in most distros there, are no default sounds or even a control panel for it,
especially not in Arch, which is what I am running.
The solution: we simply script it, because it is Linux we are talking about
I installed mplayer, although every other via console usable player should be right.
Then I looked through the sounds available and picked the ones out, I want to use. And I renamed them, so I could use them via bash. My selection can be found here (I hope this file hoster is okay to use).
And then I wrote myself a (very) little bash script:
#!/bin/bash
RAND=$[ $RANDOM % 20 ]
mplayer /home/lukas/scripts/error/error_$RAND.wav >/dev/null 2>&1 &
So shebang
of course, to say with what we want to run our program.
Then to make things random and not the same every time, a random number in range of the available files.
And then I call mplayer
(or whatever you want to use) with the file name according to the use case, so in this case for error sounds. And then I redirect the terminal output to /dev/null
to not get it in my console every time our script is called. This would be very annoying.
And we want this all to run in the background. So we can continue while our sound is being played and we don’t have to wait. Therefore we put an &
at the end so bash will run it “parallel” in the background.
So my events when a sound should be played:
- on start up
- before shutdown
- on error in terminal
- while find/locate
- while pacman
- while whoami
- before lockscreen (if you got one)
The principal of the scripts and the call of them is not every time the same.
So I guess we start easy with the lock screen. Although not all of you will have one. But I got one, and I am using i3lock.
So for me I got a script which works with some images and gives them then to i3lock and make a fancy looking lock screen.
I did not came up with this code, I just modified it and riced it a bit and used it.
#!/bin/bash
/home/lukas/scripts/screensaver.sh
RAND1=$[ $RANDOM % 9 ]
RAND2=-1;
while [ $RAND1 -eq $RAND2 ] || [ $RAND2 -eq -1 ];
do
RAND2=$[ $RANDOM % 9 ]
done
ICON1=/home/lukas/scripts/logos/asset$RAND1.png
ICON2=/home/lukas/scripts/logos/asset$RAND2.png
TMPBG=/tmp/screen.png
scrot /tmp/screen.png
convert $TMPBG -blur 5x10 -quality 01 $TMPBG
convert $TMPBG $ICON1 -geometry +705+290 -quality 01 -composite -matte $TMPBG
convert $TMPBG $ICON2 -geometry +2300+260 -quality 01 -composite -matte $TMPBG
i3lock -u -i $TMPBG
rm /tmp/screen.png
and notice the call of our script at the beginning:
/home/lukas/scripts/screensaver.sh
We don’t want to wait for the sound to finish playing, it is already taking long enough to lock the screen.
And the screensaver.sh looks like this:
#!/bin/bash
RAND=$[ $RANDOM % 4 ]
mplayer /home/lukas/scripts/screensaver/screensaver_$RAND.wav >/dev/null 2>&1 &
The matching files have to be called screensaver_[index].wav
or the code has to be change accordingly. So has the number of sounds available where we make our random number.
Next up our error sound script.
The code looks almost the same.
#!/bin/bash
RAND=$[ $RANDOM % 20 ]
mplayer /home/lukas/scripts/error/error_$RAND.wav >/dev/null 2>&1 &
But now to the call.
As you see, my prompt is making a little smiley [^_^]
. And that’s because of my configuration of my .bashrc
where I set my prompt according to the success of a command. If I get a positive response ^_^
in green (it isn’t possible to make anything in color with the markdown, is there?), on a failure 0_0
in red.
(disclaimer: also this code is not created by me, I also just used it for my rice)
The code for this looks like this:
PS1='$(if [[ $? == 0 ]]; then echo "\n┌[\e[0;32m^_^"; else echo "\n┌[\e[1;31m0_0"; fi)\e[0m]-[\h]-[\e[0;32;49m\w\[\e[0m]\n└>'
But you might already can guess where this is going.
So this is the code to set the PS1 alias the prompt. And then after echoing 0_0, we simply call our script to make a matching sound:
PS1='$(if [[ $? == 0 ]]; then echo "\n┌[\e[0;32m^_^"; else echo "\n┌[\e[1;31m0_0"; /home/lukas/scripts/error.sh & fi)\e[0m]-[\h]-[\e[0;32;49m\w\[\e[0m] \n└>'
And like this, every time we enter a command the PS1 gets set and we are right there. We will get back to the .bashrc later.
### startup
Now, startup should be really easy. And I took the easiest way available. I used the XFCE session and startup tool, which either is pre-installed with XFCE or comes with the utils package. So if you use a different desktop environment you can go for their program doing this kind of stuff. An other option would be to create a .desktop file by hand or create a daemon or a service which calls our script on startup. Be sure to active the daemon so it will work.
I’m not going into each option, there are tutorials which tell you how to set this up.
That’s how XFCE makes it:
Greet.desktop
[Desktop Entry]
Encoding=UTF-8
Version=0.9.4
Type=Application
Name=Greet
Comment=
Exec=/home/lukas/scripts/startup.sh
OnlyShowIn=XFCE;
StartupNotify=false
Terminal=false
Hidden=false
startup.sh
:
#!/bin/bash
RAND=$[ $RANDOM % 10 ]
mplayer /home/lukas/scripts/startup/startup_$RAND.wav >/dev/null 2>&1 &
### shutdown But now for the shutdown sound, we need to create a service. All other possibilities that I tried didn't work. Either did the system already hang out my drive or the sound drivers aren't working anymore, or the system does not wait for the sound to finish playing. That was probably the hardest sound to play. But I got it working now, and it's really awesome.
Laying in /etc/systemd/system/shutdown.service
:
[Unit]
Description=Shutdown Sound
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/home/lukas/scripts/shutdown.sh
[Install]
WantedBy=multi-user.target
shutdown.sh
:
#!/bin/bash
RAND=$[ $RANDOM % 16 ]
mplayer /home/lukas/scripts/shutdown/shutdown_$RAND.wav >/dev/null 2>&1
!!!Pay attention that we don't want to put an `&` at the end, because then Linux won't wait with shutting down until the sound is completely played!!!
Now the rest and they all work the same way. We got our scripts:
install/pacman
#!/bin/bash
RAND=$[ $RANDOM % 2 ]
mplayer /home/lukas/scripts/install/install_$RAND.wav >/dev/null 2>&1 &
whoami
#!/bin/bash
RAND=$[ $RANDOM % 3 ]
mplayer /home/lukas/scripts/whoami/whoami_$RAND.wav >/dev/null 2>&1 &
locate/find
#!/bin/bash
RAND=$[ $RANDOM % 2 ]
mplayer /home/lukas/scripts/locate/locate_$RAND.wav >/dev/null 2>&1 &
And we write us an alias function in our `.bashrc` or wherever you have your aliases.
#sound
whoami_sound(){
/home/lukas/scripts/whoami.sh
/usr/bin/whoami
}
alias whoami=whoami_sound
locate_sound(){
/home/lukas/scripts/locate.sh
/usr/bin/locate "$@"
}
alias "locate"=locate_sound
find_sound(){
/home/lukas/scripts/locate.sh
/usr/bin/find "$@"
}
alias "find"=find_sound
install_sound(){
/home/lukas/scripts/install.sh
sudo /usr/bin/pacman "$@"
}
alias pacman=install_sound
Every time we call for example whoami, we instead call our function which calls our script and then the command we were calling in the first place. But important, don’t just write the command down, write the full path. Otherwise we create and endless loop, because apparently this counts as normal call.
On command which take parameters we have to give them to our real call too.
Conclusions
So that’s it. You may want to add a flag to regulate the volume or the output device.
I was thinking about making it a real program, like Iron Mans J.A.R.V.I.S for example, but I haven’t had an idea about how I can get all those different events into a single program. Maybe you got a idea and leave a hint in the comments.
I really hope you liked it, and stay tuned there will another part follow, maybe with some hardware included .
``` # .bashrc
Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
#screenfetch
RAND=$[ $RANDOM % 70]
os=(“Alpine Linux” “Antergos” “Arch Linux” “Arch Linux - Old” “BLAG” “BunsenLabs” “CentOS” “Chakra” “Chapeau” “Chrome OS” “CrunchBang” “CRUX” “Debian” “Deepin” “LinuxDeepin” “Devuan” “elementary OS” “Exherbo” “Fedora” “Frugalware” “Fuduntu” “Funtoo” “Fux” “Gentoo” “gNewSense” “Jiyuu Linux” “Kali Linux” “KaOS” “KDE neon” “Kogaion” “Korora” “Mint” “LMDE” “Logos” “Mageia” “Mandriva” “Manjaro” “Netrunner” “NixOS” “openSUSE” “Parabola GNU/Linux-libre” “Pardus” “Parrot Security” “PCLinuxOS” “Peppermint” “Qubes OS” “Raspbian” “Red Hat Enterprise Linux” “ROSA” “Sabayon” “SailfishOS” “Scientific Linux” “Slackware” “Solus” “SparkyLinux” “SteamOS” “SwagArch” “Trisquel” “Ubuntu” “Viperr” “Void” “DragonFlyBSD” “FreeBSD” “NetBSD” “OpenBSD” “FreeBSD - Old” “Unknown” “Haiku” “Mac OS X” “Cygwin”)
screenfetch -w -A ${os[$RAND]}
#—
#PS1 without sound
#PS1=’$(if [[ $? == 0 ]]; then echo “\n┌[\e[0;32m^_^”; else echo “\n┌[\e[1;31m0_0”; fi)\e[0m]-[\h]-[\e[0;32;49m\w[\e[0m]\n└>’
rightprompt() {
printf “%*s” $COLUMNS “<┘”
}
#under construction
#PS1=’[$(tput sc; rightprompt; tput rc)]left prompt > ’
PS1=’$(if [[ $? == 0 ]]; then echo “\n┌[\e[0;32m^_^”; else echo “\n┌[\e[1;31m0_0”; /home/lukas/scripts/error.sh & fi)\e[0m]-[\h]-[\e[0;32;49m\w[\e[0m]-[\A]-[##]-[]-[]---------------------------------┐\n[$(tput sc; rightprompt; tput rc)]└>’
#ps1 and error sound
#without right text
#PS1=’$(if [[ $? == 0 ]]; then echo “\n┌[\e[0;32m^_^”; else echo “\n┌[\e[1;31m0_0”; /home/lukas/scripts/error.sh & fi)\e[0m]-[\h]-[\e[0;32;49m\w[\e[0m] \n└>’
#—
User specific aliases and functions
cd_ls() {
cd $1
ls --color=auto
}
alias cd=cd_ls
alias ls=“ls --color”
alias …=“cd …”
alias …=“cd …/…”
alias makepkg=“makepkg -sirc”
#sound
whoami_sound(){
/home/lukas/scripts/whoami.sh
/usr/bin/whoami
}
alias whoami=whoami_sound
locate_sound(){
/home/lukas/scripts/locate.sh
/usr/bin/locate “$@”
}
alias “locate”=locate_sound
find_sound(){
/home/lukas/scripts/locate.sh
/usr/bin/find “$@”
}
alias “find”=find_sound
install_sound(){
/home/lukas/scripts/install.sh
sudo /usr/bin/pacman “$@”
}
alias pacman=install_sound
And here is my whole `.bashrc` if you want to take a look, maybe at some other aliases.
<hr/>
# Feedback
It is some work for me to write those articles, obviously, but I am also having fun.
Please give me some feedback and improvement suggestions.
Sorry for the long post, here is an ascii APERTURE logo :slight_smile:
.,-:;//;:=,
. :H@@@MM@M#H/.,+%;,
,/X+ +M@@M@MM%=,-%HMMM@X/,
-+@MM; $M@@MH+-,;XMMMM@MMMM@+-
;@M@@M- XM@X;. -+XXXXXHHH@M@M#@/.
,%MM@@MH ,@%= .—=-=:=,.
=@#@@@MX ., -%HX$$%%%+;
=-./@M@M$ .;@MMMM@MM:
X@/ -$MM/ .+MM@@@M$
,@M@H: :@: . =X#@@@@-
,@@@MMX, . /H- ;@M@M=
.H@@@@M@+, %MM+…%#$.
/MMMM@MMH/. XM@MH; =;
/%+%$XHH@$= , .H@@@@MX,
.=--------. -%H.,@@@@@MX,
.%MM@@@HHHXX$$$%± .:$MMX =M@@MM%.
=XMMM@MM@MM#H;,-+HMM@M+ /MMMX=
=%@M@M#@$-.=$@MM@@@M; %M%=
,:+$±,/H#MMMMMMM@= =,
=++%%%%+/:-.