From owner-freebsd-questions Fri May 18 17:17:36 2001 Delivered-To: freebsd-questions@freebsd.org Received: from guru.mired.org (okc-65-26-235-186.mmcable.com [65.26.235.186]) by hub.freebsd.org (Postfix) with SMTP id BA1EA37B43C for ; Fri, 18 May 2001 17:17:33 -0700 (PDT) (envelope-from mwm@mired.org) Received: (qmail 55691 invoked by uid 100); 19 May 2001 00:17:33 -0000 From: Mike Meyer MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <15109.48029.122640.995173@guru.mired.org> Date: Fri, 18 May 2001 19:17:33 -0500 To: Eric Boucher Cc: questions@freebsd.org Subject: Re: C programming: how to execute command line? In-Reply-To: <125517501@toto.iv> X-Mailer: VM 6.90 under 21.1 (patch 14) "Cuyahoga Valley" XEmacs Lucid X-face: "5Mnwy%?j>IIV\)A=):rjWL~NB2aH[}Yq8Z=u~vJ`"(,&SiLvbbz2W`;h9L,Yg`+vb1>RG% *h+%X^n0EZd>TM8_IB;a8F?(Fb"lw'IgCoyM.[Lg#r\ Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG Eric Boucher types: > Hi everyone, > > I'm beggining programming in the C language and I want > to know something: is it possible to simulate that a > user have enter a command in a shell, and catch the > exit code in a variable. Here is an exemple of what I > want to do to be more explicit. > > Imagine that I want to catch the exit code if I'm in a > shell prompt, I would write: > > $ ls -l; echo $? > > and the exit code will be displayed on the screen. So > now, what I want to do is to catch this exit code in > my C program. I think it will be something like this: > > int var; > > ... > ... > > var = the_command_that_will_simulate_the_command_line var = system("ls -l") is the easy way. That passes the command line directly to the shell, doing expansion and so on. If you need more control, Josef Grosch has provided pointers to the appropriate system calls. http://www.mired.org/home/mwm/ Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message