Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何使linux中的脚本使用我的Interpeter并工作?(#!)_C_Linux_Shell - Fatal编程技术网

C 如何使linux中的脚本使用我的Interpeter并工作?(#!)

C 如何使linux中的脚本使用我的Interpeter并工作?(#!),c,linux,shell,C,Linux,Shell,我为linux制作了一个简单的shell。它使用getline()逐行读取,直到将ctrl+d(eof/-1)输入标准输入 在逐行输入stdin代码时,如下所示: ls -al & ls -a -l 我的外壳很好用 我试图在shell中运行脚本,但它不起作用。当我执行脚本时,我的shell将自动执行(第1行),但shell不会解释其他行 #!/home/arbuz/Patryk/projekt/a.out ls -al & ls -a -l 是什么引起的?我不得不说,我是li

我为linux制作了一个简单的shell。它使用getline()逐行读取,直到将ctrl+d(eof/-1)输入标准输入

在逐行输入stdin代码时,如下所示:

ls -al &
ls -a -l
我的外壳很好用

我试图在shell中运行脚本,但它不起作用。当我执行脚本时,我的shell将自动执行(第1行),但shell不会解释其他行

#!/home/arbuz/Patryk/projekt/a.out
ls -al &
ls -a -l
是什么引起的?我不得不说,我是linuxes的初学者,老师并没有对所有这些东西说什么。只是一个家庭作业。我做了一些研究,但仅此而已

这是我的外壳代码。我已经在etc/shell中添加了shell路径,但它仍然不起作用

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>

int main()
{

    ssize_t bufer_size = 0;
    char* line = NULL;
    int line_size;

    while ((line_size = getline(&line, &bufer_size, stdin)) != -1) // while end of file
    {
        char** words_array;
        words_array = (char**)malloc(200 * sizeof(char*));

        int words_count = 0;
        int i;
        int j = 0;
        int words_length = 0;
        char word[100];
        for (i = 0; i < line_size; i++)
        {
            if (line[i] == ' ' || line[i] == '\n')
            {
                words_array[words_count] = (char*)malloc(words_length * sizeof(char));
                int b;
                for (b = 0; b < words_length; b++)
                {
                    words_array[words_count][b] = word[b];
                }
                j = 0;
                words_count++;
                words_length = 0;
            }
            else
            {
                word[j] = line[i];
                j++;
                words_length++;
            }
        }

        bool run_in_background = false;

        if (words_array[words_count - 1][0] == '&')
        {
            run_in_background = true;
            words_array[words_count - 1] = NULL;
        }

        int a = fork();

        if (a == 0) // child process
        {
            execvp(words_array[0], words_array);
        }
        else       // parent process
        {
            if (run_in_background == true)
            {
                printf("\n ---- running in background. \n");
            }
            else
            {
                printf("\n ---- running normal \n");
                wait(NULL);
            }
        }
    }

    return 0;
}
#包括
#包括
#包括
#包括
#包括
int main()
{
ssize_t bufer_size=0;
char*line=NULL;
内线尺寸;
while((line_size=getline(&line,&bufer_size,stdin))!=-1)//文件结束时
{
字符**字\u数组;
单词数组=(字符**)malloc(200*sizeof(字符*);
int words_count=0;
int i;
int j=0;
int字长度=0;
字符字[100];
对于(i=0;i
问题是shell读取标准输入,而she-bang
#使脚本作为命令行参数传递。所以你的外壳被称为

/home/arbuz/Patryk/projekt/a.out <script>
/home/arbuz/Patryk/projekt/a.out

。。。忽略命令行参数并等待标准输入上的命令。您必须从
argv[1]
读取脚本。您的shell必须接受命令行参数。在这种情况下,您的程序将按如下方式调用:

/home/arbuz/Patryk/projekt/a.out your_script 然后解析参数
argc
包含参数量。脚本的文件名在
argv[1]
中传递。您需要打开它(使用
fopen()
)并从中读取命令,而不是
stdin
。如果文件的第一行以
#
开头,您可能应该确保shell忽略该文件的第一行


如果调用脚本时没有绝对路径(不是以
/
开头的路径),则文件名相对于当前目录。您可以从环境中获取,也可以通过编程方式使用
getcwd()

获取任何内容。你有任何输出吗?我的shell正在运行(这样我就可以自己将脚本行写入stdin),你能给我们举个简短的例子吗?请参阅。它应列在/etc中/shells@wildplasser
/etc/shells
列出了有效的登录shell,任何程序都可以使用shebang运行。有没有办法解决这个问题?@Patryk:yes,
fopen(argv[1],“r”)
并读取其中的行。他还必须告诉解析器忽略shebang(扩展一下,任何以
#
开头的行)如果脚本位于与shell不同的文件夹中,我是否应该将shell的整个路径放在您的_脚本的位置?我是否应该设置一个条件,检查参数中是否有内容(因此它将从文件写入)或什么都没有(因此它从stdin读取)?@Patryk当前的shell可以计算出来。例如,您的shell可以这样调用:
/home/arbuz/Patryk/projekt/a.out/tmp/your_script
参数中包含路径。在本例中,
argv[1]
将指向
/tmp/your_script
@Patryk哦,你的意思是当你没有指定脚本的绝对路径时。在这种情况下,你可以从环境中获取当前目录。具体来说,是从
PWD
环境变量中获取的。@Patryk只是一个提示,你可能想运行一下,以获得一些关于处理参数的帮助。Ad另外,还有一种方法可以帮助您解析传递的脚本。
int main(int argc, char* argv[])