Writing short utilities in other languages
You can of course write short command-line utilities and scripts
to automate tasks
in
almost any programming language.
Compiling a C++ program in UNIX / Linux
"Hello World" program
edit prog.cxx
g++ prog.cxx
This creates a binary executable file called
a.out
To make the output a file called
prog
do this:
g++ prog.cxx -o prog
You may need to:
chmod +x prog
Then run:
prog (args)
-
g++
- GNU C++ compiler
-
gcc
- GNU C-only compiler
-
CC
- Sun C++ compiler
-
cc
- Sun C-only compiler
-
cxx
- DEC C++ compiler
Accessing the command-line from C++
In a HLL, calling other programs
(and access to the command-line in general) is usually more awkward
than in a command-line-oriented script.
Example 1
In C/C++
you use the system() call:
#include <stdlib.h>
main()
{
system ( "rm file7.txt" );
}
which is more complex than the Shell:
rm file7.txt
You also have to compile the program, and keep track of 2 files
- the source and the binary.
In Shell, there is only 1 file.
Example 2
Also, the above is
alright if filenames are static.
But consider where the file name is variable. In Shell:
for i in 96 97 98 99 00
do
rm $i.log $i.txt
done
In C++ this is much more complex:
#include <stdio.h>
#include <stdlib.h>
main()
{
char buf [ 30 ];
for ( int i=96; i<=99; i++ )
{
sprintf ( buf, "rm %d.log %d.txt", i, i );
system ( buf );
}
system ( "rm 00.log 00.txt" );
}
Question
And if you want to get directory listings Shell is much easier.
Q.
How would you code the following Shell script in C++?
for i in */*doc */*xls
do
cp $i $HOME/backups/$i
done
Environment variables
Access to environment variables is usually a bit more awkward in the HLL:
#include <stdlib.h>
char *homestring = getenv ( "HOME" );
Advantages and Disadvantages of C++ compared to Shell
- Disadvantages of C++ compared to shell (for writing short utilities):
-
Constructing command-lines in general, with environment variables,
variable and wildcard filenames, and piping and redirection,
is more cumbersome.
- The programs have to be recompiled any time
you move to a new system.
- Advantages of C++ compared to shell:
-
Execution speed much faster.
-
High-level programming features - types, data structures,
arrays,
object-oriented classes with inheritance,
class libraries.
Support for GUIs, threads, etc.
Shell is only useful for simple programs.
There are some clever projects to get some limited GUI support into Shell.
- Packages to allow shell scripts pop up GUI dialogs (and retrieve results):
Color selection dialog in Zenity,
launched from Shell, return value captured in Shell.
Usage like:
COLOR=`zenity --color-selection --show-palette`
Screenshot from
here.
Other HLLs
Of course, Java and a range of other HLLs could be used for the tasks above.
Interpreted language.
Direct access to command-line.
More advanced than Shell.
PHP
PHP can be used as a command-line scripting language.
Can use a mix
I tend to write my short utilities in Shell.
For more complex utilities, I use C++.
For simpler utilities I try to express them as aliases.
Often, I use both HLL and Shell:
-
I surround a C++ utility with a small Shell wrapper
that prepares the filenames and environment variables,
calls the C++ program, and then possibly does some processing of the output.
-
In some of my online scripts, I surround Shell utilities
with C++ character by character input processing for security.
A hierarchy of languages
I want to customise my system,
and automate many tasks.
Like any programmer, I am always starting to
write programs.
How should I approach writing small utilities?
- Very simple customisation - Check out program preferences
or command-line arguments.
- 1-liner utilities - aliases
- Command-line utilities with some logic - Shell
- Complex command-line utilities - Perl
(or other)
- Small applications doing lots of calculations - C++
(or HLL of your choice)
- Complex applications - Before investing a load of time
in writing it yourself, maybe look online for freeware,
shareware,
or even (gasp) something you might buy.