0

I have to write a C++ program to pass a command line argument into a shell script. My code will compile but when I try to run the program with the argument it starts a new line like it's waiting for input instead of passing the argument into the script.

This is my C++ code:

#include <iostream>
#include <cstdio>
#include <cstdlib>

char command[50];

int main(int argc, char *argv[])
{

        sprintf(command, "%s %s", "./findName", argv[1]);
        system(command);
}

I'm running the file from a unix terminal like this:

./findName someargument

user2929674
  • 3
  • 1
  • 3
  • I'm confused; are you trying to run your C++ program from a shell or are you trying to kick off a shell command from your C++ program? – MrDuk Apr 18 '14 at 19:39
  • 1
    How are you calling the c++ program? What happens when you run `findName` without arguments? – FDinoff Apr 18 '14 at 19:40
  • 2
    Why are you fishing for bugs by using a fixed buffer? Use `std::string`. – molbdnilo Apr 18 '14 at 19:40
  • Your program will keep running until the command terminates. – ooga Apr 18 '14 at 19:44
  • What is the script doing? – evading Apr 18 '14 at 19:44
  • Your program will just run in an infinite loop. It will recursively call itself each time it is called and never return. Which reminds me, you should always `return 0;` on successful execution. – RamblingMad Apr 18 '14 at 19:45
  • I'm running this from a shell. The shell script findName just searches a file using the argument. The shell script runs fine on it's own but I need to use a C++ program to run it and I've never coded in C++ before. – user2929674 Apr 18 '14 at 19:47
  • 1
    @CoffeeandCode `return 0` is implicit in `main()` – Angew is no longer proud of SO Apr 18 '14 at 19:49
  • @Angew No it isn't. [The standard doesn't define what main will return if you don't specify it yourself.](http://stackoverflow.com/questions/3727051/why-does-a-main-function-without-a-return-statement-return-value-12) – RamblingMad Apr 18 '14 at 19:59
  • @CoffeeandCode Yes, it does. C++11,3.6.1§5: "If control reaches the end of `main` without encountering a `return` statement, the effect is that of executing `return 0;`" It's present in C++03 as well. I don't know about C++98, but I'd expect it there too. Note that it's absolutely specific to `main`. – Angew is no longer proud of SO Apr 18 '14 at 20:01
  • 1
    @Angew Oh, well I'll remember that now. Although I would never encourage it. – RamblingMad Apr 18 '14 at 20:01
  • @CoffeeandCode Note that the question you linked is about C. C and C++ can easily differ in this regard. – Angew is no longer proud of SO Apr 18 '14 at 20:02

2 Answers2

0

working.cpp

#include <iostream>
#include <cstdio>
#include <cstdlib>

char command[50];

int main(int argc, char *argv[])
{

        sprintf(command, "%s %s", "./findname", argv[1]);
        system(command);
}

and findname

#!/bin/bash
echo "hello $1"

In terminal

$gcc -o working working.cpp
$./working world
hello world

You obviously can't put the name of your cpp program in the command to system as it will call itself forever.

evading
  • 3,032
  • 6
  • 37
  • 57
  • I'm using a make file to compile and then entering it the same way you have in this and it doesn't work – user2929674 Apr 18 '14 at 19:56
  • @user2929674 what is the name of your cpp program and what is the name of your script? – evading Apr 18 '14 at 19:57
  • Is `findname` executable? What is the error message you are getting? – Massa Apr 18 '14 at 19:58
  • @evading the cpp program is findName.cpp and script is findName.sh – user2929674 Apr 18 '14 at 19:59
  • Then you should call findName.sh in your call to system in your cpp program or you will call your cpp program from your cpp program causing an endless loop of calling. – evading Apr 18 '14 at 20:00
  • @Massa findName is executable and I'm not getting an error it just waits for input but doesn't pass the input into anything. – user2929674 Apr 18 '14 at 20:01
  • Change to `sprintf(command, "%s %s", "./findName.sh", argv[1]);` and post your script if you can. – evading Apr 18 '14 at 20:02
  • @evading okay I'm an idiot I didn't realize when I was calling the system I wasn't calling the script but the program itself. Thanks for the help. – user2929674 Apr 18 '14 at 20:03
0

What does findName.sh does? Nothing you said is about waiting for input. And furthermore, if that is the name of the shell script, that is what you should put in the sprintf line, upper case N, .sh at the end and all....

Massa
  • 8,647
  • 2
  • 25
  • 26