Hey! Here's a Command Line Interface application I've written in C language that works like a Linux shell
Here are some important Linux commands that I've tested on my Linux shell application
![commands_1](https://private-user-images.githubusercontent.com/85508314/311488702-8098031e-be1a-43c3-9c99-f32f6dfb04be.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU5ODY4MzMsIm5iZiI6MTcyNTk4NjUzMywicGF0aCI6Ii84NTUwODMxNC8zMTE0ODg3MDItODA5ODAzMWUtYmUxYS00M2MzLTljOTktZjMyZjZkZmIwNGJlLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA5MTAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwOTEwVDE2NDIxM1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWY0MTQwYTZjMmExZDI1YWMxNDA2YTIwMWJjNWI2NjdiMmFmNjQ1YzRiOGIwNTlmYTdkOWI4NjQ1ODY3NmUxMDMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.ZEo85aXx1I7cmeCIH0iMZbxvV5TBpo1vnRJVNYC0VWg)
![commands_2](https://private-user-images.githubusercontent.com/85508314/311488774-daac292d-25e7-4e2f-be2b-538baca8a945.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU5ODY4MzMsIm5iZiI6MTcyNTk4NjUzMywicGF0aCI6Ii84NTUwODMxNC8zMTE0ODg3NzQtZGFhYzI5MmQtMjVlNy00ZTJmLWJlMmItNTM4YmFjYThhOTQ1LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA5MTAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwOTEwVDE2NDIxM1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTFjMWY2YTQzNDEyZThlNTVhYWU5ZWIzYjkzYmMzNDBjMmExNzI5YWUyOWE4MTRjOTg0MTc3Y2U5YTJiMjc2Y2MmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.RylC7HnY7Zqu3G2rb5w9AyEWRb2DOXCqdtaVIeeX4m8)
- The main( ) function executes a while loop We need this loop to run the shell input command infinitely after execution of each command
- The shell function does the following job :
- It stores the user input in the character array cmd[100] (string)
- length of this string cmd is stored in l
- We pass this string cmd and its length l to a token function tok(cmd,l)
- The tok(cmd, l) function's return type is a 2D array (an array of strings)
- This function takes the cmd string and tokenizes it into a group of strings
- A global variable g is used to store the number of tokens obtained
- The array of strings returned by token function is stored in a
- Now, we compare the first token a[0] given by the user with cd, echo, pwd, If the token matches with any one of them, its respective function is called :
- Cd(a) function uses chdir(a[1]), an in-built function to change the directory
- Echo(a) function concats all the tokens from a[1] to a[g-1] and stores in a string b
- A for loop is used to remove " character
- Pwd( ) function uses getcwd( ), an in-built function to get the current working directory
- Cmd(a) function is used to access commands like mkdir, ls, cat, rm, date executables in the bin folder using execvp( )
- In this Cmd(a) function, fork( ) function is used to initialize a child process
- In the child process execvp is used. Its return value is stored in a variable z
- If z != 0, It displays an error
- execvp takes the input command and the array of strings as arguments
- execvp function calls the file from bin folder according to the user input
- Meanwhile, the parent process waits for the child process to exit
- After leaving the if-else loop and executing one of the Cd( ), Pwd( ), Echo( ), Cmd( ) functions, the array a memory is freed
- Because we have a while loop in our Main function and it breaks only when character ch != '\n'
- At each step, it calls the shell function again