1

I am developing a binary executable program command to execute programs and return the output into a text field.

The parameters to the command involve the command itself as it would be typed on the command line, and the directory. So the routine that executes the operation first switches to the directory, then executes the command.

For example if I want to execute the command some.cmd in the directory /home/user the parameters are command = 'some.cmd' and directory = '/home/user'.

What I have found is that some.cmd does not work but if I change command to /home/user/some.cmd the command works. However the command ls -l works. I also notice that the cd command is not recognized. If I run it remotely via ssh such as setting command to ssh user@localhost 'cd /home/user && ./some.cmd' it works.

It seems that some settings which are present when the command is executed in a shell are not present when it is run directly, but doing it via ssh seems to create the settings for it work.

Is there some explanation for this?

UPDATE: After some enquiries I got to learn that the API used for executing the commands were not being executed in the shell, or were not executed with the normal environment available from the console. After executing the commands with the /bin/sh -c "cd ..." option the problem is no more. This is the environment doing ssh user@localhost 'command ...' gave me.I am not so sure of the technical details, but apparently the existence of the environment available when you execute in your normal shell is not always available to commands executed directly by the OS.

  • 2
    Do you really need to cd before running your command? Why not just ssh user@localhost /home/user/some.cmd? – roaima May 15 '15 at 14:53
  • You never explained how you implemented the directory parameter. – Mikel May 21 '15 at 13:32
  • @roaima Preceding the command with 'ssh user@localhost' is an unnecessary overhead and there should be a way of creating the environment without logging in again via SSH – vfclists May 21 '15 at 13:54
  • The API I am using has a property called 'CurrentDirectory' and it switches to the 'CurrentDirectory' before executing 'Command'. It is not actually a shell, like bash. As I said 'ls -l' returns the correct output so 'Current Directory' is set properly. – vfclists May 21 '15 at 13:58
  • 1
    @vfclists when I try to resolve a question I like to partition the problem. You yourself offered ssh user@localhost so I started with that as a working assumption and asked whether you really needed the cd for your application to work. Once I had the answer to that I would have been able to make a decision how to proceed with an offered solution. – roaima May 21 '15 at 13:58
3

When you try to execute a file, the system has to know how to find the file. That's why it works if you specify the full path to it. The shell also has a PATH environment variable that stores a list of directories to look in to find an executable. That's why you don't have to specify the full path for ls.

  • Why then does the 'cd` command not work? – vfclists May 15 '15 at 14:47
  • 6
    @vfclists ; Because cd is a shell builtin command not found via PATH. (If you need a C library function have a look at chdir().) – Janis May 15 '15 at 14:49
  • Do you mean that cd depends on the shell but ls does not? The problem is that if the ls command can list the directory contents correctly then why can't the executable which is already there not run? – vfclists May 15 '15 at 15:07
  • 1
    Right, cd is a command the shell knows how to implement ls is an external executable (often /bin/ls, you can find yours by doing which ls). The PATH variable tells the shell to look in /bin among other places, so when you try to run ls it find the executable and runs it. Generally . is not in PATH so just being in the same directory as an executable does not make it "findable" to run, that's why using ./some.cmd works, you are now telling it where to find it instead of making it look in PATH – Eric Renouf May 15 '15 at 15:10
  • 2
    @vfclists you may be interested in the type command too, which will give you information about what will happen if you try to run a command. For example type cd gives me cd is a shell builtin and type ls gives me ls is aliased to ls --color=auto'` and can be augmented with which ls which gives me alias ls='ls --color=auto' /bin/ls – Eric Renouf May 15 '15 at 15:24
3

This is entirely an issue with the command not being in your PATH. Unlike Windows systems the current directory is not implicitly in the search path for executables. To run a command such as ls (eg ls -l) it needs to be in your PATH, and indeed it is - type ls will show you it's either in /bin or /usr/bin. However, some.cmd is not in your PATH and so cannot be executed transparently as a command. Three options spring to mind:

  1. You can move some.cmd to a directory that is in your PATH. For example, on some distributions /usr/local/bin/ and "$HOME"/bin are added to the PATH by default

  2. You can add "$HOME" to your PATH

  3. You can run ./some.cmd (a relative path to the current directory) instead of trying to use plain some.cmd

  • I think you may have misread the question. The commands are not entered into a terminal, but are executed by an application. ssh@localhost works as I expect because the commands get run in a terminal environment. What I need is for the application to be able to execute the commands as though they are typed in a terminal without going through SSH and return the output, a command that creates the environment like ssh@localhost does, but a local one. – vfclists May 21 '15 at 15:36
  • @vfclists yes, exactly. The PATH needs to be set, or you need to provide /some/sort/of/path to the application you want to run. – roaima May 21 '15 at 15:55
1

Hi


First Way :

You Can Use The Libeshell C++ Library For Writing Shell-like Console Applications

with This Library You Can Creation Of Command-line And Shell-style Applications


Second Way :

Use The This Code For Execute The Command With C++ :

#include <stdlib.h>

int main(){

system("gnome-terminal -x sh -c 'mkdir /root/Desktop/New-Dir-1 ; mkdir /root/New-Dir-2'");
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.