c executing shell problem


 
Thread Tools Search this Thread
Top Forums Programming c executing shell problem
# 1  
Old 11-27-2009
c executing shell problem

Hello
im geting error here:

Code:
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main (int argc, char *argv[])
{
char user[20];
string command;
 cin << user;
command = printf ("grep '%s' /etc/shadow", user);

system (command.c_str());
 }
 return 0;
}

it should search shadow file for a entered user, but it shows "sh not found"
# 2  
Old 11-27-2009
to start off:
Code:
printf ("grep '%s' /etc/shadow", user);

Is not a valid command in sh or bash.

What system does -
1. fork a new process
2. exec a shell in the child process and pass the arguments to to it
3. wait until the child process is done
4. get status from wait (see man 2 wait nad man system for exact details)

When you are at the command line:
show the output of
Code:
ls /bin/sh

What does your system manpage say about the default name of the shell it intends to use? -- it may refer you to exec...
# 3  
Old 11-27-2009
Code:
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main (int argc, char *argv[])
{
char user[20];
string command;
 cin << user;
command = printf ("grep '%s' /etc/shadow", user);

system (command.c_str());
 }
 return 0;
}

I'm surprised this even compiles. How did you get namespace::std or cin without iostream?

also, printf doesn't work that way. printf prints to stdout. If you want to print to a string, you use sprintf, like this:

Code:
char buf[512];
sprintf(buf, "grep '%s' /etc/shadow", user);



---------- Post updated at 12:49 PM ---------- Previous update was at 12:48 PM ----------

Lastly, why write a C command to run a shell command? wouldn't it be easier to run the shell command in the first place?
# 4  
Old 11-28-2009
Why not do it all from within C using the getpwent() et al APIs? Much safer.
# 5  
Old 12-03-2009
hi..
why dont you use exec family of functions to execute shell scripts from within the C program.
you can use execlp.
plz check the exec family of functions. its the most portable and simple way of executing shell commands. plz reply to extend this conversation. Smilie
regards
ttyl
# 6  
Old 12-05-2009
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main (int argc, char *argv[])
{
char user[20];
string command; ---> char command[25];
cin << user; ----> cin>>user;
command = printf ("grep '%s' /etc/shadow", user); --->

sprintf(command,"grep '%s' /etc/shadow", user)

system (command.c_str());

return 0;
}


Try this..
# 7  
Old 12-06-2009
Please use
Code:
code tags

Why to mix printf with cin, just use scanf instead of cin?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in executing For loop....

Hi frnds I trying to execute the following ksh, #!/bin/ksh file=$OBS_APP_PATH/config/com/uhg/obs/inbound/configs/ServiceFeeDetail.xml inputFileDir=$OBS_APP_PATH/config/com/uhg/obs/inbound/configs/ echo $file echo $cntWrd if then echo "Inside IF" for i in 'ls -t1... (7 Replies)
Discussion started by: balesh
7 Replies

2. Shell Programming and Scripting

problem executing awk in shell "not found"

Hello, The INPUT file a.txt contains this a a a b b b I'm trying to execute this shell script from the Unix Command Line like this: ./k.sh a.txt > newfile.txt #!/usr/bin/sh infile="$1" awk '{print $0;}' < $infile I get this error message on the command line: (9 Replies)
Discussion started by: script_op2a
9 Replies

3. Shell Programming and Scripting

Problem with executing a shell script through the cron

Hi, I have a shell script as below: ORACLE_HOME=/usr/local/opt/oracle/product/dev export ORACLE_HOME PATH=$PATH:$ORACLE_HOME/bin:/usr/bin export PATH OUTFILE=/export/home/`basename $0`.out export OUTFILE export IDEN df -k . | tail -1 | read a b c d e f echo $a >> $OUTFILE echo $b... (4 Replies)
Discussion started by: Abhinav Pandey
4 Replies

4. Shell Programming and Scripting

Problem Executing Firmware Command using Shell Script

Guys, I have a script that should change one of the configuration Parameter in a http accelerator, this config change which will halt http traffic into device. So I have designed a script which should do these changes. But after executing this script, found that one of the input variable is not... (8 Replies)
Discussion started by: raghunsi
8 Replies

5. AIX

Having problem with executing shell script to get the pid details!

Hello Everyone, I am new to shell scripting and also i am very new to AIX machine I have a question. I have shell script which contains the follwing "ps -e |grep $1 echo $2" i run this schell script by doing this ./startSehllscript 3444 passed with two parameters . Accroiding my... (4 Replies)
Discussion started by: swati
4 Replies

6. Shell Programming and Scripting

Problem executing a script

Hi Friends, When I try to execute one of my script script1.shl, I am getting the error message as "/tmp/sh1871978.13: cannot create" . I could not find the specified sh* file in the /tmp directory and I also checked the disk space of /tmp and it is only 60% filled. Please help me on... (5 Replies)
Discussion started by: mr_manii
5 Replies

7. Shell Programming and Scripting

Problem executing script through at

Hey fellows, i've got a probkem while executing a command through a scheduled at-job; Somehow my script starts looping, when triggered throug at; It doesnt do so if started manually. Any ideas? #!/bin/sh # atrun uid=33 gid=33 # mail www-data 0 umask 22 cd /var/django/webripper/ripper... (2 Replies)
Discussion started by: viktor4124
2 Replies

8. Shell Programming and Scripting

Problem executing a script

The script startserver.sh has permissions to execute. Still the nohup command returns error with 'No such file or directory' Any sugggestions: $ nohup ./startserver.sh & Error $ nohup: appending output to `nohup.out' nohup: cannot run command `./startserver.sh': No such file or... (4 Replies)
Discussion started by: hemangjani
4 Replies

9. Shell Programming and Scripting

Problem executing nohup

I am trying to submit background jobs using the nohup command on a client system where my session is running under a "master shell" (BASH). If I try to nohup the actual job (ie: nohup MYJOB.BAT > MYJOB.LOG 2>&1 &) the command will fail with a return code of 126 and a "permission denied" message.... (0 Replies)
Discussion started by: christyw
0 Replies

10. UNIX for Advanced & Expert Users

problem in executing a file

All, I've a script that I'm trying to execute with crontab. But it was not working. Then when i checked executing the script manually, I've found a strange thing. the script executes only with sh <SCRIPTNAME> and not with ./<SCRIPTNAME> I'm using Red hat 7.3, and the error that i'm getting... (2 Replies)
Discussion started by: i2admin
2 Replies
Login or Register to Ask a Question