且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从变量运行脚本命令?

更新时间:2023-10-21 10:15:22

您正在演示Shell与内核之间的区别.

You're demonstrating the difference between the shell and the kernel.

"ls -l"可通过系统execve()调用执行.您可以man execve获取详细信息,但这可能对您来说太多了.

"ls -l" is executable by the system execve() call. You can man execve for details, but that's probably too much detail for you.

"ls -l | grep e"需要shell解释才能设置管道.在不使用外壳的情况下,"|"字符只是作为ls的参数传递到execve()中.这就是为什么您看到没有这样的文件或目录"错误的原因.

"ls -l | grep e" needs shell interpretation to set up the pipe. Without using a shell, the '|' character is just passed into execve() as an argument to ls. This is why you see the "No such file or directory" errors.

解决方案:

cmd="ls -l | grep e"
bash -c "$cmd"