Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Shell scripting and it's benefits

Shell scripting and it's benefits

Whilst I'm not the biggest user of Bash, I do use it at least everyday for lots of jobs. I learned a lot from a friend but also from experimenting a lot whilst being an OS X user. In this post I'm going to discuss the main benefit of Bash for me. 

The first notable benefit of Bash is automation. Since Bash is relatively easy to access and will not need compiled it's easy to automate certain things. One of the things I use Bash for is it's find command. This allows me to list all files which match a certain name. Using this command I can run an exec command on it and do things like move them or delete them.

But, you ask, I've got a Mac with a graphical interface so why do I not just search using the Finder search tool? This is very true but I'm not able to do that on my Raspberry Pi which is not connected to any display am I? No. What I do here is the second notable benefit of Bash - remote login. 

Remote login is generally handled by SSH for me. This fantastic tool lets me manage my Raspberry Pi remotely from my Mac but it also gives me access to the server from which this website is being hosted on. Couple the two aforementioned features of Bash and I can manage my website server using my Mac remotely. Not only does this give me more access around the server but it gives me speed.

Say I want to delete 1,000 files which are in a directory of directories, I would need to recursively delete them. With FTP, I would request the deletion of the whole directory and my FTP client (FileZilla) would send a REMOVE command to the server. However, before it can do that it would need to go into each sub directory, recursively (meaning it would need to visit each sub directory of that sub directory too) and send a REMOVE command on each file inside of it and then on the empty directory itself. The FTP client needs to check each directory out for any files which it requests from the server each time. This is a long and cumbersome task. For me it takes no time though, because I know Bash. 

Using Bash the deletion of remote files can be sped up considerably. Once I've SSH'ed into my server and I wish to remove a directory and it's contents it's fairly simple:

rm -r test_dir

In the above example I'm removing the test_dir directory and it's contents. Since Bash sends a single one line command to the server which the server then interprets and executes using its own Bash interpreter, the execution time for the above example is less than one second. This is an astronomical improvement over the FTP client. 

So there you have it. My favourite reason for using Bash. 

I will criticise Bash over one thing however, and I guess I think of myself as a bit of a snob when it comes to programming languages since I feel that a lot of languages have ugly syntax and I do not feel that Bash is any different. For example if statements do not use the normal < or > signs to represent their comparators. No, in Bash this is done with the -lt and -gt words. Then you've got the whole thing with variables where instantiating a variable does not require a $ but referencing one does. 

This ugly syntax has often deterred me from using Bash to do something. My own ZPE aimed to fix a lot of this and has, just without the horrid syntax of Bash. It's worth a look if you want a simple scripting syntax and powerful language all in one. 

bash
shell
scripting
Linux
unix
macOS
OS X
Comments
Powered by DASH 2.0