473,602 Members | 2,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

implementing a bash shell

1 New Member
hi all,
im tring to implement a bash shell with some operations like(pipe,redir ection etc).im using execvp system call to achieve this.im stuck at redirecting the outut of a command to a file.the output im getting for the follwoing code is "*** ERROR: exec failed\n".....n y help??????????? ????
Expand|Select|Wrap|Line Numbers
  1. #include  <stdio.h>
  2. #include  <sys/types.h>
  3. #include  <stdlib.h>
  4. #include  <string.h>
  5. #include  <fcntl.h>
  6. void  parse(char *line, char **argv)
  7. {
  8.         while (*line != '\0')
  9.         {
  10.                 while (*line == ' ' || *line == '\t' || *line == '\n')
  11.                         *line++ = '\0';
  12.                 *argv++ = line;
  13.                 while (*line != '\0' && *line != ' ' &&
  14.                                 *line != '\t' && *line != '\n')
  15.                         line++;
  16.         }
  17.         *argv = '\0';
  18. }
  19.  
  20.  
  21. main(void)
  22. {
  23.         char  line[1024];
  24.         char  *argv[64];
  25.         char **envp;
  26.         char command[50][20];
  27.         int inredir=0,outredir=0; //FLAGS FOR CHECKING WHETHER REDIRECTIONS EXIST OR NOT
  28.         char in_redir[100],out_redir[100];
  29.         while (1)
  30.         {
  31.                 printf("Shell -> ");
  32.                 gets(line);
  33.                 printf("\n");
  34.                 parse(line, argv);
  35.                 if (strcmp(argv[0], "exit") == 0)
  36.                         exit(0);
  37.                 //execute(argv);
  38.                 int i=0,j=0;
  39.                 while(argv[i])
  40.                 {
  41.                         //printf("%s\n",argv[i]);
  42.                         if(strcmp(argv[i],"<")==0)
  43.                         {
  44.                                 inredir=1;
  45.                                 strcpy(in_redir,argv[++i]);
  46.                         }
  47.                         else if(strcmp(argv[i],">")==0)
  48.                         {
  49.                                 outredir=1;
  50.                                 strcpy(out_redir,argv[++i]);
  51.                         }
  52.                         else
  53.                                 strcpy(command[j++],argv[i]);
  54.                         i++;
  55.                 }
  56.                 strcpy(command[j],"\0");
  57.                 pid_t  pid;
  58.                 int    status,fd1,fd2;
  59.                 if ((pid = fork()) < 0)
  60.                 {
  61.                         printf("*** ERROR: forking child process failed\n");
  62.                         exit(1);
  63.                 }
  64.                 else if (pid == 0)
  65.                 {
  66.                         if(inredir)
  67.                         {
  68.                                 fd1 = open(in_redir, O_RDONLY);
  69.                                 if (fd1 < 0)
  70.                                 {
  71.                                         perror("catf1f2: f1");
  72.                                         exit(1);
  73.                                 }
  74.  
  75.                                 if (dup2(fd1, 0) != 0)
  76.                                 {
  77.                                         perror("catf1f2: dup2(f1, 0)");
  78.                                         exit(1);
  79.                                 }
  80.                                 close(fd1);
  81.                         }
  82.                         if(outredir)
  83.                         {
  84.                                 fd2 = open(out_redir, O_WRONLY | O_TRUNC | O_CREAT, 0644);
  85.                                 if (fd2 < 0)
  86.                                 {
  87.                                         perror("catf1f2: f2");
  88.                                         exit(2);
  89.                                 }
  90.                                 if (dup2(fd2, 1) != 1)
  91.                                 {
  92.                                         perror("catf1f2: dup2(f2, 1)");
  93.                                         exit(1);
  94.                                 }
  95.                                 close(fd2);
  96.                         }
  97.                         if (execvp(command[0],command))
  98.                         {
  99.                                 printf("*** ERROR: exec failed\n");
  100.                                 exit(1);
  101.                         }
  102.                 }
  103.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 }
  104.  
im using a gcc compiler on fc6...
Aug 11 '07 #1
0 2967

Sign in to post your reply or Sign up for a free account.

Similar topics

3
8250
by: John Bowling | last post by:
I have a java (2.0) program with the following lines: String cmdArray1 = {"lp", "-d", "hp4m", "MyFile"}; System.out.println(Runtime.getRuntime().exec(cmdArray1)); It compliles properly, but does not print the file to the printer. It displays the following as the return from Runtime...: java.lang.UNIXProcess@1034bb5
3
2667
by: William Park | last post by:
I'm very excited to announce shell interface to GTK+2 (2.6.1) for Bash. It read XML syntax describing the widget layout, and returns user selection as shell variable or runs shell command as callback. It's designed for simple GUI dialog or layout, with the emphasis on getting the user data back into shell. For the moment, the shell variable and command are disabled. It just prints to stdout, instead. But, you can change it easily.
11
2986
by: Magnus Jonneryd | last post by:
Hi, I'm planning on writing a program that interactively is fed input via a shell (bash). I also want to be able to write a shell script that executes various commands related to my program. In short i want to provide input to a program using some (or all) of the functionality found in bash. It's mainly the format of the file I'm having trouble with. I wanted to be able to write something like this: #!/bin/bash for x in xs
7
5562
by: chakkaradeepcc | last post by:
HI all, How to execute bash scripts from python (other than using os.popen) and get the values that those bash scripts return. I would be happy if someone could help me out in this.. thanks in advance.. With Regards,
16
3931
by: John Salerno | last post by:
Hi all. I just installed Ubuntu and I'm learning how to use the bash shell. Aside from the normal commands you can use, I was wondering if it's possible to use Python from the terminal instead of the normal bash commands (e.g. print instead of echo). My main reason for asking is that I like using Python for everything, and if I don't need to learn the bash 'language', then I won't just yet. Thanks.
14
4884
keyvanrahmadi
by: keyvanrahmadi | last post by:
Sorry if this post is rather long but hopefully you wont get bored half way through. I have a project in hand which i have started and have a deadline of 1 week. Basically what i need to do is to create a shell script in Bash for the game Hangman. I am sure everyone is well aware of the game. The down side is that i can not use "sed" or "awk". FUN FUN I am at my ends wit now and truley utterly lost, so any help would go a long way to stop...
10
4427
by: Isaac Gouy | last post by:
$ ./test_fail 1 Floating point exception - but this leaves 'somefile' zero size $ ./test_fail 1>somefile Floating point exception
1
3259
by: Fredrik Lundh | last post by:
John Lawrence wrote: doesn't exactly work for Python scripts, though: $ cat env.py #!/usr/bin/env python import os os.environ = "hello"
6
1866
by: Frantisek Malina | last post by:
What is the best way to do the regular bash commands in native python? - create directory - create file - make a symlink - copy a file to another directory - move a file - set permissions I need to write a program that creates real application/FTP accounts
0
7993
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8404
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6730
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5867
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2418
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1254
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.