Open In App

Greeting By Your Favourite Movie Quotes in Rainbow Colors on Every Terminal Launch

Last Updated : 01 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Would not it be cool, if you would be greeted by your favorite movie quotes? Let us see how to make this happen. Before moving further, please fire the below-mentioned command on your Linux terminal.

sudo apt-get install lolcat

Let us focus on some shell commands now.

  • echo “message” – To print a message on the terminal.
  • cat filename – To show the contents of the file on terminal
  • lolcat filename – To show the contents of a file on the terminal in rainbow colors.

Suppose we have a text file, saved as file.txt, in which we have written “GeeksforGeeks” five times.

taking a sample file

displaying the contents of the file

Whatever we write in front of echo inside “”, will be printed as it is on the terminal. For example, firing the command echo “We are gonna look at contents of file.txt” will print “We are gonna look at contents of file.txt” on the terminal. Firing the command, cat file.txt will show the contents of file.txt on terminal. Firing the command, lolcat filename will show the contents of file in rainbow colors on the terminal.
So if we fire command lolcat file.txt. Contents of file.txt will be seen on terminal, but in rainbow colors.

Suppose we want to print a message on the terminal, in rainbow colors. Then how do we do that? Here we will combine “echo” and “lolcat” together. Consider the picture given below

combining echo and lolcat command

Let us understand command echo GeeksforGeeks | lolcat. Here, ” | ” in shell scripting is called a pipe. With the help of pipe we pushed output of echo that was “GeeksforGeeks” to lolcat as input. So lolcat accepted it and printed it(“GeeksforGeeks”) in rainbow colors.

But, how to do same thing with the help of a C program.

Run the below code on your Linux system only, after installing lolcat. Online IDE does not have provisions for lolcat.




#include<stdio.h>
#include<stdlib.h> 
void main() {
  
        for(int i = 1; i <= 5; i++)
                system("echo GeeksforGeeks | lolcat");
}


C program to print in Linux

To run any shell command from the “C program” we use the system(” “). Any shell command passed to system(“”) in C program, is executed from that C program. Here we passed shell command “echo GeeksforGeeks | lolcat” to system(). And it was executed. GeeksforGeeks was printed in rainbow colors as output of our C program. Now let us move to the actual problem statement, with the help of the program, given below.

Run the below code on your Linux system only, after installing lolcat. Online IDE does not have provisions for lolcat.




#include<stdio.h>
#include<stdlib.h>
#include<time.h>
   
void main() {
   
srand(time(NULL));
int number = rand();
number = number % 10;
  
  
switch(number) {
  
    case 0 : system("echo “Truth can be found in one place: the code” | lolcat");
              system("echo - Thor, Thor: The Dark World | lolcat");
              break;
  
    case 1 : system("echo “Part of the journey is the end.” | lolcat");
             system("echo - Tony Stark, Avengers: Endgame | lolcat");
             break;
  
    case 2 : system("echo “Tony, trying to get you to stop has been one of the few failures of my entire life.” | lolcat");
             system("echo - Pepper Potts, Avengers: Endgame | lolcat");
             break;
  
    case 3 : system("echo “No amount of money ever bought a second of time.” | lolcat");
             system("echo - Tony Stark, Avengers: Endgame | lolcat");
             break;
  
    case 4 : system("echo “You know, I keep telling everybody they should move on and grow. Some do. But not us.” | lolcat");
             system("echo - Steve Rogers, Avengers: Endgame | lolcat");
             break;
  
    case 5 : system("echo “It's not about how much we lost. It's about how much we have left.” | lolcat");
             system("echo - Tony Stark, Avengers: Endgame | lolcat");
             break;
  
    case 6 : system("echo “No mistakes. No do-overs. Look out for each other. This is the fight of our lives.” | lolcat");
             system("echo - Steve Rogers, Avengers: Endgame | lolcat");
             break;
  
    case 7 : system("echo “The hardest choices require the strongest wills.” | lolcat");
             system("echo - Thanos, Avengers: Endgame | lolcat");
             break;
  
    case 8 : system("echo “Today we don’t fight for one life, we fight for all of them.”| lolcat");
             system("echo - Black Panther, Avengers: Endgame | lolcat");
             break;
  
    case 9 : system("echo “It’s not enough to be against something. You have to be for something better.” | lolcat");
             system("echo - Tony Stark, Captain America: Civil War | lolcat");
             break;
  
}
}


Let us save the program by name quotes.c We have actually written a C program, which generates a random number, and on that random number we have performed mod by 10. After performing mod by 10 on any random number, the resultant number will be any number between 0 to 9 (0 and 9 inclusive). And we will print a quote in rainbow color for resultant value generated.

executing c program

So every time our program runs, a number between 0 to 9 is generated and any quote out of 10 quotes is printed in rainbow colors.

Now, how do we run the program shown above, by default on each terminal launch? Let us save the program quotes.c(or any name of your choice) in the home folder. Fire this shell command on terminal, to hide quotes.c

mv quotes.c .quotes.c

mv renames quotes.c to .quotes.c In Linux adding “.” before any file, hides the file. Now, quotes.c exists in your home folder but in hidden mode. Install gcc(if you do not have it) by firing a shell command on terminal

sudo apt-get install gcc

To run .quotes.c by default, let us focus on a file called “.bashrc” The .bashrc file is a script that is executed whenever a new terminal session is started. Open .bashrc by typing command “gedit .bashrc” on terminal. Add two lines at the bottom of .bashrc file.

  • gcc .quotes.c (to compile our hidden program)
  • ./a.out (to run our hidden program) and save the changes done in .bashrc

Finally, your terminal should be looking like this, after launching.

terminal launching with Quotes

Note: An a.out file will appear in the home folder after every terminal launch. You can write your favorite quotes, instead of marvel movie quotes(as shown here). You can increase the number of quotes to be chosen from like you can generate a random number and perform mod by 50. So you have to write quotes for cases 0 – 49, and any random quote out of 50 your quotes will be printed.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads