All of them were written and tested on Gnu/Linux systems. They are or were useful for me, so I'm sharing them in hope they might be useful for someone else.
Unless otherwise noted, they are licensed under WTFPL.
Collection of shell scripts that I've written and that I still use or maintain.
In some scripts (maybe most of them), I've tried to not use bash-specific features. The reason behind this is to make the scripts more portable, and also to make them work on shells other than bash. See also why Ubuntu has changed /bin/sh from bash to dash.
Suppose you've mounted a FAT file system (but it could also be NTFS, a CD/DVD, or basically any file system that does not have Unix-style permissions). Later, you copy some of these files to your ext3 file system (but it could also be ext2, reiser, xfs, or anything with Unix-style permissions). Then you start working with these files, and note that they are read-only, or that all files have the execute bit set (obviously an image or a multimedia file is not executable), or simply that they don't have the permissions you were expecting.
The first approach to this problem is to run chmod -x -R . or maybe chmod 644 -R .. If you try that, you are going to receive errors, because directories without the execute bit won't allow you to access its contents. Then you realise that directories and files should have different permissions, and, thus, that naive recursive chmod won't work.
For this reason I wrote this script. It will recursively apply different permissions to files and directories. It is a simple and quick way to set permissions on the contents of some directory.
This script is called fat2ext because, originally, it was intended to fix permissions when copying from FAT file systems to my ext3 file system, but of course it will work on other file systems too.
This script requires find, and was tested on both bash and dash.
"Fixes" permissions of files and directories by just recursively applying permissions to files and directories. Files will get different permissions than directories.
I've kept previous versions of this script on my disk, and thus I can make older versions available for download. Only the newest version is recommended, the older versions might be interesting to see different solutions for the same problem, and are kept for historical and (dis)educational purposes.
Don't you sometimes want to know the size of a file before downloading it? Well, sometimes I do. This is why I've written this script, years ago.
This script is pretty simple. It just runs wget with parameters to not download the file, but just print the HTTP headers from server. Then, sed extracts the Content-Length: header. Finally, this script prints the size in bytes, kibibytes and mebibytes.
This script was tested on both bash and dash.
Prints the size of a file without downloading it. Only HTTP is supported.
What's the size of Gentoo minimal live-cd for x86?
Sometimes, we run ping just to see if we are losing packets, how many packets are being lost, and when the packets are being lost. The standard ping output is not useful for this, because it doesn't show lost packets. Window's version of ping shows lost packets, but it is still too verbose (one packet per line).
Based on these motivations, I wrote prettyping.sh. It uses awk to parse standard ping output, and replaces the response lines by just one character: a green dot if the response has been received, or a red exclamation point if not. This gives the user a quick and helpful view of which packets are being lost, and how many of them.
Currently, prettyping.sh does not parse any command-line parameters. All parameters are passed directly to the ping program.
This script was tested on both bash and dash.
Makes ping output less verbose, by printing only one colored character for each ping response that has been received or lost.
I've removed the network cable for a few seconds to make me lose some packets.
This time this is a real screenshot. This huge packet loss has actually happened.
Sometimes it is useful to have a program or script to print just the IP address of some interface. Or maybe the IP and the netmask. This type of thing is usually more useful when called from other programs. Unfortunately, looks like there is no such standard tool for this.
Here comes the printmyip.sh script. It parses the ifconfig output and prints only the desired information. It can print the IP address, the netmask, the netmask in numeric format, and the hardware address (MAC address). It can also use an external HTTP service to discover the external public internet IP address, so it's also useful for people behind NAT.
It requires sed, awk (only for numeric netmask) and wget (for external IP discovery). This script was tested on both bash and dash.
Prints the IP address, or IP/netmask, or netmask, or hardware address, for the requested interfaces. Can also print the real external IP address.
The year is 2003... I was still learning Gnu/Linux... I was using Slackware 9.0 and fluxbox. I had wmCalClock in my dock. I just wanted to click on wmCalClock and make it open a calendar for the whole year.
Two manpages later, this script was born. It is so simple that I could embed it right here, instead of offering a download option. Heck, the script has only one line! It just calls cal -y and pipes the output to xmessage.
It was one of my first scripts. Almost 5 years later, I still use it and the code is still the same.
Yes, I know, xmessage is really ugly, but it does its job pretty well. (Note: if you know how to make xmessage prettier, please tell me!)
Should I rewrite this to something more "modern"? I don't know. Is there something as simple, as fast and as useful as this one?
Opens a X window with the calendar for current year.
The terminal I usually use is xterm. However, I don't like the standard default xterm settings. Moreover, many distros (Fedora... argh!) like to modify the standard settings.
Then I wrote this script. It runs xterm with my preferred settings. (Ok, instead of writing this script I could have messed with ~/.Xresources, but this script is a lot more convenient.)
At that time (around year 2005), there was no composite extension, there was no Xgl, there were no compositing window managers. In those days, to get a cool-looking terminal, people were forced to run a terminal with pseudo-transparency support (it used the wallpaper to simulate pseudo-transparency effects). One of these terminals was aterm.
So, in addition to run xterm, this script can also run aterm. And what's more, every time it will run aterm with a different background color tint. Oh... On those days that was cool.
This script works by analysing the name used when running it. bash's [[ command is used to make this easier, thus, bash is required. In addition, this script is completely transparent, because all parameters are passed directly to the terminal program.
To install this script, put it somewhere in the PATH and add a couple of symbolic links (aterm.sh, bigxterm.sh, bigaterm.sh) pointing to the same script. Please read the script source for more instructions and to understand how it works.
Opens a terminal (xterm or aterm) with my preferred settings.
Some shell scripts that are kept here just for historical or (dis)educational purposes. I don't use them anymore and there are no plans to update them. Probably they won't work right now.
I wrote this once in December 2005 because I was curious to know how much time was spent on compiling the entire KDE on a Gentoo system. Since at that time I couldn't find any better way, this script was written in a very quick-and-dirty-and-ugly way.
I used it once, and only once and I don't plan to use it again. By the way, even I want to use it again, I will need to update it (remember, it is quick-and-dirty-and-ugly).
This script uses a hardcoded (quick-and-dirty-and-ugly) list of packages that were, on that time, the direct dependencies of kde-base/kde. It then uses genlop to print the merge time of each one. However, genlop prints that in a human-readable format, very difficult to parse using simple scripts. Then I wrote a quick-and-dirty-and-ugly awk script that tries to parse that output and print the sum.
Nowadays, if I wanted to do calculate the KDE merge time, I would try to use qlop and maybe other tools from portage-utils. At least they can print things in a machine-readable way. Maybe I should also use the solution described at How can I list/unmerge all split ebuilds derived from a given package? section of The KDE Split Ebuilds HOWTO.
Once, long time ago, it calculated the merge time of the entire KDE.
If you use Gentoo you should already know that ebuilds sometimes print useful information while they are being merged. However, since the compilation process takes long time, everybody leaves the computer compiling things and later come back to see the result.
The problem is that these important messages can be easily missed, specially when you are emerging many packages.
If you have taken a look at /etc/make.conf.example, you might have noticed some variables related to logging (being PORTAGE_ELOG_CLASSES and PORTAGE_ELOG_SYSTEM the most important variables). They allow you to select how the ebuild messages should be logged. You can let each package output be saved on a different file, you can make portage mail you the messages, or you can ask portage to run one specific command to log the messages.
Here is where this script comes handy. You can configure portage to run this script to log the messages, and then this script will save them to /var/log/elog_saved.log. Later, when portage finishes emerging all packages, you can just look at that file to read all messages you could have missed otherwise.
This script uses bash, and frankly I don't care if it will work on dash or not. Please read the script comments to learn how to configure /etc/make.conf to use it.
Note: Current portage versions have a module called save_summary that can be used on PORTAGE_ELOG_SYSTEM and that works basically the same way as this script. This module was included into portage on 2006-12-22 and is now enabled by default. This script, however, was written on 2006-09-20, when there was no other solution for this problem. Thus, nowadays this script is not needed anymore.
A script that should be called by portage and will log all package messages to a single file.
I wrote this script because I wanted to convert some video files to 3GP format in order to play them on my mobile phone. It calls ffmpeg with some crazy parameters. Unfortunately, it turned out that only a very small portion of videos could be converted, while many of them failed for unknown reasons (well, unknown to me).
This is a very well-written bash script, with tidy code and help messsage, and it supports many command-line options. Unfortunately, the actual conversion does not work very well. If you know how to fix it, please contact me!
This script calls ffmpeg to convert any video to 3GP, but unfortunately the conversion often fails.
OCReMix (OverClocked ReMix) is a site featuring hundreds of remixes of video game songs. Ormgas is a streaming Internet radio station that plays the remixes from OCReMix.
ocremix_current_songs.php was a CLI PHP script (thus, it is not strictly a shell script) I wrote in 2006-01-16 that extracted information about current song (as well as recently played songs and the next songs) from Ormgas main page. I wrote it because I wanted to keep track of that information, but I didn't want to open a (bloated) browser just to do that.
This script features a html_decode_entities() function. I wrote this function because PHP had no good built-in functions for decoding HTML entities.
This script is abandoned because it is messy, because good PHP code requires tons of lines, and because suddenly this script stopped working and I don't want to fix it.
This used to extract information about current/previous/next songs from ocremix at ormgas.com online streaming radio.
I wrote this on 2004-07-31. It is a gawk script that runs rexima and prints a command-line that will restore the current volume levels. You should, then, store this command-line somewhere else (maybe in some script or alias called restore_volume) and call it when needed. It's pretty simple and should work very well.
However, I put this into abandoned section because nowadays I have no need to run it anymore. This script should still work, but nowadays I use alsactl <store|restore> or /etc/init.d/alsasound <save|restore>.
Prints a command-line to call rexima with parameters to restore current volume levels.
In past, in the first few years of Velox (an ADSL service provided by Telemar, now known as Oi), users were forced to authenticate through a login page before they could actually access the Internet. Of course, this process was boring and annoying, and Gnu/Linux users have automated it.
This script will check if there is an active PPP connection. If not, then it will run adsl-start (nowadays this command has been renamed to pppoe-start). Then it will check if the user is already authenticated (by pinging to some hosts). If not, it will use curl to submit the POST data to the HTTP server. Finally, it can optionally run a firewall script.
This script was not written by me. Manoel Pinho is the original author of this script, and I got this script from a friend. I've modified this script a bit to add a (non-)verbose option, and maybe one thing or another.
I've used this script between 2003 and 2006. During that time, it was run every 5 minutes on my machine, using cron. Then, finally Velox stopped requiring that silly authentication page, and thus this script lost its purpose.
I'm leaving this script here because it was really useful for me during several years (as well as similar scritps were really useful for many people during that time). Maybe this script can be modified to be useful for you, maybe someday it can be useful for someone else, or maybe you can just learn from reading this script (but I must warn you: this script is somewhat verbose and bloated, and it is written in Portuguese). I'm sure I learnt many things while reading and editing this script (e.g. I've discovered that I could submit POST data in command-lines and scripts by using curl).
For some years, it connected to and authenticated at the Velox ADSL service.