Shell programming is fun. But so is Python. People with the background of C, or most importantly people who use windows won’t agree. People often tend to prefer using a graphical based interface than command line ones. But that is so totally wrong. Graphical user interface is for people who think that the software is doing what its description says it does. But that is not always the case. Especially in the case of shell programming windows where viruses and malwares have created a havoc which is the size of a Tornado. So people using a command line interface would usually agree that what they are doing is right, and I do agree with them that they are right.
Many times you would usually get stuck and the whole computer hangs due to a simple Graphic interface based software. Working on a command line interface is often more elegant, fast and most importantly you know what you are executing. But again, using shells and writing programs is not something that everyone would prefer to do.
Besides, writing programs in C or Java would actually be much more worse than using a Graphic user interface based software in windows. I am not cursing C or Java here, its just that they are not suitable for everyday use. And this is where scripting languages like Shell programming and Python languages excel. So, today we are at war here which one is more suitable for smaller as well as larger applications. Or is it that we can actually combine both of them together and make something new and better out of the two. Let’s take a deeper look into both of them and see where it takes us.
Bash Shell Scripting
Shell Scripting is really awesome. You can almost do most of your jobs here in just one line of code. For example to say, take this piece of code:-
$ touch file.txt | echo -e ‘hello\n New\n World\n hello\n Hello’ >> file.txt | cat file.txt | sort | uniq | wc -l |
So, as you can see, a lot of shell scripting is going on here, but what is exactly happening? First the ‘ | ‘ is known as the pipe. It is used to transfer the output of one file to other. The ‘touch’ syntax is used to create any file and the echo syntax is used to print any statement. The double right sided arrows ‘>>’ are used to insert the output to any file or program. ‘cat’ syntax is used to read the contents of a file. Sort is used to sort everything in order and ‘uniq’ is used to remove duplicates from the sorting. The ‘wc’ is used for counting lines and -l is just a flag to print them. So all of these chained commands do the following.
- First a file is created with the name file with extension txt to inform it’s a text file.
- Secondly, echo -e is used to print multiple words inside of the file.txt file and ‘slash n’ or ‘\n’ is used to enter a newline or go to the next line.
- After entering strings into the file, the file is read, sorted out in order and transferred to the uniq syntax to remove any existing duplicates, here which in our case is ‘hello’.
- Finally, we print the number of lines using ‘wc -l’ to count and print it on the terminal screen.
So, though the above is just one line of code, it can tend to get complicated when more conditions get inserted. Thus, shell scripts are good, but they cannot be used for doing new jobs all the time. Sometimes, you want things to be simpler, as simple as a Graphic user interface. Shell scripts are actually very good for running batch scripts and doing repeated jobs, but at the same time they do come with some disadvantages:-
- Shell scripts tend to get extremely complicated as they become large.
- Scripts reusage are almost none. What I mean here is that, bash scripts are extremely hard to insert in C or other Java codes.
- Advanced features such as HTML parsing are not easily available for bash.
However, there is Python here for the rescue, and the most suitable replacement for the same. But is Bash Shell scripting totally replaceable? Lets take a look.
Recommended courses
Python Shell Scripting
Python is installed by default on almost all Linux and UNIX systems. Sometimes the distribution may have an older version, but it can be updated by just a simple command. Python Shell programming is much more easier to understand and cleaner to write even for beginners. Also, by default Python has the style of Read Eval Print Loop which helps in trying out new codes in the interpreter. But just using python can be a bit tough without the help of bash shell scripting. Besides, in our previous one line program, we used the ‘uniq’ syntax to remove duplicates, but it did not show us what the duplicates were. So, lets write a python program to do the same:-
4.8 (7,864 ratings)
View Course
#!/usr/bin/env pythonimport sys#importing system modules to work with directory filesif __name__ == “__main__”:# Starting with an empty dictionary here. Which is termed as the order# All keys in this dictionary appear as a name and the specified values for them# will be the amount of times that specific name will appear.order = {}# sys.stdin is an object used for files. All those functions that’s being applied to
# a file object can also be used for sys.stdin. for order in sys.stdin.readlines(): order = order.strip() if order in orders: orders[order] += 1 else: orders[order] = 1 for order, count in orders.iteritems(): sys.stdout.write(“%d\t%s\n” % (count, order)) |
Now, this file first reads the input from the sys.stdin object. All the output is written to the sys.stdout object. After creating this script, assuming that you have named it as order.py, you can just execute the following shell script in the terminal and you will find the total count of all the duplicate strings.
$ cat file.txt | python order.py |
Similarly, we can also sort this out by using th sort syntax:-
$ cat file.txt | python order.py | sort -rn |
Python Shell vs Bash Shell Programming Scripting
Now we have seen how we can combine python and shell scripts to create a chain of commands and execute them together. Now, let’s take a step further and see whether Python can totally replace Bash Shell.
Speaking of bash shell programming, in terms of performance, bash totally beats the crap out of python. But if you compare it to data types and other advanced stuff, bash doesn’t have much compatibility. The start-up time of a bash shell script is 2.8 mili seconds while that of python is 11.1 mili seconds. To be more frank, bash is a general purpose language just like Python, but both have their own strengths and weaknesses. Bash shell programming is the default terminal in most Linux distributions and thus it will always be faster in terms of performance. But does that mean it can totally replace Python? Nope. When dealing with large programs, Bash will keep on getting complicated whereas Python does not. Python can also be used as an Object oriented language as far as I know. If you are just a beginner, then you might not even know the difference between the two. Python is the most elegant scripting language, even more than Ruby and Perl. Bash shell programming on the other hand is actually very excellent in piping out the output of one command into another.
Shell Scripting is simple, and it’s not as powerful as python. It does not deal with frameworks and its tough to get going with web related programs using Shell Scripting. The real power of shell scripting lies in the Stream Text editor or sed, the Awk Programs and similar apps.
File Handling and Web Application Development
Bash Shell Scripting works flawlessly and fast when dealing with files. By handling files, I mean copying, cloning disks, writing backup apps for networking, ftp servers, storing file inputs and accessing them later and transferring those outputs later on to something else with the help of pipe. Python on the other hand is more useful for dealing with chunks of data such as reading data from a file and processing data. If I be more specific, bash is not even a programming language. It’s more of a simple shell which was targeted towards dealing with system files using the command line in order for the process to be more fast and quick.
Thus, if you know bash shell programming properly, then you also know that variables and scopes in bash extremely limited. Python on the other hand is more of a shell scripting language than programming language. If we term Bash as a programming language, its more like telling Python is an object oriented shell programming language. One can deal with python in an object oriented way, but it’s never gonna be purely object oriented like C or Java. Similarly, Bash though is a scripting language; it’s more of better used as something which is required to deal with files quickly rather than writing large programs in it. Also, though Python is a shell scripting language, it actually deals within its own shell. For example, moving all file which is on Desktop to some other random directory via Bash shell would go something like this:
$ cd Desktop$ mv * randomDirectory |
But on the other hand, doing the same thing in python is a bit more complicated. It goes something like this: –
import os, globfor fname in glob.glob (‘*’):os.rename (fname, ‘randomdirectory’) |
However, at the best, one can make it the most compact in this manner: –
import os, glob[ os.rename (fname, ‘randomdirectory’) for fname in glob.glob ( ‘*’ ) ] |
When learning any language, people will often tell you how to get the basics clean and all those stuff, but they won’t tell you how to deal first with the system itself which is the most important. And besides, Python and Bash both are Shell scripting language which means both of them are developed mainly to deal with the system and its files.
Bash inside Python?
Yes, you read it right. Till now we have read about how we can introduce Python scripts inside of Shell. But we can also introduce Shell Scripts within the scripts of python. Take this for example: – Type in ‘apt-get update’ in the python shell? You will obviously get some error like invalid syntax. So, the thing is if you want to use the bash codes like ‘ls, cd, mv’ or anything else in python, you will have to import the OS module. Now, do one thing, copy the following code and paste it into the Shell of python, and check how it works:-
$ python>>>from os import *>>> system ( ‘sudo apt-get update’ ) |
And now it works…yay. This is another way as to how to get python and bash scripts working together. Besides, there is another python module which is specifically built in order to work with bash. This module is known as pexpect. Pexpect is a python module which is used for spawning child processes, controlling them and returning results as per the child process’s request. The pexpect module has an extremely easy interface to import required modules and child processes inside of it and execute them accordingly. One just needs the pip to be installed in their system to install pexpect and one can install the module as follows: –
$ pip install pexpect |
The version of Pexpect needs python version greater than 3.3 or specifically 2.7.
There is actually much more to mixing up python and bash and nothing works as better as a mixture of script of Python and Bash. This mixture makes the script fast because of Bash and inclusive of advanced features because of Python. One can read about pexpect and its documentation at https://pexpect.readthedocs.org/online.
Recommended Articles
Here are some articles that will help you to get more detail about the Bash Scripting and Python so just go through the link.