How To Add A System Command


by Gary Ratliff, Sr. (eronstuc)

Well it has been quite a while since I last wrote. Since I am getting to be an old man, I have been thinking about getting my last things in order. Also, since I had never used Wine, I decided to learn a bit more about it.

Then I realized that most of the things I enjoy about computing were related to writing software. I also looked through my library to discover that the language which I have the most on is the C++ language.

In this library, I learned that two of the volumes contained CD's which had a free development system called Dev-C++ from Bloodshed Software. I also ordered the CD for this, which has the linux version of this development package. OK … so I also learned that it seems to take forever to get the people to send the item. The system is not really in current development and has not had anything added to it since 2005.

One of the features of this package for Windows is a command: system(“PAUSE”) which ends most of the programs developed for a console application. This is because when Windows executes these programs, it will just execute the code and return to Windows! So the PAUSE command stops the system so the user can see the output of the program.

Now if we use g++ and Linux, we don't have that problem, since the program is mainly launched from a terminal and its output is clearly visible.

However, I thought that it would be a challenge to develop a PAUSE system command which would perform the same as this feature of the Dec-C++ package.

The output from this is simply a message which writes: “Please press any key to continue” and then waits for the user to press any key.

After much study in the methods of the cin object, I learned that the single character get feature was just the thing we needed. Once the PAUSE feature was created, we would want to add it to the system path. To do this, we need to be root or the action will be denied.

Therefore, I will show you how to add this to your system programs. I will be using a freshly installed version of Full Monty which I made to a flash drive from my computer Gabriel (the eMachine I bought last Christmas). So learn where the system files are kept. To learn this execute the command:

which g++

and on the Full Monty, the answer returns as /usr/bin/g++.

Therefore, we know that the pause command will have the location/usr/bin for its home once it has been created.

After some experiments which went awry, this C++ program was found to work correctly. We will use the Kwrite editor to create the following program. So launch it with kwrite get.cpp and enter the following program:

#include <iostream>

using namespace std;

int main()

{

        char ch;

        cout << “Please press any key to continue.... “;

        ch = cin.get();

        return 0;

}

Now save the program and remember that all these steps are performed as the root user. We are now back in a terminal and ready to compile the program with this command:

g++ get.cpp -oPAUSE

Next, you may test it to verify that it works correctly using:

./PAUSE

and the message is shown to the terminal and vanished once you press any key. You are now ready to save the program as a new system file with:

mv PAUSE /usr/bin

Now the program is on the path and will run if you enter the command PAUSE. So now we want to use the new feature of allowing the line:

system(“PAUSE”) to be a part of our C++ programs.

To test this, we will use the tried and true Hello World program. By the way, once this has been installed on the system path, there is no longer the need to remain root.

OK. Let’s enter the infamous Hello World program once again using the kwrite editor from the command: kwrite hello.cpp and then enter the following program:

#include <iostream>

#include <cstdlib> // or #include <libstd.h>

using namespace std;

int main()

{

        cout << “Hello World!” << endl;

        system(“PAUSE”);

        return 0:

}

We finish up by saving the program and then compile it using:

g++ hello.cpp -ohello

(or just g++ hello.cpp and the default output will be named a.out.)

Now we launch it with ./hello or ./a.out and see the result:

Hello World!

Please press any key to continue....

The cstdlib or the stdlib.h file is needed to allow you to use the system command. The endl object is needed to force a newline to the text Hello World. If you leave off the endl then the message: Press any key to continue is printed before the message Hello World. I verified this on both the Full Monty and my Knoppix 6.4 computer and both have the same result.

Btw I have now used flash drives to store my linux systems as I am not anxious to loose an entire hard drive. The Full Monty may be installed on a 16 Gig flash drive and once installed will have 1.6 gigs of space available for you to use for your own data.