Jump to content

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/32576-solved-inserting-variables/
Share on other sites

Why don't you use PHP to add the line?
[code]<?php
$fp = fopen('/BlueEmu/test.txt','a'); // open the file for append
fwrite($fp,$fun."\n");
fclose($fp);
?>[/code]

This assumes that the process that the webserver runs under has permission to write the the file.

Ken
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.
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).
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.