쉘이 내장 에코가 아닌 외부 에코만 ​​실행하도록 만드는 방법은 무엇입니까?

쉘이 내장 에코가 아닌 외부 에코만 ​​실행하도록 만드는 방법은 무엇입니까?

system()라이브러리 함수를 사용하는 C 프로그램이 있습니다. 아래는 소스 코드입니다.

#include<stdlib.h>
int main()
{ 
    //Some code
    system("echo Hello World");
    //some code
    return 0;
}    

$PATH이 C 프로그램을 실행하기 전에 대신 /home/user1/bin/echo실행되도록 변경했습니다./bin/echo

export PATH="/home/user1/bin"

그런데 C 프로그램을 실행하면 실행이 되지 않습니다 /home/user1/bin/echo.

쉘이 echo하나이기 때문에 찾으려고 하지 않았기 때문 입니까 ?shell built-inecho$PATH

그렇다면 내장 echo된 .$PATHecho

답변1

command기능을 우회하지만 쉘 내장 기능은 우회하지 않습니다. 가장 안전한 방법은 전체 경로를 사용하는 것입니다.

system("/home/user1/bin/echo Hello world!")

그렇게 할 수 없다면 exec내장된 기능을 사용해 보세요.

system("exec echo Hello World!")

예를 들어:

$ cat foo.c       
#include<stdlib.h>
int main()
{ 
    //Some code
    system("exec echo --help");
    system("command echo --help");
    system("echo --help");
    //some code
    return 0;
}  
$ gcc -o foo foo.c
$ ./foo
Usage: echo [SHORT-OPTION]... [STRING]...
  or:  echo LONG-OPTION
Echo the STRING(s) to standard output.

  -n             do not output the trailing newline
  -e             enable interpretation of backslash escapes
  -E             disable interpretation of backslash escapes (default)
      --help     display this help and exit
      --version  output version information and exit

If -e is in effect, the following sequences are recognised:

  \\      backslash
  \a      alert (BEL)
  \b      backspace
  \c      produce no further output
  \e      escape
  \f      form feed
  \n      new line
  \r      carriage return
  \t      horizontal tab
  \v      vertical tab
  \0NNN   byte with octal value NNN (1 to 3 digits)
  \xHH    byte with hexadecimal value HH (1 to 2 digits)

NOTE: your shell may have its own version of echo, which usually supersedes
the version described here.  Please refer to your shell's documentation
for details about the options it supports.

Report echo bugs to [email protected]
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'echo invocation'
--help
--help

두 번째 및 세 번째 호출은 플래그를 지원하지 않는 system내장 함수를 실행합니다 . 내 경우 첫 번째 실행은 이 플래그를 지원하는 GNU에서 제공됩니다.echo--help/bin/echo--help

~에서man 3 system(POSIX):

The environment of the executed command shall be as if a child  process
were created using fork(), and the child process invoked the sh utility
using execl() as follows:

      execl(<shell path>, "sh", "-c", command, (char *)0);
where <shell path> is an unspecified pathname for the sh utility.

당신이있는 경우리눅스:

system()  executes a command specified in command by calling /bin/sh -c
command, and returns after the  command  has  been  completed.   During
execution  of  the  command,  SIGCHLD  will  be blocked, and SIGINT and
SIGQUIT will be ignored.

명시적으로 사용되기 때문에 /bin/sh -c일반적인 방법으로는 영향을 미칠 수 없습니다. 교체할 수 있습니다 /bin/sh. 그러면 파일 드라이버를 사용하여 버블랩을 터뜨릴 수 있습니다.

답변2

Bash를 사용하는 경우 다음을 사용하여 내장 쉘 명령을 활성화 및 비활성화할 수 있습니다.enable

enable -n echo

내장된 echo 명령을 비활성화합니다.

답변3

command이를 수행하는 명령이 있습니다 . 분명히, 당신은 할 수 없습니다.

system()그런데 보안이 걱정된다면 조심하세요.

관련 정보