![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Nov 2004
Posts: 47
Rep Power: 0
![]() |
System Info
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
system( "echo Date: `date`" );
system( "echo Uptime: `uptime`" );
system( "echo Name: `uname -n`" );
system( "echo Kernel: `uname -r`" );
}But I want to be able to add flags for instance ./sysinfo -d prints only the date line, can someone tell me how to make that possible? And also I found a line in bash that displays the hdd space. df | awk '{ sum+=\$2/1024^2 }; END { printf (\"%dGb\", sum
)} |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
You're going to need to declare main like this:
int main(int argc, char *argv[]) where argc is the number of arguments that are passed from the command line, and argv is a string representation of an argument. So for instance, in your -d example, argc would be 2 (I believe), since their are two arguments (the name of your program and "-d"), and argv[1] would be "-d".
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Nov 2004
Posts: 47
Rep Power: 0
![]() |
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[2])
{
system( "echo Date: `date`" );
system( "echo Uptime: `uptime`" );
system( "echo Name: `uname -n`" );
system( "echo Kernel: `uname -r`" );
} |
|
|
|
|
|
#4 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
You would need some if statements.
And main would have to be declared as such: int main(int argc, char *argv[]) And then, if you want to use the -d flag or something you could do something like this: if(strcmp(argv[2], "-d") == 0)
{
// print out date
}Keep in mind, strcmp() is in the string.h header file. Also, just for your info, you could also declare main like this: int main(int argc, char **argv) but I've never done it like that. I always use the method I posted before. Also, the argument names could be changed to best suit your needs, argc and argv are just customary, though.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Nov 2004
Posts: 47
Rep Power: 0
![]() |
You kinda lost me, but I tried the code
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
system( "echo Date: `date`" );
system( "echo Uptime: `uptime`" );
system( "echo Name: `uname -n`" );
system( "echo Kernel: `uname -r`" );
if(strcmp(argv[2], "-d") == 0)
{
system( "echo Date: `date`" );
}
}c0debox programming # ./sysinfo -d Date: Tue Feb 15 00:26:13 EST 2005 Uptime: 00:26:13 up 2:33, 4 users, load average: 0.07, 0.08, 0.06 Name: c0debox Kernel: 2.6.10 Segmentation fault |
|
|
|
|
|
#6 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
I appologize. It should be argv[1]. I don't know what I was thinking.
Anyway, here's code for you: #include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int main(int argc, char *argv[])
{
if(argc>1)
{
if(strcmp(argv[1], "-d") == 0)
{
system( "echo Date: `date`" );
}
}
else
{
system( "echo Date: `date`" );
system( "echo Uptime: `uptime`" );
system( "echo Name: `uname -n`" );
system( "echo Kernel: `uname -r`" );
}
}
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Nov 2004
Posts: 47
Rep Power: 0
![]() |
Cool it worked. Now can you explain this line for me so I can set the flags up for everything else
if(argc>1)
{
if(strcmp(argv[1], "-d") == 0)
{
system( "echo Date: `date`" );
} |
|
|
|
|
|
#8 |
|
Programmer
Join Date: Nov 2004
Posts: 47
Rep Power: 0
![]() |
// Written by c0demuncher
#include <iostream>
#include <string>
using namespace std;
void c0de()
{
system( "echo Date: `date`" );
system( "echo Uptime: `uptime`" );
system( "echo Name: `uname -n`" );
system( "echo CPU: `uname -p`" );
system( "echo Kernel: `uname -r`" );
}
int main(int argc, char *argv[])
{
if(argc>1)
{
if(strcmp(argv[1], "-d") == 0)
{
system( "echo Date: `date`" );
}
}
else
{
c0de();
}
} |
|
|
|
|
|
#9 |
|
Programming Guru
![]() |
To be honest with you, you need to go back to the fundementals of programming looking at if statements, string handlers loops etc etc before you get into this because if you had read about arrays and if statements this would be a breeze for you.
|
|
|
|
|
|
#10 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
if(argc>1) means if there's more than one argument passed from main. Everytime you leave a space when calling a program, and then add another string after the name of the program, you're passing arguments to it. The first argument is the name of the program, the second argument is, in this case, "-d." argc just counts arguments.
if(strcmp(argv[1], "-d") == 0) means if argv[1] (argument passed from main) is -d then you do whatever code that follows.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|