How to make a linux script use my interpteter and work? (#!)

I created a simple linux shell. It reads line by line with getline () until ctrl + d (eof / -1) is entered into standard input.

When entering stdin string using string code:

ls -al & ls -a -l 

My shell works very well.

I tried to run the script through my shell, but it does not work. When I execute the script, my shell is automatically executed (1st line), but the shell does not interpret other lines.

 #!/home/arbuz/Patryk/projekt/a.out ls -al & ls -a -l 

What can cause this? I have to say that I am very new to Linux, and the teacher said nothing about all this. Just homework. I did some research, but all I found.

Here is the code of my Shell. I added shell path to etc / shells but it still doesn't work

 #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; } 
+8
c linux shell
source share
2 answers

Your shell should accept command line arguments. In this case, your program will be called like this:

  /home/arbuz/Patryk/projekt/a.out your_script 

So, you will need main() this signature:

 int main(int argc, char* argv[]) 

and then parse the arguments. argc contains the number of arguments. The script file name is passed to argv[1] . You will need to open it (using fopen() ) and read the commands from it instead of stdin . You should probably make sure your shell ignores the first line of the file if it starts with # .

If your script is called without an absolute path (a path that does not start with / ), then the file name refers to the current directory. You can get this from the environment or programmatically using getcwd() .

+12
source share

The problem is that your shell is reading from standard input, and she-bang #! causes the script to be passed as a command line argument. So your shell is called

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

... ignores the command line argument and waits for commands on standard input. You should read the script from argv[1] .

+6
source share

All Articles