the_oliver Posted January 2, 2007 Share Posted January 2, 2007 Hello,I have a scrip which i want to add a line onto the bottom of the text file. If i am in terminal i can just type: [code]printf "bla bla" >> /etc/filename.whatever[/code]If i then do this:[code]$output = shell_exec('printf "bla bla" >> /etc/filename.whatever');[/code]It works like a dream! However if i try to involve variable it all goes wrong! The code below for example places nothing into the file.[code]$fun = "me";$funky = "'printf \"".$fun."\" >> /BlueEmu/test.txt'";$output = shell_exec('$funky');neither does:$output = shell_exec('printf \"".$fun."\" >> /BlueEmu/test.txt');[/code]Can someone tell me where im going wrong? - Thanks Quote Link to comment https://forums.phpfreaks.com/topic/32576-solved-inserting-variables/ Share on other sites More sharing options...
kenrbnsn Posted January 2, 2007 Share Posted January 2, 2007 Why don't you use PHP to add the line?[code]<?php$fp = fopen('/BlueEmu/test.txt','a'); // open the file for appendfwrite($fp,$fun."\n");fclose($fp);?>[/code]This assumes that the process that the webserver runs under has permission to write the the file.Ken Quote Link to comment https://forums.phpfreaks.com/topic/32576-solved-inserting-variables/#findComment-151486 Share on other sites More sharing options...
wildteen88 Posted January 2, 2007 Share Posted January 2, 2007 Here you are doing it wrong:$output = shell_exec('$funky');Variables are treated as text if they are in single quotes. Remove the quotes. Also echo out the funky variable to see what is being passed to the shell_exec function. Quote Link to comment https://forums.phpfreaks.com/topic/32576-solved-inserting-variables/#findComment-151488 Share on other sites More sharing options...
ober Posted January 2, 2007 Share Posted January 2, 2007 Surely that is one way to do it, but you might have better luck using fopen(), fwrite(), etc to do file manipulation rather than calls to the shell.Besides, printf only takes variables as arguments, not directly in the string. You'd have to do something like:printf("blah blah %s", $fun) (or however you reference variables within printf... it's been a while since I used it). Quote Link to comment https://forums.phpfreaks.com/topic/32576-solved-inserting-variables/#findComment-151489 Share on other sites More sharing options...
the_oliver Posted January 2, 2007 Author Share Posted January 2, 2007 It was the ' that was causing the problem. Genius's! Quote Link to comment https://forums.phpfreaks.com/topic/32576-solved-inserting-variables/#findComment-151497 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.