Shell programming is fun. But so is Python. People with a background in C, or most importantly, people who use windows, won’t agree. People often tend to prefer using a based graphical interface to command-line ones. But that is so wrong. The graphical user interface is for people who think the software is doing what its description says. But that is not always the case, especially in the case of shell programming windows, where viruses and malware have created havoc the size of a Tornado. So people using a command-line interface usually agree that what they are doing is right, and I agree with them that they are right.
You would often 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, importantly, you know what you are executing. But again, using shells and writing programs is not something everyone would prefer to do.
Besides, writing programs in C or Java would be much worse than Graphic user interface-based software in windows. I am not cursing C or Java here; it is just that they are not suitable for everyday use. And this is where scripting languages like Shell programming and Python languages excel. Today we are at war on which one is more suitable for smaller and larger applications. Or is it that we can combine both of them 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 awesome. You can almost do most of your jobs here in just one line of code. For example, 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 happening here, but what exactly is happening? First, the ‘|’ is known as the pipe. It is used to transfer the output of one file to other. The ‘touch’ syntax creates files, and the echo syntax prints any statement. The double right-sided arrows’>>’ are used to insert the output to any file or program. ‘cat’ syntax reads a file’s contents. Sort is used to sort everything in order, and ‘unit’ removes 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 text to inform it’s a text file.
- Secondly, echo -e is used to print multiple words inside the file.txt file, and ‘slash n’ or ‘\n’ is used to enter a new line or go to the following line.
- After entering the file’s strings, the file is read, sorted out in order, and transferred to the uniq syntax to remove any existing duplicates, which in our case is ‘hello.’
- Finally, we print the number of lines using ‘wc -la to count and publish it on the terminal screen.
So, though the above is just one line of code, it can get complicated when more conditions are 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 perfect 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 significant.
- Scripts reusage is almost none. Bash scripts are tough to insert in C or other Java codes.
- Advanced features such as HTML parsing are not readily available for Bashash however, Python is here for the rescue and the most suitable replacement. But is Bash Shell scripting replaceable? Let us take a look.
Python Shell Scripting
Python is installed by default on almost all Linux and UNIX systems. Sometimes the distribution may have an older version, but a simple command can update it. Python Shell programming is easier to understand and cleaner to write, even for beginners. Also, by default, Python has the Read Eval Print Loop style, which helps try out new codes in the interpreter. But just using PPython can be a bit tough without the help of bash shell scripting. Besides, in our previous one-line program, we used the ‘unique’ syntax to remove duplicates, but it did not show us what the duplicates were. So, let us write a python program to do the same:-
#!/user/bin/env python import sys#importing system modules to work with directory files if __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 number of times that specific name will appear.order = {}# sys.stdin is an object used for files. All those functions 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)) |
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 you have named it order.py, you can execute the following shell script in the terminal, and you will find the total count of all the duplicate strings.
$ cat file.txt | PPython order.py |
Similarly, we can also sort this out by using the 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. Let’s take a step further and see whether Python can replace Bash Shell.
Speaking of bash shell programming, in terms of performance, Bashash the crap out of Python. But if you compare it to data types and other advanced stuff, Bashash doesn’t have much compatibility. The start-up time of a bash shell script is 2.8 mili seconds, while that of PPython is 11.1 mili seconds. Bash is a general-purpose language like Python, but both have strengths and weaknesses. Bash shell programming is the default terminal in most Linux distributions; thus, it will always be faster in terms of performance. But does that mean it can replace Python? Nope. When dealing with large programs, Bashash gets complicated, whereas Python does not. As far as I know, PPython can also be used as an Object-oriented language. If you are just a beginner, you might not even know the difference between the two. Python is an even more elegant scripting language than Ruby and Perl. On the other hand, Bash shell programming is excellent in piping out the output of one command into another.
Shell Scripting is simple, and it’s not as robust as Python. It does not deal with frameworks, and it’s tough to get going with web-related programs using Shell Scripting. The 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 a pipe. On the other hand, Python is more beneficial for dealing with chunks of data, such as reading data from a file and processing data. If I am more specific, Bashash is not even a programming language. It’s more of a simple shell targeted toward dealing with system files using the command line for the process to be faster and quicker.
Thus, if you know bash shell programming properly, you also know that variables and scopes in Bashash are minimal. On the other hand, Python is more of a shell scripting language than a programming language. If we term Bashash programming language, it’s more like saying Python is an object-oriented shell programming language. One can deal with PPython in an object-oriented way, but it will never be purely object-oriented like C or Java. Similarly, Bashash is a scripting language; it’s better used as something required to deal with files quickly rather than writing large programs in it. Also, though Python is a Shell scripting language, it sells within its Shell. For example, moving all file which is on the Desktop to some other random directory via Bash shell would go something like this:
$ cd Desktop$ mv * random directory |
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 (name, ‘random directory’) |
However, at best, one can make it the most compact in this manner: –
import os, glob[ os.rename (name, ‘random directory’) 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 are both Shell scripting languages, which 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 get some errors 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 PPython, and check how it works:-
$ python>>>from os import *>>> system ( ‘sudo apt-get update ) |
And now it works…yay. This is another way how to get python and bash scripts working together. Besides, another python module is specifically built to work with Bashash. His module is known as Pexpect. Pexpect is a python module used to spawn child processes, control them, and return results per the child process’s request. The Pexpect module has a straightforward interface to import required modules and child processes inside of it and execute them accordingly. One needs the pip to be installed in their system to install expect, and one can install the module as follows: –
$ pip install expect |
The version of Pexpect needs a python version greater than 3.3 or 2.7.
There is much more to mixing up PPython and Bash. Nothing works as better as a mixture of the script of Python and Bash. This mixture makes the hand fast because of Bash and 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 get more details about Bash Scripting and Python, so go through the link.
- Python vs. JavaScript
- Python Interview Questions
- Python vs. JavaScript Differences
- R Programming vs. Python
40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses