LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-25-2017, 11:39 AM   #1
goodhombre
Member
 
Registered: Mar 2010
Location: Ungheni, Rep. Moldova
Distribution: Ubuntu
Posts: 89

Rep: Reputation: 22
Bash question


Hi Guys,

I wonder if somebody can help me to figure this out. A program receives some input from the user and spawns a bash shell.

Code:
#include <stdlib.h>
#include <stdio.h>

/*
gcc -m32 -o test_shell  test_shell.c  
*/


int main()
{
  char buf[40];
  printf("\nGive the input:");
  fgets(buf,40,stdin);
  printf("\n[buf]: %s\n", buf);
  system("/bin/bash");
  printf("Shell closed! Bye.\n");
  return 0;
}
the problem I cannot sort out is that I cannot interact wit the new shell when I'm providing user input using pipes :

Code:
echo aaa |  ./test_shell
It just open the new shell and close it right away .

Code:
root@kali:/tmp# echo -n 'aaa' |  ./test_shell

Give the input:
[buf]: aaa

Shell closed! Bye.
The C code cannot be changed.

Thanks.
 
Old 06-25-2017, 12:36 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
What do you wish to happen? What do you wish to achieve with this program?
 
Old 06-25-2017, 12:45 PM   #3
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Yes, please explain further what your goal is.

In any case, I think the C has to be changed.
 
Old 06-25-2017, 12:48 PM   #4
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
Hi goodhombre!

Generally:
  1. when bash is run interactively, there's often some sort of terminal emulation session associated with it; when the terminal emulator is closed, the bash session ends.
  2. With your test, the input to the bash is closed after the "aaa" is sent to bash.
It will stay open, if instead it is tested like this:
Code:
while read a_line; do echo $a_line; done | test_shell

Hi!
Give the input:
[buf]: Hi!

Shell is still open, but C program won't echo any more lines because it is not looping, looking for additional input.
I compiled your test_shell.c program, and the above is the actual output, the shell session stays open.

But I do have to wonder what you think you will be able to accomplish with the C program, exactly as it is. The C program isn't going to be able to read any more input, unless it loops, or some such, looking for more input.

Last edited by rigor; 06-25-2017 at 12:49 PM.
 
1 members found this post helpful.
Old 06-25-2017, 02:26 PM   #5
goodhombre
Member
 
Registered: Mar 2010
Location: Ungheni, Rep. Moldova
Distribution: Ubuntu
Posts: 89

Original Poster
Rep: Reputation: 22
Thanks rigor a lot,

I adjusted a little bit and it worked perfectly.

Code:
root@kali:b="aaaaaa";while read a_line; do printf $b; echo $a_line;b=''; done | ./test_shell


Give the input:
[buf]: aaaaaa

whoami
printf: usage: printf [-v var] format [arguments]
root
cat test_shell.c
printf: usage: printf [-v var] format [arguments]
#include <stdlib.h>
#include <stdio.h>

/*
gcc -m32 -o test_shell  test_shell.c  
*/


int main()
{...........................


The C program was a simplified version I wrote to be easier to explain what I need to do.

The idea was the I had to feed the input for fgets from command line so I can format it properly, there were some dodgy chars I had to write them in hex .

Thanks rigor a lot .
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash question kpachopoulos Programming 2 07-20-2009 11:27 AM
Bash question fhsm Programming 8 06-12-2009 08:33 AM
bash question? kalleanka Programming 2 02-13-2006 09:57 AM
Bash Question mbjunior99 Programming 5 08-31-2005 04:33 AM
BASH question cxel91a Programming 6 12-31-2003 03:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration