The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
C++ to Linux - some questions
Discuss C++ to Linux - some questions in the C Programming forum on Dev Shed. C++ to Linux - some questions C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

April 3rd, 2003, 07:51 PM
|
 |
not a fan of fascism (n00b)
|
|
Join Date: Feb 2003
Location: ct
|
|
|
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  ?
|

April 4th, 2003, 01:47 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
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.
|

April 4th, 2003, 02:30 PM
|
 |
not a fan of fascism (n00b)
|
|
Join Date: Feb 2003
Location: ct
|
|
|
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++.
|

April 4th, 2003, 03:02 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
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.
|

April 4th, 2003, 03:07 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
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.
|

April 4th, 2003, 08:32 PM
|
 |
not a fan of fascism (n00b)
|
|
Join Date: Feb 2003
Location: ct
|
|
|
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?
|

April 4th, 2003, 09:14 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|