14 minute read

Have Fun with the Script Command

Open Gurus Let's Try

Have Fun with the Script Command

Advertisement

The command line is one of the salient features of GNU Linux and UNIX. GNU/Linux provides many useful command line utilities and ‘script’ is one of them. It is available by default on most GNU/Linux distributions. So explore its possibilities and try out some script recipes that should be fun.

The ‘script’ command makes a typescript of everything printed on a terminal, including error messages, linefeeds and backspaces. Once it is started, it works in the background. As you continue to work normally, the ‘script’ session dumps everything from the terminal to the log file. It is useful when you want to show someone how to perform a particular task from the command line. Script stores typescript in plain text format. For certain interactive commands, for instance, vi/vim, it creates garbage characters in typescript. It works best with the commands that do not manipulate the screen.

Upon start-up the script command forks off a new interactive shell—if the SHELL environment variable is set then that shell is used, otherwise the default shell is bash (for GNU/Linux). The script session ends when the forked-off shell exits (either by pressing Ctrl+D or typing the exit command).

Command line syntax

Shown below is the command line syntax for the script command.

script [-a] [-c command] [-e] [-f] [-q] [-t[=file]] [-V] [-h] [file]

To start a script interactive session, type the following:

$ script [file]

The file name is optional. If the file name is provided, it stores all dialogues in the ‘file’; otherwise, dialogues are stored in the ‘typescript’ file.

Let us look at how to use the command with the help of a simple example.

$ script # Start script session. Script started, file is typescript # Welcome message. $ hostname minty $ uname Linux

$ lsb_release -i Distributor ID: LinuxMint $ exit exit Script done, file is typescript # End script session.

# Goodbye message.

Now, let us look at the contents of the ‘typescript’ file.

$ cat typescript Script started on Monday 17 February 2014 09:51:34 PM IST $ hostname minty $ uname Linux $ lsb_release -i Distributor ID: LinuxMint $ exit exit

Script done on Monday 17 February 2014 09:51:51 PM IST

Yes, indeed, it stores everything from the terminal.

Command line options

Script provides many command line options to control its default behaviour. Let us see how this is done, one by one, with examples.

By default, the script command shows welcome and goodbye messages upon start-up and exit, respectively. We can suppress these messages by using the ‘-q’ or ‘—quiet’ option.

$ script -q # Observe there is no welcome message. $ hostname minty $ uname Linux $ exit # Also no goodbye message. exit

We can order the script command to store terminal typescript into ‘file’ instead of the default ‘typescript’ file. But here’s a catch; what if ‘file’ already exists and has useful data. By default, script will overwrite ‘file's’ content. Naturally, we don't want this behaviour, so let’s override this default behaviour by using the ‘-a’ or ‘—append’ option. Instead of overwriting, the append option appends the output to the file and retains its prior contents. The example below explains how the append option works.

Let’s suppose we have a file ‘imp.log’ with the following contents:

$ cat imp.log This file contains very important data.

Let us use the ‘imp.log’ file to store the terminal typescript with the append option.

$ script -a imp.log Script started, file is imp.log $ hostname minty $ uname Linux $ exit exit Script done, file is imp.log

Now observe the contents of the ‘imp.log’ file.

$ cat imp.log

This file contains very important data. Script started on Monday 17 February 2014 10:39:58 PM IST $ hostname minty $ uname Linux $ exit exit

Script done on Monday 17 February 2014 10:40:07 PM IST

Let’s congratulate ourselves! We didn't lose any important data.

Instead of providing an interactive shell, we can instruct Script to execute commands. The ‘-c’ or ‘—command’ option will do this job. As we are not executing the commands interactively, the log file will contain only the output of the commands and not the commands themselves. The example below will give you a better idea about the ‘—command’ option:

$ script -qc "hostname; uname" minty Linux

Let us look closely at the contents of the ‘typesript’ file.

$ cat typescript # Observe that file contains output of the "hostname" and "uname" command respectively. Script started on Monday 17 February 2014 10:52:21 PM IST minty Linux

The script command ends its session by quitting the child shell gracefully. That is why it returns a zero exit code. We can use the ‘-e’ or ‘—return’ option to fetch the exit code of the child process. It is useful for error handling. The example below returns the correct exit code upon failure:

$ script -q -e $ ls invalid_file # Provide non-existing file name. ls: cannot access invalid_file: No such file or directory $ exit exit $ echo $? # Check exit status. 2

The ‘-t’ or ‘—timing’ option dumps timing data to the standard error stream. This timing data contains two spaceseparated fields. The first field indicates how much time has elapsed since the previous output, and the second field indicates how many characters were printed this time. Sounds confusing? For now, just remember that it stores some timing information. Later on, in the script recipes section we will look at how this information is useful to play back terminal sessions.

When we invoke the script command with the‘-f’ or ‘— flush’ options, it flushes the output after each write operation. In the script recipes section, we will see how to leverage this behaviour to broadcast terminal sessions.

Script recipes

We are now familiar with the script command and we have also looked at a few examples. Let us now discuss a few recipes related to this command.

Recipe 1. Record and play back terminal sessions: We can record the order and timing of the command into a plain text file. Later on, we can revisit it by replaying it. Timing information ensures that the output happens at the same speed as it originally appeared when the script was recorded. Trust me, you will be amazed at what you see.

To start the terminal recording, execute the following command:

$ script -q -a output.log -t 2> timing.log # Redirect "stderr" to store timing data in file. $ hostname minty $ uname Linux $ lsb_release -i Distributor ID: LinuxMint $ exit exit

Now replay the recording by using the ‘scriptreplay’ command. Both the ‘—typescript’ and ‘—timing’ options are self-explanatory.

$ scriptreplay --typescript output.log --timing timing.log

Recipe 2. Speed up the replay display: The‘scriptreply’ command provides the ‘-d’ or ‘—divisor’ option, which is used to speed up the replay display by the number of times specified.

$ scriptreplay --typescript output.log --timing timing.log --divisor 4

The above command will speed up the replay display by four times.

Recipe 3. Broadcast the terminal session using script

and named pipes: We can achieve good tele-cooperation by taking the following simple steps. 1. Open two separate terminals (it would be better if you use terminals instead of tabs). 2. In the first terminal, create a named pipe and invoke the

Script command with the ‘-f’ or ‘—flush’ options:

$ mkfifo broadcast.fifo $ script -f broadcast.fifo

3. Switch to the second terminal and perform the ‘cat’ operation on the named pipe:

$ cat broadcast.fifo

4. Switch back to the first terminal and type a few commands. You will observe that anything typed on the first terminal will be broadcast to the second terminal. To stop broadcasting, quit the script session.

By Narendra Kangralkar

The author is a FOSS enthusiast and loves exploring anything related to open source. He can be reached at narendrakangralkar@gmail.com.

CIGNEX Datamatics’ Open Business Model is Designed to Succeed

There is a school of thought that questions the sustainability of a successful open source business model. But FOSS has proved to be the cash-spewing machine for CIGNEX Datamatics, a global leader in open source enterprise solutions, as almost 100 per cent of its revenue comes from developing and supporting open source solutions in portals, content and Big Data analytics.

The open source team at CIGNEX Datamatics

The spirit of open source is indeed heady as hundreds of enterprises are not only expanding their businesses but also augmenting their massive revenue bases with the help of FOSS. Abiding by its punch line ‘Making Open Source Work’, CIGNEX Datamatics (a subsidiary of Datamatics Global Services Ltd) continues to be bullish about its business prospects in the open source domain. For the last few years, the company has been growing steadily at a compound annual growth rate (CAGR) of over 50 per cent due to the increasing adoption of open source solutions and services among enterprises.

Since its inception in 2000, the company has developed solutions for enterprises using industry-standard open source technology products. Being a systems integrator for leading open source products has helped the company to build the right solutions for its clients’ systems in order to enhance profitability. Based in Santa Clara, USA, CIGNEX Datamatics boasts of 22 offices in nine countries, including India, with Ahmedabad as its India headquarters.

Munwar Shariff, co-founder and chief technology officer at CIGNEX Datamatics, helps us understand the company’s ‘open’ journey to success. Not many know that

“Our motivation for taking up open source as a business model stems from the business reality that there are

Munwar Shariff, co-founder and chief technology officer at CIGNEX Datamatics more than 200,000 open source software options for an organisation to choose from, but only the right partner can help it minimise the risk and reduce adoption time, while ensuring performance, scalability, high availability, quality and security; so that’s where we fit in.”

he has co-authored five technical books—Plone Live, Implementing Alfresco, Alfresco 3 WCM, Alfresco 3 ECM and Alfresco 4 ECM Implementation.

“With over 13 years of experience and 400+ open source solutions, we have gained a vast experience in enabling enterprise clients to address their technology challenges and business needs through our tailor-made open source solutions (OSS). Our solutions enable them to make smart architecture-related decisions through a feasibility study, accelerate time-to-market through proven open source based product frameworks; and enable coexistence with proprietary solutions by the integration of applications and more. We also reduce the enterprise’s risk in dealing with open source software through our open source adoption model (OSAM), best practices, project governance, and knowledge transfer,” explains Shariff.

The profitability mantra

It’s not often that companies figure out how to use open source to reinforce their businesses but CIGNEX Datamatics was quick to realise its potential.

The benchmark for a successful open source enterprise solutions player lies in how efficiently it is able to leverage its partnerships with leading open source technology platforms, believes Shariff. “We have forged strategic partnerships with commercial open source technology leaders for portals (Liferay Platinum partner), content management (Global Alfresco Platinum Partner), Big Data (MongoDB Advanced Partner, Cloudera Hadoop Partner) and e-commerce (Magento Silver Partner). In Liferay, we have implemented over 200 enterprise solutions across all verticals, are a certified Liferay training partner, authored seven books and contributed over 4000 posts in various blogs, forums, wikis, etc. Likewise, we have authored five books on Alfresco and delivered over 80 implementations across various industries, worldwide,” says Shariff.

The courage to go open

Unless you firmly believe in the potential of FOSS, you will think twice about adopting open source as a viable business model. So what motivated CIGNEX Datamatics to go open? “Demand for open source has gained considerable momentum among enterprises in recent years, not only due to its cost benefits but also the greater freedom of choice it offers, complete visibility into all aspects of an application, and consistent improvements in the user experience. Our motivation for taking up open source as a business model stems from the business reality that there are more than 200,000 open source software options for an organisation to choose from, but only the right partner can help it minimise the risk and reduce adoption time, while ensuring performance, scalability, high availability, quality and security; so that’s where we fit in,” says Shariff.

The business case for open source

The company’s clients include leading names among Fortune 500 enterprises as well as government agencies.

So are most of CIGNEX Datamatics’ customers adopting open source solutions along with proprietary applications or are they replacing them completely? And how does it work—does CIGNEX approach a firm and convince it to try open source solutions or is it the other way round? “It’s really a mixed bag. We have adequate cases where we really had to evangelise the benefits of open source solutions. But many times, clients were already aware of its benefits and approached us directly as we have a sizeable number of implementations and reference case studies in developing customised solutions using Liferay, Alfresco, Drupal, Magento, and more. In most cases, we see a co-existence between proprietary solutions and our open source solutions because of the immense interoperability between the two,” explains Shariff.

“As an organisation, we are highly sensitive to the needs of the open source community and go out of our way to give back to it. For instance, CIGNEX Datamatics has been actively contributing to the Liferay community with over 4000 posts in various forums, blogs and wikis, and members of our team have been invited to numerous Liferay events as keynote speakers.”

According to Shariff, the company’s robust partnerships with leading open source technology platforms play a key role in bringing new business. The company has documented cases where CIOs approached it after consulting analyst reports such as ‘Gartner Magic Quadrant for Horizontal Portals, 2013’, in which Liferay is rated as a ‘Leader’; or ‘Forrester Wave for Enterprise Content Management, 2013’, in which Alfresco is reported as a ‘strong performer’. “We are able to get repeat business from our clients as our solutions and services overcome specific business challenges. For example, CIGNEX Datamatics helped a leading Mexicobased hotel group, Groupo Posadas, achieve its business and technology goals with an innovative, integrated hospitality portal solution (Liferay + Alfresco) reducing the TCO by 40 per cent. In another key example, we developed a Big Data solution to improve productivity for a leading European manufacturing company by enabling a 10 times better search performance, at a cost that was 20 times lower compared to proprietary solutions.”

The company had helped a mobile carrier to manage the life cycle of serving videos on mobile devices at a rapid speed (5 million/hour) with huge data volumes. This was not possible earlier due to high costs and low scalability. Now it is possible with big data

Community involvement

Shariff firmly believes that any business built around the open source framework has to eventually fall back on the global open source community for support. “As an organisation, we are highly sensitive to the needs of the open source community and go out of our way to give back to it. For instance, CIGNEX Datamatics has been actively contributing to the Liferay community with over 4000 posts in various forums, blogs and wikis, and members of our team have been invited to numerous Liferay events as keynote speakers. Like I mentioned earlier, our team members have authored seven books on Liferay expertise, and regularly conduct training sessions and workshops in various countries, including India, as we are a certified training partner for Liferay. We have been awarded the ‘Liferay Community Excellence Award’ in 2011, 2012 and 2013 as a result of our contribution to the Liferay community,” says Shariff.

The company has also developed frameworks such as the Open Contract Manager—a contract lifecycle management system that is available for free download.

The vision

The company’s immediate plan is to maximise its market presence in the portals and Big Data solutions business. “We want to continue with our innovation through new solutions built on open source frameworks, including mobile portals built on Liferay, RAPIDO (a content management publishing framework), Open Contract Manager, a mobile media site using Drupal and MongoDB, etc. Our vision is to be amongst the largest open source companies and to become a US$ 100 million company by 2015, with the stated aim to improve enterprise productivity through the adoption of open source solutions,” quips Shariff.

According to Shariff, open source solutions have come of age in the enterprise space and have been experiencing robust growth in the past few years. Gartner predicts that by 2016, almost 99 per cent of the Global 2000 firms will be using some form of open source software.

And what is Shariff’s advice to nascent entrepreneurs who are hesitant to adopt the open source business model? “We advise budding entrepreneurs to believe in the open source movement, as this is the only future worth talking about. CIGNEX Datamatics embodies this hallowed principle not only in our corporate vision, ‘Making Open Source Work’ for enterprises but also, intrinsically, within our own organisation. A lot of our internal applications such as the Intranet, Extranet, document repositories, helpdesk ticketing generation for admin, HR, IT and support teams are based on open source,” says Shariff, as he signs off.

CIGNEX Datamatics intends to stay at the top by widening its customer base and selling more open source solutions. So will other companies follow suit?

By Priyanka Sarkar

The author is a member of the editorial team. She loves to weave in and out the little nuances of life and scribble her thoughts and experiences in her personal blog.

This article is from: