
You know, I've never found any real use for a shell script. Recently, I found an actual reason to write one for moving and converting audio files using a few less keystrokes.
I admit it, I'm really a desktop user who doesn't mind getting under the hood every now and then. I enjoy playing with the 'big' toys, like RDBMSes because I can afford to. But mostly, using GNU/Linux affords me an opportunity to do what I need and want to do for a price that can't be beat. Much as I've tried, the only shell scripts I've written in the past were pretty contrived, or simply following some tutorial. Now I have a shell script I can actually make use of.
I'm not going to try to teach Bash scripting here. There are plenty of places where you can learn that. LinuxCommand.org is one. The Advanced Bash-Scripting Guide is another. But I do want to make the argument that even non-technical people ought to consider taking a little time to learn something about the Bash shell and how to write a simple script.
For those that don't already know, I'm using gNewSense, a GNU/Linux distribution that aims at being completely libre, on my laptop. But at least one site I use offers some content only in RealMedia or MP3 format. Unfortunately, MP3 is hampered by patent issues, and I prefer to avoid that. So I started downloading the MP3 files and converting them to Ogg-Vorbis, and then deleting the MP3 files. But that requires downloading the files, moving them into the folder I want, and then converting and deleting them.
First, let me explain the need to move the files. Most of the time, I put my downloaded files in one folder, and choose a different folder for multimedia files by choosing "Save link as" from the context menu. In this case, the site uses a dialog that makes this approach impossible. So, after I download the file, it's a matter of dragging it to the right place in Konqueror, or typing the appropriate command at a Bash prompt. Then I have to run mp32ogg using Bash. And, of course, I have to delete the MP3 file when I'm finished. Using a shell script simplifies this task.
The whole command-line sequence to accomplish my task involves three commands: 'mv', 'mp32ogg', and 'rm'. Of course, in the text file I created, I added the standard initial comment lines:
#!/bin/bash
# Move MP3 files from /downloads to /media/mp3 and convert to Ogg-Vorbis format
mv downloads/*.mp3 media/mp3
mp32ogg media/mp3/
rm media/mp3/*.mp3
So then I saved the file and made it executable. Now, instead of typing those commands, I just open a Bash prompt and type the one new command I created, like this, after downloading the file:
dw_convert
So, now I download the file, type the new command, and play it Amarok.
This may seem a bit contrived. After all, why not just download the MP3 codec, or use a distribution that supports MP3 out of the box? I've already answered that - there are legal issues that lead me to avoid MP3. The site I use has not responded yet to my request for Ogg-Vorbis files, and avoiding the site is detrimental to my goals, unfortunately.
The other reason this is a good example, rather than a bad one, is that, in the Free/Open Source Software community, there are numerous ways to do any particular task. So, while some newbies or non-technical folks might prefer the path of least resistance, and choose a distro that supports MP3 out of the box, this example demonstrates that you can make up new commands as you go (within reason, of course) to simplify what might seem like an otherwise daunting task.
A more typical task for shell scripting is automating your back-ups or auto-configuring your wifi card at boot-time, depending on your location. Blue GNU readers will surely be able to offer other examples. But I hope that non-technical people will be able to see better why anyone would even bother trying to write a shell script.
Note:As has been pointed out in the comments, this script needs some error checking and could probably be greatly enhanced.
Comments
Any command can be a script!
Any command, or series of commands, can be a script.
That's certainly not all there is to know about scripting, but it's just about all that I know, and just that has allowed me to improvise some inelegant but effective automation solutions.
Here's a drastically simplified and incomplete version of a script I used to use to sort downloaded files according to their extension:
mv *.mp3 ~/audio
mv *.jpg ~/images
mv *.avi ~/video
mv *.pdf ~/documents
The actual script used keywords surrounded by wildcards
mv *[rR]olling*[sS]tones*.mp3 ~/audio/Rolling_Stones
to sort some files into specific subdirectories, leaving other files in the main directories to be sorted manually. The more specific commands would go higher in the list, so that they can be removed from the list before the more general items.
mv *[rR]olling*[sS]tones*.mp3 ~/audio/Rolling_Stones
mv *[fF]rontline*.avi* ~/video/documentaries
mv *[Ll]inux*.pdf ~/documents/tech
mv *.mp3 ~/audio
mv *.jpg ~/images
mv *.avi ~/video
mv *.pdf ~/documents
It wasn't perfect. Bob Dylan's recording of "Like a Rolling Stone" might wind up in the directory ~/audio/Rolling_Stones. If I had needed the results to be perfect (I didn't) some visual checking would have been necessary.
But it was pretty close to perfect, saved me hours of the most tedious cutting and pasting, and the point here is that all it took was basic knowledge of one very basic command.
The script used absolute and not relative paths, and that meant that this script would produce the same results from wherever I ran it in my system.
Well, this isn't a tutorial on how to use my dumb script. It's an example of how the command line makes it possible for a little bit of ingenuity to make a little bit of knowledge go a long long way. The point is that if you know how to use the command line, you can combine commands in a text file to make a script, use the script to automate the commands, and save yourself a lot of work.
Incidentally, it seems that it's always the most boring, mechanical tasks that lend themselves to simple command line automation.
This is why I say that it is the command line that turns Linux users into fanatically devoted fanboys. Not only because it saves time, but because it introduces an element of creativity into solving everyday computings tasks, and that makes computing fun.
UNIX Clerks
In the years before Microsoft indoctrinated everyone, us dumb office computer users used the shell commands to do a lot of our work, like typing letters with vi and embedding nroff macros in the text, having a look at the wysiwyg output, before sending it off to a printer or typesetter, or using shell scripts with awk and plot commands to look at our data and print pretty charts, or using refer to catalogue our magazine articles. Some of us used S equations to analyse scientific information and print out graphical representations.
Nobody thought it was difficult, especially after some self learning with the UNIX learn system.
Has MS actually made people dumber?
It is not laziness, as often a few quick shell commands are faster, more versatile and more productive than using two or three GUI apps to do the same job as you point out in the article.
Well, if you re-read the
Well, if you re-read the article, perhaps you'll notice that I pointed to LinuxCommand.org, where one can not only learn about shell scripting, but also about using the command line. It's in the 3rd paragraph, in blue type. It's a pretty darned good resource. :-)
There is also RUTE: http://rute.2038bug.com/index.html.gz
D.C. Parris
Publisher, Blue Gnu
http://www.linkedin.com/in/dcparris
https://www.xing.com/profile/Don_Parris
How Should Non-Technical Users Begin?
Interesting article, but how do you propose non-technical users begin learning to write a shell script? Once you know a few command, a shell script isn't that large of a step, but I would assume that non-technical users aren't exactly using the command line anyways.
I've written a few shell scripts for my moms laptop. One played a slideshow to music. The other is for her backup, it mounts her samba share on a Linksys Network Storage Link (NSLU2) we have in the basement, rsyncs her home directory to it, and then unmounts. There's a launcher in a scripts drawer on her panel that she can click to run the backup.
She can use that, but I don't see how she could have authored the script herself.
My girlfriend on the other hand is comfortable issuing some commands on the terminal with some guidance, but not comfortable enough to do it on her own, nevermind to author a shell script.
I agree that it's good for non-technical users who are interested to learn more about their computers though, but I'm not sure where most can start.
How Should Non-Technical Users Begin?
Interesting article, but how do you propose non-technical users begin learning to write a shell script? Once you know a few command, a shell script isn't that large of a step, but I would assume that non-technical users aren't exactly using the command line anyways.
I've written a few shell scripts for my moms laptop. One played a slideshow to music. The other is for her backup, it mounts her samba share on a Linksys Network Storage Link (NSLU2) we have in the basement, rsyncs her home directory to it, and then unmounts. There's a launcher in a scripts drawer on her panel that she can click to run the backup.
She can use that, but I don't see how she could have authored the script herself.
Even nicer!
A nicer approach is to use the --delete of mp32ogg (it also avoid the removal of the MP3 files if the conversion is not properly achieved):
#!/bin/bashmp32ogg --delete ~/downloads/
mv ~/downloads/*.ogg ~/media/mp3/
By the way, calling "mp3" a directory containing only ogg files is kind of funny!
Notice that this is the kind of script you love placing in a crontab executed, let say, hourly. Maybe for a future article. ;-)
Typo is fixed. @ rstanley
Typo is fixed.
@ rstanley - Thanks for pointing that out. Maybe I'll work on that and post back the results.
D.C. Parris
Publisher, Blue Gnu
http://www.linkedin.com/in/dcparris
https://www.xing.com/profile/Don_Parris
One other problem
You are assuming that everything works fine, and that no errors have occurred.
If there was a problem converting the mp3 files, then you have deleted them without any ogg files being created. Some error checking is in order, and I would process them one at a time. COPY one, convert, then delete if OK, THEN go back and delete from the downloads.
typo
Isn't the line in your example above missing the "mv" command?
Shouldn't it be
mv downloads/*.mp3 media/mp3
?
Good write-up though. Shell scripts are always handy, even if one doesn't get deeply into the uber-nerdy regular expressions.