Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

i have a sh script and want a c or cpp script to do the same

Status
Not open for further replies.

jurros

Programmer
Feb 14, 2005
13
NO
Code:
#!/bin/sh
IFS=`echo -e "\n\r"`
for I in `cut -f1 $1`
do
source="[URL unfurl="true"]http://startsiden.no/sport/nyheter/"[/URL]
echo "Kilde: $source"

lynx -dump $source > /tmp/nyhet

hva=$(cat /tmp/nyhet |grep $I | cut -d'.' -f1 | sed -e s/\ //g)
for nr in $hva; do
	 over=$(cat /tmp/nyhet | grep -G "\W$nr\W" | head -n 1| cut -d']' -f2)
	 url=$(cat /tmp/nyhet | grep -G "\W$nr\W" | tail -n 1 | cut -d' ' -f4)
echo "<a href=\"$url\">$over</a><br>"
done
done
rm /tmp/nyhet
im pretty new at c, but the sh script gets urls form standard in or from a file, then it downloads the hfref+topic of the website,....
help in c witht that?
 
Whilst it is possible, you've got to have a good reason for wanting to do something like that in C.

It's all text processing and collecting the ouput of other processes using pipes. Dead easy in shell, but a real pain in C.

If you're looking for a big performance boost, then I suggest you find another way of dealing with the file which doesn't involve "cat /tmp/nyhet" twice each time around the loop.

Shell programmers normally resort to PERL when they need something to happen quicker. For example, PERL can read and hold in memory the whole file, and has regular expressions which are more powerful than grep.

My suspicion is that the records you get each time around the loop are adjacent to one another in the file.

--
 
actually the sh script works...
it sends the website into a temp file on the computer then it gets the urls i want to.. Still how do i connect to a website and dump all the hrefs*topic onto a tmp file with c?
Code:
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main(int argc,char *argv[]){
int i=1;
char buffer[256];
  ifstream omFil (argv[i]);

  if (!omFil.is_open())
  {
   char buffer[256];
   char str[80];
  ifstream omFil (scanf ("%s",str));
}
  while (! omFil.eof() )
  {
    omFil.getline (buffer,100);
    cout << buffer << endl;
  }
  return 0;
}
This is what i have done so far... it can read the text in a file from an argument..., now i need to dump a website onto a file tmp file..
 
jurros said:
actually the sh script works...
Actually, no one suggested it didn't...

Thus, you've apparently misunderstood the reply and should probably go back and reread it.


Now, unless this is purely for educational purposes, you're probably looking for a tool like [tt]wget[/tt] or [tt]curl[/tt]. Don't reinvent the wheel.


If it is for educational purposes, you might want to look into socket programming and learn the HTTP protocol. More likely, you'll want to find and use some library that already implements HTTP.
 
yes i misunderstod u :) sorry about that.. but is it possible to use lynx at the same way i used it in the sh script.. thats coz lynx adds numbers to the urls when i download it.. Um yes i have also tryed to learn about the HTTP1.1 somthing.. but i dont think i need to go there yet :) so that will work with my cpp prog..
 
To use an external program/script you can either fork and use an exec function (execvp(), evec(), et. al.) OR use system which forks and runs the passed command in a new instance of the shell (i.e. the shell is transparent)....

Really using an exisiting library is more effecient (as it doesn't require a second process) and is more portable (as long as the library doesn't make system-dependant calls or has a port for every major OS).
 
ok but how do i use the exec function coz i want to include lynx into my script...
Code:
char arg[254];
char adr[254] = [URL unfurl="true"]http://www.vg.no[/URL]
arg = printf("-d %s", addresse);
exec("lynx -dump addresse")", arg);
is it somthing like this above?
 
exec will need to be forked...

it'd be:

Code:
int f = fork();
if(f > 0)
{
   //parent -- f will be the PID of the new process
   if(wait() != 0)
   {
      //lynx crashed
   }
}
else if ( f == -1 )
{
  //failed to fork
  //handle error
}
else
{
   //child   f will be zero
   close(0);
   close(1);
   close(2);
   open("/tmp/nyhet","w");
   //
   char *arg[2] = {"-dump","[URL unfurl="true"]http://example.com"};[/URL]
   exec("lynx", arg);
}

With system, it'd just be:
system("lynx -dump > /tmp/nyhet");
 
lol how did that come here.. i cant remeber posting that,.,, tought i deleted that :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top