Embedding languages

Shell scripting provides a certain set of features as compared to what we get in other scripted programming languages such as Python, Ruby, Perl, and AWK. These languages provide additional features as compared to what we get in a shell script language. On Linux and UNIX-based system, to use these languages, we have to install them separately if they are not preinstalled.

Consider a simple example: there is a json or XML file and we want to parse it and retrieve the data stored in it. It's very hard and error-prone to do this using shell and its commands, but if we are aware of the Python or Ruby languages, we can easily do it and then embed it into a shell script. Embedding another language in a shell script should be done to reduce the effort and also to achieve better performance.

The syntax for embedding other languages in a shell script is as follows:

Scripting language

The syntax of embedding into a shell script

Python (Python version 2)

python -c ' '. Inside single quotes write the Python code to be processed

Python3

python3 -c ' '. Inside single quotes write the Python version 3 code to be processed

Perl

perl -e ' '. Inside single quotes write the Perl code.

Ruby

ruby -e ' '. Inside single quotes write the Ruby code.

AWK

This can be used as a command utility. Refer to the awk man page for available options.

Embedding Python language

To embed Python language inside a shell script, we will use python -c " Python Code". To learn about Python, refer to the official website at https://www.python.org/.

A simple Python example would be printing Hello World in Python, which is done as follows:

print "Hello World"

To embed this in a shell script, we can write the code as follows

#!/bin/bash
# Filename: python_print.sh
# Description: Embeding python in shell script

# Printing using Python
python -c 'print "Hello World"'

We will now execute the python_print.sh script as follows:

$ sh python_print.sh
Hello World

To embed multiple lines of Python code in a shell script, use the following code:

 python -  <<EOF
# Python code
EOF

Here, python - instructs the python command to take the input from stdin and EOF is a label that instructs to take the stdin input until it encounters the EOF text.

The following example embeds Python language in a shell script and fetches unread e-mails from the user's Gmail account:

#!/bin/bash
# Filename: mail_fetch.sh
# Description: Fetching unread email from gmail by embedding python in shell script

# Enter username and password of your gmail account
echo Enter your gmail username:
read USER
echo Enter password:
read -s PASSWD

echo Running python code
python - <<CODE
# Importing required Python module

import urllib2
import getpass
import xml.etree.ElementTree as ET

# Function to get unread messages in XML format
def get_unread_msgs(user, passwd):
    auth_handler = urllib2.HTTPBasicAuthHandler()
    auth_handler.add_password(
        realm='mail.google.com',
        uri='https://mail.google.com',
        user=user,
        passwd=passwd
    )
    opener = urllib2.build_opener(auth_handler)
    urllib2.install_opener(opener)
    feed = urllib2.urlopen('https://mail.google.com/mail/feed/atom')
    return feed.read()

xml_data = get_unread_msgs("$USER", "$PASSWD")
root = ET.fromstring(xml_data)

# Getting Title of unread emails
print "Title of unread messages:"
print "........................"
count=0
for e in root.iter('{http://purl.org/atom/ns#}title'):
    print e.text

CODE

echo "Done!"

After executing this script, the sample output looks as follows:

$ sh mail_fetch.sh
Enter your gmail username:
[email protected]
Enter password:

Running python code
Title of unread messages:
.....................……………..
Gmail - Inbox for [email protected]
Unread message1
unread message2
Unread message3
Done!

Embedding AWK language

Awk is a programming language designed for text processing and is mainly used for fetching relevant data and for reporting tools. To learn more about AWK programming language, refer to its man page or visit the website at http://www.gnu.org/software/gawk/manual/gawk.html.

The Awk language can be very easily used in a shell script. For example, consider the output of the df command on a running system:

$ df -h
Embedding AWK language

To fetch the fourth column—that is, the Avail field using awk—we can write a shell script using awk as follows:

#!/bin/bash
# Filename: awk_embed.sh
# Description: Demonstrating using awk in shell script

# Fetching 4th column of command df output
df -h |awk '{ print $4 }'
Embedding AWK language

Consider another example in which we will use an input file that will be the /etc/passwd file of a system. This file contains the basic information about each user or account on a Linux or UNIX-based system.

Each line of a /etc/passwd file looks as follows:

root:x:0:0:root:/root:/bin/bash

There are seven fields and each field is separated by a colon (:). To learn the detailed meaning of each field, refer to the Wikipedia link at https://en.wikipedia.org/wiki/Passwd.

The following shell script makes use of awk features and displays some useful information from the /etc/passwd file. For example, we will consider the following as the content of the passwd file:

$ cat passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt

$ cat passwd_file_info.sh	   # Shell script content
#!/bin/bash
# Filename: passwd_file_info.sh
# Desciption: Fetches useful information from /etc/passwd file using awk

# Fetching 1st and 3rd field i.e. Username and UID and separate them with blank space
awk -F":" '{ print "Username: " $1 "	UID:" $3 }' passwd

# Searching line whose user is root
echo "User root information"
awk '$1 ~ /^root/' passwd

Running this script gives following result:

$ sh passwd_file_info.sh
Username: root  UID:0
Username: bin   UID:1
Username: daemon        UID:2
Username: adm   UID:3
Username: lp    UID:4
Username: sync  UID:5
Username: shutdown      UID:6
Username: halt  UID:7

User root information
root:x:0:0:root:/root:/bin/bash  

Note

It is also possible to use compiled languages such as C, C++, and Java in a shell script. To do so, write commands to compile and execute the code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.81.173