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
This creates a binary executable file called
To make the output a file called
g++ prog.cxx -o prog
You may need to:
chmod +x prog
- GNU C++ compiler
- GNU C-only compiler
- Sun C++ compiler
- Sun C-only compiler
- 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.
you use the system() call:
system ( "rm file7.txt" );
which is more complex than the Shell:
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.
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
rm $i.log $i.txt
In C++ this is much more complex:
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" );
And if you want to get directory listings Shell is much easier.
How would you code the following Shell script in C++?
for i in */*doc */*xls
cp $i $HOME/backups/$i
Access to environment variables is usually a bit more awkward in the HLL:
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,
object-oriented classes with inheritance,
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.
COLOR=`zenity --color-selection --show-palette`
Screenshot from here
Of course, Java and a range of other HLLs could be used for the tasks above.
Direct access to command-line.
More advanced than Shell.
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
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
- 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,
or even (gasp) something you might buy.