Building a simple Shell for linux

Click For Summary
SUMMARY

The discussion focuses on building a simple shell for Linux using C programming. The user aims to implement a command called 'copy2flash' that copies the shell to a flash memory device. The provided code snippet initializes a character array and utilizes a switch statement to handle user input. The user seeks guidance on recognizing the command and executing the corresponding action using string comparison methods like strcmp().

PREREQUISITES
  • C programming language
  • Linux operating system basics
  • Understanding of character arrays in C
  • Familiarity with string manipulation functions in C
NEXT STEPS
  • Implement command recognition using strcmp() in the shell code
  • Explore file handling in C for copying files to flash memory
  • Learn about process management in Linux for executing commands
  • Research error handling techniques in C programming
USEFUL FOR

Developers interested in systems programming, particularly those looking to create custom shells or enhance their understanding of Linux command execution.

FrostScYthe
Messages
80
Reaction score
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
Use the string comparison methods like strcmp().

- Warren
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
4K
  • · Replies 13 ·
Replies
13
Views
21K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
11K