Jump to content

[SOLVED] inserting variables


the_oliver

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
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).

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.