Dr. Mark Humphrys

School of Computing. Dublin City University.

Home      Blog      Teaching      Research      Contact

My big idea: Ancient Brain

Search:

CA114      CA170

CA668      CA669      Projects


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.



C / C++



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)





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



GUI in Shell

There are some clever projects to get some limited GUI support into Shell.



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.


Perl

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:



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?

  1. Very simple customisation - Check out program preferences or command-line arguments.
  2. 1-liner utilities - aliases
  3. Command-line utilities with some logic - Shell
  4. Complex command-line utilities - Perl (or other)
  5. Small applications doing lots of calculations - C++ (or HLL of your choice)
  6. 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.



ancientbrain.com      w2mind.org      humphrysfamilytree.com

On the Internet since 1987.

Wikipedia: Sometimes I link to Wikipedia. I have written something In defence of Wikipedia. It is often a useful starting point but you cannot trust it. Linking to it is like linking to a Google search. A starting point, not a destination. I automatically highlight in red all links to Wikipedia and Google search and other possibly-unreliable user-generated content.