|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
||||
|
||||
|
C++ to Linux - some questions
Ok, i wasnt sure if this belonged in Linux sectoin or not, but here it goes:
I want to write a bunch of programs in C++ that i will be using on RedHat8. They will be console based. I was wondering if anyone has done this on here. I am kind of confused as to how i would call Shell commands thru C++ ? And pass them parameters and whatnot? Say i wanted to do a command like: telnet somewhere.com 25 and then once in telnet issue more commands that i would have already stored in a string. is this possible or am i using the wrong language ? |
|
#2
|
||||
|
||||
|
You could use the socket() functions to connect to the port directly and send commands to it. A good book to learn socket programming is UNIX Network Programming by W. Richard Stevens.
If you want to use the system's telnet program to do your connecting, then you're probably better off using Expect, which is a tool designed specifically to automate other programs. Hope this helps. |
|
#3
|
||||
|
||||
|
well thnx for both of those links. the expect looks very interesting, it seems as tho i will have to learn that language Tcl, but there are tons of online tutorials so that should be fun. for now i will stick with the socket way i guess since i already have a few chapters in my current book on that topic. one other question, does linux support C++ ? it seems as tho every bit of source i ever have d/l is always in plain old C and not C++.
|
|
#4
|
||||
|
||||
|
You have to have the g++ compiler installed. Install it from the CD (or rpm or whatever), if it is not already installed for you.
|
|
#5
|
||||
|
||||
|
Yes, Linux does support C++. The C++compiler is g++, which I think is just a different invocation of gcc (so flame me if I'm wrong, which I probably am).
As for calling shell commands from within a program, the possibilities boggle the mind. Many C functions mirror shell commands. Or you could use system() or exec*() to run a program from within another -- keep in mind that exec* replaces the calling process and there seem to be some issues about using system(). Or you could use perl, which also recognizes back-quoted commands. Or you could use shell scripts. One example that our instructor gave us uses telnet to fetch a stock quote for IBM. I tried to attach it, but the forum doesn't accept text as a valid file type. Anyway, the pertinent portion is: Code:
( # you must include the parentheses as shown
waitconnect
echo "GET /q?s=ibm&d=e HTTP/1.0"
echo
echo
waitdone
) | telnet finance.yahoo.com 80 > $TEMP 2> /dev/null
sed 's/<[^>]*>/,/g' $TEMP > /tmp/jj1
exit
IBM=`sed 's/<[^>]*>/,/g' $TEMP |
grep 'IBM,*[0-9]:[0-9][0-9] *[aApP][mM],*[-+]\{0,1\} *[0-9]*\.[0-9]*'`
if [ -n "$IBM" ]
then
PRICE=`echo "$IBM" |
sed 's/.*IBM,*[0-9]:[0-9][0-9] *[aApP][mM],*\([-+]\{0,1\} *[0-9]*\.[0-9]*\).*
echo The current stock price for IBM is $PRICE
else
echo "Error: cannot determine the price" >&2
fi
It may well look cleaner with Expect -- I haven't worked with that yet. |
|
#6
|
||||
|
||||
|
aol<< well i kind of understand what u posted right there, u have any good links that have tutorials on how to do that stuff? im just wondering where type that? or what do i use to create the script rather?
|
|
#7
|
||||
|
||||
|
Shell programming, AKA "shell scripting". It's like DOS batch files, only much more powerful.
Unfortunately, I learned most of it in class, so I don't know any web sites. The quoting rules can be a bother at first, but basically they work just like in perl. You can assign text to a variable and then access it like a macro in a makefile, eg: Code:
CC=`pwd` # save the current directory in CC cd /usr/local/bin # cd to another directory cd $CC # return to where you were Plus, you can define functions, which is what waitconnect and waitdone are in the sample code. Comments start with a # The first line has a "shebang", '#!' followed by the program to run the script, eg: #!/bin/sh or #!/usr/bin/perl Most of the startup code in Linux consists of shell scripts; look in /etc/rc.d . And script files are just text files, so use whatever text editor you want to. The main point I was trying to make is that you have a very wide range of options open to you. For example, if you decide to write a sockets program to do what you want, then perhaps perl will work better for you than C or C++, since perl has telnet modules and the like. And the thing with shell scripts is that you can start out experimenting with telnet to see what inputs to provide and what outputs to expect. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > C++ to Linux - some questions |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|