Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with brackets using shell_exec

Status
Not open for further replies.

229257

Programmer
Apr 23, 2002
54
GB
Hi,

Was wondering if someone could help with the following problem? I want to execute a shell command but i think there is a problem with the brackets because i get a parse error. The command works from the command line but not through PHP. Any ideas?

$output = shell_exec('ypcat passwd | awk '{if (substr($0,1,2)=="sh") print}'');

Thanks
00229257
 
$output = shell_exec([red]'[/red]ypcat passwd | awk [red]'[/red]{if (substr($0,1,2)=="sh") print}'');

note that with the 2nd [red]'[/red] you are closing the command!

Cheers.

Chacal, Inc.[wavey]
 
Hi, yeah i realised this but i'm unsure on how to correct it? I've tried every combination of brackets but cant get it to work.

Thanks
00229257
 
try with \'

$output = shell_exec('ypcat passwd | awk [red]\'[/red]{if (substr($0,1,2)=="sh") print}[red]\'[/red]');

Chacal, Inc.[wavey]
 
Thanks for that it works a treat!!

And here's the but....

How can i pass a php variable with this command? I tried;

$passwdline = shell_exec('ypcat passwd | awk \'{if (substr($0,1,2)=="$username") print}\'');

and

$passwdline = shell_exec('ypcat passwd | awk \'{if (substr($0,1,2)==$username) print}\'');

but no luck?

Thanks for your help
00229257
 
hmmm...

$passwdline = shell_exec('ypcat passwd | awk \'{if (substr($0,1,2)==[red]' . $username . '[/red]) print}\'');

try it.

Cheers.

Chacal, Inc.[wavey]
 
I guess this is best.. closing between " " the username:

$passwdline = shell_exec('ypcat passwd | awk \'{if (substr($0,1,2)==[red]"' . $username . '"[/red]) print}\'');

Cheers.

Chacal, Inc.[wavey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top