![쉘이 내장 에코가 아닌 외부 에코만 실행하도록 만드는 방법은 무엇입니까?](https://linux55.com/image/75902/%EC%89%98%EC%9D%B4%20%EB%82%B4%EC%9E%A5%20%EC%97%90%EC%BD%94%EA%B0%80%20%EC%95%84%EB%8B%8C%20%EC%99%B8%EB%B6%80%20%EC%97%90%EC%BD%94%EB%A7%8C%20%E2%80%8B%E2%80%8B%EC%8B%A4%ED%96%89%ED%95%98%EB%8F%84%EB%A1%9D%20%EB%A7%8C%EB%93%9C%EB%8A%94%20%EB%B0%A9%EB%B2%95%EC%9D%80%20%EB%AC%B4%EC%97%87%EC%9E%85%EB%8B%88%EA%B9%8C%3F.png)
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-in
echo
$PATH
그렇다면 내장 echo
된 .$PATH
echo
답변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()
그런데 보안이 걱정된다면 조심하세요.