Thursday, August 18, 2011

A Shell Script to do shell scripting faster



   Shell Scripting is all about automating a particular task, doing a task a bit faster than what it takes to do manually. However, when we write a shell script, if we notice properly, the amount of time we take to get it done is little more. In other words, can we think of ways in which we can write and test a shell script faster?

  Typically, when we write a shell script, we open the editor, write something, save and close it. Run the script. Depending on the output or some error thrown, open the file again, edit something, save and close it. Run the script, and this goes on and on and on. The focus here is the amount of time you spent on saving the file every time, running it, and again opening the file and repeating this whole process umpteen times. In some cases, while running, we get error, and by the time we re-open the file, we forget what the error is. Now, again run the script and see the error again and re-open it. The amount of time we spend here is pretty high.


  Sometimes, what people do is, they keep 2 terminals open, edit and save the file in one terminal, and keep running the script in the other. This is a good time saver, but let us see what can be done to save the maximum amount of time to write and test a script.

The method which we are going to discuss to make this process faster involves two steps:

1.  Map a key in vi to save and close a file.
2. Write a shell script which will open the file(which is a shell script), and run the script.

Lets discuss those 2 points in detail:

1. Let us map the function key F2 in vi to do a save a close. The following is the line which we should add in the .exrc or the .vimrc file. Once the below line is added, on pressing F2 inside vi, the file will get saved and quit vi.
map #2 :wq!^M
Note: ^M is put using the following sequence: Ctrl+V+M

 #2 refers to the function key F2. The above setting will work if your Unix flavor is Solaris or Linux. If it is HP-UX,  refer this link: Mapping function keys in vi

2. Write a small script,  with the following contents. I name this script as workon:
$ cat workon
#!/usr/bin/bash

if [ ! -f $1 ]; then
  echo "#!/usr/bin/bash" > $1
fi

while [ 1 ];
do
        vi $1
        chmod 755 $1
        ./$1
        read dummy
done

What this script does is simple: It accepts a file name as argument, which is a shell script in our case. If the file is empty, adds the she bang line and gets it ready. Else simply opens the file. On closing the file, runs the script and the output is displayed. On pressing any key, it takes you back to vi again. And the cycle repeats.

Now, lets write a sample script, test.sh.

1. Invoke the workon script with the test.sh as its argument:

























2. On invoking workon, the below screen will be opened. As you see, the she bang line gets added automatically since its a new file:























3. Let us edit the file and put some contents in it:
























4. Press F2 to save and run the file. As seen below, the output is displayed:























5. Press 'Enter' to edit the file again, and the cycle repeats:






















 

In this way, we can write and test a shell script much much faster.

Generic workon script:
The next question, can we customize this workon  script even more?

For example, if you are a developer, at a given point of time, you might be writing a C/C++ program, or a Java program or a shell script or a perl script or a python script or a PL/SQL script and so on.When we write a C program, on pressing F2, we would like it to get compiled and the program be run, and the same holds good for  Java programs as well. Like the she bang line added automatically for a shell script, we would also like the same to get added for a Perl or Python script. Similarly, for a PL/SQL script, we would like the SET statements to get added automatically.

  The below shown program is the customised workon program which I use. Hope it might be useful for you too. You might customize it further to suit your needs:
Enjoy the world of automation!!!

2 comments:

  1. In your vi session you can call a shell command.
    The first edit is a bit difficult:
    vi test.sh
    (edit script)
    :w
    :!chmod +x %
    :!./%

    Continue editing and testing with the short cycles
    (edit script)
    :w
    :!!

    ReplyDelete