0

I would like to make a script for testing my c program but I could not figure out why it does not work I tested it with a easy code so that I am sure that the problem is not because of the C file.

My C Code is:

#include <stdio.h>
#include <stdlib.h>

int main()
{
printf("Hello World\n");
return 0;
}

And my Shell Script is:

gcc -o main main.c
echo "Hello world"

If I execute the script I get the error message on my console

: No such file or directory
gcc: fatal error: no input files
compilation terminated 

This is the Error I get if I want to execute the script

https://i.sstatic.net/SP6Qr.jpg

In the image you can see my problem If I compile the C file “per hand” it has no problem but if I execute the script which contains the same statement it does not work.
If I just want to compile and only write the command for compiling in my script it works but as soon as I ad an echo or any other command it will not work.

I am using an Ubuntu Shell under Windows.

Any help would be very appreciated.

1 Answer 1

1

My guess: you wrote your script with a Windows-only editor such as Notepad, so it used Windows newlines (\r\n AKA CRLF). bash passes main.c\r as argument to gcc, which cannot find it. Printing out the error, the terminal interpret \r as carriage return character, so, it goes in column 1 and prints the rest of the message, which results in the bizarre thing you are seeing.

You can check if this is the case by running dos2unix over your script, if now it works correctly it's as I suspected.

Solution: use a more serious editor and/or make sure it writes Unix newlines (plain \n, AKA LF).

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your help I tried the vi editor and it worked perfectly Thank you very much
TBH, as much as I love vi and despise any editor that doesn't have vi keybindings, there are a lot of less hardcore alternatives that support Unix line endings... :-) Pretty much anything besides notepad do (e.g. Notepad++ is a popular alternative and does support Unix line endings). Just make sure to check the corresponding option when first saving the file.
Thank you very much this helps a beginner like myself a lot the vi editor is surely a great editor but for a beginner like me it was very overwhelming and Notepad++ suits my needs perfect thanks for the advise :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.