0

I would like to edit my Apple Script file to improve the format of the output in Mac Terminal.

Background to the problem:

I am new to learning C, and my knowledge of bash is rather limited. I have no experience in writing scripts.

When compiling the C language via TextWrangler, I save the following code in a ".c" file:

#include <stdio.h>

int main() {
    printf("Hello, World!");
    return 0;
}

and I save it in a directory called "C."

In that directory, I saved a script file that can be found here. For your convenience, I've attached the AppleScript file here:

enter image description here

Now, when I go to "compile and run" in the TextWrangler menu bar, the program compiles successfully- but the output view isn't really all that great. Here's what it looks like:

Users-MacBook-Pro:C username$ cd 
'/Users/username/Desktop/Self_Learning/C/'; gcc -o output'
hello_world.c';'/Users/username/Desktop/Self_Learning/C/'output
Hello, World!Users-MacBook-Pro:C username$ 

(For what it's worth, there are no differing font colors in the Terminal; assume all of it is black).

What I'd like to do- and what I hope people here can help me achieve- is change the script so that the following is not visible:

'/Users/username/Desktop/Self_Learning/C/'; gcc -o output'
hello_world.c';'/Users/username/Desktop/Self_Learning/C/'output

and that right after Hello, world! output, the bash prompt goes to a new line instead of staying on the same line as the output. In other words, I'd like to see something like this:

Users-MacBook-Pro:C #very minimal text here 
Hello, World! #output 
Users-MacBook-Pro:C username$ #new line 

Thank you in advance for anyone who could help shed light on how I can go about doing this. My motivation for this adjustment is when I run a C file with lots of code and output, I'd like to quickly see any debugging errors that the Terminal might output, and not confuse that pertinent information with compiling commands.

daOnlyBG
  • 595
  • 4
  • 20
  • 49
  • 1
    you could write a makefile, where 1) each line is prefixed by '-' so the line is not echo'd and redirect the stdout output from the compiler and linker to /dev/null using '>'. – user3629249 Jul 18 '15 at 21:32

1 Answers1

0

You forgot to add a new line after the write 'hello world'. Try this code:

#include <stdio.h>

int main() {
    printf("Hello, World!\r\n");
    return 0;
}
Antonio Ragagnin
  • 2,278
  • 4
  • 24
  • 39
  • Thank you, but does that mean I'm going to have to use printf( "\r\n"); inside each C program? I'm wondering if there's a way to automatically include a break for each C file so that one doesn't have to include that each time. – daOnlyBG Jul 18 '15 at 20:59
  • @daOnlyBG Yes you have. C is a "low level" language. There are many scenarios where you do not want a new line, so C does not force it. If the answer is correct, please upvote and set as best answer. – Antonio Ragagnin Jul 18 '15 at 21:03
  • 1
    @daOnlyBG: No, you don't need the `\r`, just the `\n`. The last character you print to a text stream (in this case, to `stdout`) should be a newline. You don't necessarily need it on each call; you can legally do `printf("hello"); printf(", world\n");` Note that the `puts` function prints a single string and appends a newline, so `puts("hello")` is equivalent to `printf("hello\n")`. But `printf` is more flexible. – Keith Thompson Jul 18 '15 at 21:15
  • Thank you, @AntonioRagagnin. I understand C is a "low level language," and I'm not proposing anything be changed with a C compiler or anything else concerning with C, for that matter. I would just like to change the bash code so that the Terminal output looks better. – daOnlyBG Jul 18 '15 at 21:17