Building a simple Shell for linux

Linux using c programming language.- He needs to be able to recognize a command, such as 'copy2flash', and print a message.- Warren is unsure how to do this using an array of chars.- He includes several libraries and initializes some variables.- The program prompts the user with a welcome message and instructions.- It uses a while loop to continuously get input from the user.- Inside the loop, there is a switch statement that checks for different cases, including the 'copy2flash' command.- If the command is recognized, the program prints a message and clears a temporary variable.- Otherwise, it adds the input character by character to the temporary variable.- In summary, Warren
  • #1
FrostScYthe
80
0
Hi,

I'm trying to build a shell for Linux, programming it in c currently. I need to be able to read a command like 'copy2flash' so that it'll copy itself to another flash memory, but right now I need the case instruction to be able to recognize the command and print something like "copying to device" I'm not sure how to do this with an array of chars?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <string.h>

#define MAX 80 /* 80 chars per line, per command, should be enough. */

int main(int argc, char *argv[], char *envp[])
{
/* Inicializa a c y tmp. */
char string [MAX];
char c = '\0';
char tmp = '\0';
printf("Welcome to MySHELL, use 'copy2flash' to copy flash memory\n");
printf("to another flash memory to another system\n");
printf("[MySHELL] ");
while(c != EOF) {
c = getchar();
switch(c) {
case '\n': /* parse y ejecute. */
printf("[MySHELL] ");
break;
/**** case 'copy2flash': parse y ejecute.
printf("Copying to another flash...");
bzero(&tmp, 1);
break; */
default: strncat(&tmp, &c, 1);
break;
}
}
/* some processing before terminating. */
return 0;
}
 
Technology news on Phys.org
  • #2
Use the string comparison methods like strcmp().

- Warren
 
  • #3


Hi there,

Building a shell for Linux can be a challenging yet rewarding project. It seems like you have a good start with your code, but there are a few things that can be improved upon.

Firstly, when reading user input, it's important to use a loop to continually read characters until the user presses enter. This way, you can process the entire command at once instead of character by character. Additionally, instead of using getchar(), you can use fgets() to read the entire line at once and then process it accordingly.

Next, for recognizing and executing commands, you can use a combination of string comparison functions like strcmp() and strtok() to parse the user input and determine which command is being used. For example, for your 'copy2flash' command, you can use strcmp() to compare the user input with the command and then use strtok() to get the arguments (if any) for the command.

Finally, for printing messages like "Copying to another flash...", you can use printf() and format strings to dynamically print messages based on the user input.

Overall, building a shell for Linux is a great project to improve your programming skills and understanding of the operating system. Good luck with your project!
 

1. What is a simple Shell for Linux?

A simple Shell for Linux is a program that serves as a command-line interface for interacting with the operating system. It allows users to execute commands, run programs, and manage files and directories.

2. Why would I want to build a simple Shell for Linux?

Building a simple Shell for Linux can be a useful learning experience for those interested in computer science and programming. It can also be a valuable tool for automating tasks and managing systems.

3. What are the basic components of a simple Shell for Linux?

A simple Shell for Linux typically consists of a command parser, a command executor, and a user interface. The command parser interprets user input, the command executor executes the commands, and the user interface displays information and prompts for input.

4. What programming language is commonly used to build a simple Shell for Linux?

C is the most commonly used programming language for building a simple Shell for Linux. Other languages such as Python and Ruby can also be used.

5. Are there any existing resources or tutorials for building a simple Shell for Linux?

Yes, there are many online resources and tutorials available for building a simple Shell for Linux. Some recommended resources include the Linux Shell Scripting Tutorial by Steve Parker and the BashGuide by Maarten Billemont.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
883
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Computing and Technology
Replies
7
Views
3K
  • Aerospace Engineering
Replies
2
Views
7K
  • Special and General Relativity
Replies
13
Views
2K
Back
Top