Shell Scriipting-A Shell Script To Do Shell Scripting Faster
Shell Scriipting-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. 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
#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
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.
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:
#!/usr/bin/bash
if [ ! -f $1 ]; then
else
fi
fi
while [ 1 ];
do
vi $1
chmod 755 $1
javac $1
gcc $1
exit
EOF
else
./$1
fi
read dummy
done