Home > other >  How to use c function system(command) and escape character "\3" on Linux
How to use c function system(command) and escape character "\3" on Linux

Time:08-13

I wrote code using CodeBlocks on Windows. But I met with problems when I run the code on Linux, using its GCC compiler. While using the function system("title heartshape") and system("color XX") on Linux, it shows this error continuously :

"sh: color: command not found"

Line 12 to 43 in the code are ones that can draw a heart shape, which is unused in this problem, and the for(;;) loop after that can change the color of the heart shape over time continuously.

The source code is as follows:

#include <math.h>
#include <stdlib.h>
#define I 20
#define R 340
int main(void)
{
    int i, j, e;
    int a;
    long time;
    system("title Heartshape");
/*codes that use to draw a heart, unhelpful to my question*/
    for (i = 1, a = I; i < I / 2; i  , a--)
    {
        for (j = (int)(I - sqrt(I * I - (a - i) * (a - i))); j > 0; j--)
            printf(" ");
        for (e = 1; e <= 2 * sqrt(I * I - (a - i) * (a - i)); e  )
            printf("\3");
        for (j = (int)(2 * (I - sqrt(I * I - (a - i) * (a - i)))); j > 0; j--)
            printf(" ");
        for (e = 1; e <= 2 * sqrt(I * I - (a - i) * (a - i)); e  )
            printf("\3");
        printf("\n");
    }
    for (i = 1; i < 80; i  )
    {
        printf("\3");
    }
    printf("\n");
    for (i = 1; i <= R / 2; i  )
    {
        if (i % 2 || i % 3)
            continue;
        for (j = (int)(R - sqrt(R * R - i * i)); j > 0; j--)
            printf(" ");
        for (e = 1; e <= 2 * (sqrt(R * R - i * i) - (R - 2 * I)); e  )
            printf("\3");
        printf("\n");
    }
/*codes that use to change the color of the heart*/
    for (;;)
    {
        system("color a");
        for (time = 0; time < 99999999; time  );

        system("color b");
        for (time = 0; time < 99999999; time  );

        system("color c");
        for (time = 0; time < 99999999; time  );

        system("color d");
        for (time = 0; time < 99999999; time  );

        system("color e");
        for (time = 0; time < 99999999; time  );

        system("color f");
        for (time = 0; time < 99999999; time  );

        system("color 0");
        for (time = 0; time < 99999999; time  );

        system("color 1");
        for (time = 0; time < 99999999; time  );

        system("color 2");
        for (time = 0; time < 99999999; time  );

        system("color 3");
        for (time = 0; time < 99999999; time  );

        system("color 4");
        for (time = 0; time < 99999999; time  );

        system("color 5");
        for (time = 0; time < 99999999; time  );

        system("color 6");
        for (time = 0; time < 99999999; time  );

        system("color 7");
        for (time = 0; time < 99999999; time  );

        system("color 8");
        for (time = 0; time < 99999999; time  );

        system("color 9");
        for (time = 0; time < 99999999; time  );
    }
    return 0;
}

That's the whole source code of my project.

The code can be run perfectly on Windows compiler, but keep showing the error sh: color: command not foundon Linux.

In my limited experience with Linux for just a week, I can only assume that there're differences between Windows and Linux while using system()in c, and the escape character \3 won't show on Linux as well.

I've found the relevant question here, but the question seems remain unsolved, at least the answer doesn't help me. Other answers seems too complicated to solve this question.

I wanted to know the differences in using the function system(),as well as if there's any good practice to change the color of the heart shape continuously on Linux? Or are there alternatives for this?

CodePudding user response:

The problem is not with the function system(), even though it does different things under the hood. Of course, because Windows and Linux are different operation systems. But as a standard library function, it generally does the same for you on both systems: it lets the command processor of the OS execute an internal command or launch another program.

The problem is with the programs (commands) you try to execute. The commands "title" and "color" are unknown to my shell (bash on Arch Linux), but are known to Windows' shell ("cmd.exe").

A similar issue is your output of '\3'. This special character is only shown with the specific font of your shell.

You need to take another approach. There are several possible solutions, for example using "ncurses" as an OS independent library.

  • Related