Problem with system("command")


 
Thread Tools Search this Thread
Top Forums Programming Problem with system("command")
# 1  
Old 10-11-2010
Problem with system("command")

Sorry, I don't speak English very well but I will try to explain my problem!
If I write a C++ program and I use che system call system("command") I have this problem:

If I would launch a program I do this:
Code:
system("~/Agostino/program/test")

and all work fine!!

But if I divide this command in two part like these:
Code:
system(cd ~/Agostino/program/);
system("test");

no work fine because "test" is not found. "test" is searched in the default home path. The second call to system is executed like if it was invoked in another terminal session!

I showed this trivial example but, generally, I would that the second call to system start when the first terminated and as a result.

I hope I've explained well my problem! Thank you all for your cooperation!
# 2  
Old 10-11-2010
The system() function runs the command in a separate process. When you run a second call, it gets its own process and anything (environment, working directory etc.) set in the first process isn't available.

Try putting the two commands together
Code:
system( "cd ~/Agostino/program; test" );

This will change directories and then run the test command; all in the same process.

Hope this helps.

---------- Post updated at 21:04 ---------- Previous update was at 20:58 ----------

It occurs to me that the system() function usually executes /bin/sh, and depending on your environment the shell installed as /bin/sh might not support the [/code]~[/icode] as meaning "home directory." Best to pass that information into your programme, or have the programme use get the value of HOME from the environment and use it.
# 3  
Old 10-11-2010
Thanks a lot!!
I will test it when arrive at home!!
And IfI want to spend some time between the first and the second command?? I would be sure that the first command is completed before the beginning of the second.
# 4  
Old 10-11-2010
Quote:
Originally Posted by acciues
Thanks a lot!!
I will test it when arrive at home!!
And IfI want to spend some time between the first and the second command?? I would be sure that the first command is completed before the beginning of the second.
Yes, the syntax command1; command2 'blocks' until the first command has completed. You could try this from the command line and see the behaviour:
Code:
date; sleep 4; date

# 5  
Old 10-11-2010
It work fine!! But I have other problem!!

This is my code:
Code:
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>

using namespace std;

int main (int argc, char* argv[]) {
	system("scp file.pdf AgostinoPolizzano@192.168.1.100:./; ssh AgostinoPolizzano@192.168.1.100; cmd");
	cout<<"end";
	return 0;
}

If I execute it, this is the output:
Code:
Last login: Tue Oct 12 03:49:13 on ttys000
MacBook-Pro-di-Agostino-Polizzano:~ AgostinoPolizzano$ ./test
file.pdf                                      100%  331KB 331.3KB/s   00:00    
Last login: Tue Oct 12 03:49:39 2010 from 192.168.1.103

AgostinoPolizzano@SERVER ~
$

The third command does not run and the program stops at the end of the second command!! Why?? It's a SSH question??
Thanks a lot!!
# 6  
Old 10-11-2010
What exactly is "cmd" supposed to do? It's not any UNIX command I've ever heard of.

If you're trying to run it on the remote server there shouldn't be a ; between it and the ssh command, since it will try to run it locally once ssh has finished, not inside ssh!
# 7  
Old 10-11-2010
cmd is Window's Command Prompt!! The ssh connection is between a Windows Server (OpenSSH is emulated by CygWin) and MacOs or Linux (is the same!!!!).

This command
Code:
system("scp file.pdf AgostinoPolizzano@192.168.1.100:./; ssh AgostinoPolizzano@192.168.1.100 cmd");

work fine!!!

Missing the last command that interests me and we have finished!!! It launch a print job on the server:
Code:
start C:\"Program Files"\Adobe\"Reader 9.0"\Reader\AcroRd32.exe /p /h file.pdf

How can I launch the last command after "cmd"???

Thanks very very much!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

What does "force devmap reload" as in "multipath -r" means for my system and stability of my system?

Cannot present unpresented disks back again. On a test server tried this as a solution "multipath -r" and it worked. Too worried to try it in production before I know all the information. Any info would be appreciated! Also some links to the documentation on this specific issue could help a... (1 Reply)
Discussion started by: jsteppe
1 Replies

2. Shell Programming and Scripting

awk "date" and "system" command

Hello experts! I need your help please I have a file.txt of which I want to extract 3rd and 4th columns with date with the form e.g.: 2016-11-25 03:14:50and pass them to "date" command, but also append the 9th column in a file as well. So I want to execute date -d '2016-11-25 03:14:50' ... (2 Replies)
Discussion started by: phaethon
2 Replies

3. AIX

[Tip] Problem with rpm ("different operating system")

I have once experienced this problem without understanding what caused it but now learned thatn there is even a PMR dealing with it. Sometimes it happens that you encounter the following (rather cryptical) error message when trying to install an rpm-package: package <rpm_package_name> is for a... (1 Reply)
Discussion started by: bakunin
1 Replies

4. UNIX and Linux Applications

Problem on SQLplus command ""bash: sqlplus: command not found""

Hi all, i face an error related to my server ""it's running server"" when i use sqlplus command $ sqlplus bash: sqlplus: command not found the data base is up and running i just need to access the sqlplus to import the dump file as a daily backup. i already check the directory... (4 Replies)
Discussion started by: clerck
4 Replies

5. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

6. Shell Programming and Scripting

Problem using "system" command in perl

Hello!!! I'm trying to pass the output from bash command to perl variable in a perl script, and I used the "system" command to execute the bash statment and pass the result to perl string variable, in this perl script I used a variable $file that store data for using it as a regular expression.... (2 Replies)
Discussion started by: evolabo
2 Replies

7. Shell Programming and Scripting

Problem with "find" and "grep" command

I want to list all files/lines which except those which contain the pattern ' /proc/' OR ' /sys/' (mind the leading blank). In a first approach I coded: find / -exec ls -ld {} | grep -v ' /proc/| /sys/' \; > /tmp/list.txt But this doesn't work. I got an error (under Ubuntu): grep:... (5 Replies)
Discussion started by: pstein
5 Replies

8. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

9. Programming

Problem with socket binding - "system" call

Hi, I am having an issue with using sockets. I have a program which binds to a socket and listen on it. Later I spawn a thread to handle some function. In the new thread created I need to call a shell script which executes the specified function. Here I am using a system command to call the... (5 Replies)
Discussion started by: Janardhanbr
5 Replies

10. UNIX for Dummies Questions & Answers

Unix "at" / "Cron" Command New Problem...Need help

Hi All, I am trying to schedule a one time job using the at command with the help of shell script for my project. The shell script should take a parameter as a command line argument from the at command itself. Is it possible to take a command line parameter for a shell script in the command... (3 Replies)
Discussion started by: Mohanraj
3 Replies
Login or Register to Ask a Question