Solved

Pre-populate STDIN

Posted on 2011-02-26
4
396 Views
Last Modified: 2012-05-11
Hi! Please consider following:

#!/usr/bin/php
<?php
fwrite(STDOUT, "Please enter something:\n");
$answer = trim(fgets(STDIN));
echo "$answer\n";
?>

Open in new window


Is it possible to pre-populate a default value here in STDIN?
0
Comment
Question by:Julian Matz
4 Comments
 
LVL 77

Assisted Solution

by:arnold
arnold earned 100 total points
ID: 34989759
echo "data for STDIN" | your_php_script
your_php_script < "filename"
The data from the filename will be available on STDIN.
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 300 total points
ID: 34992012
Maybe something like this would make sense for your app.
#!/usr/bin/php
<?php
fwrite(STDOUT, "Please enter something:\n");
$answer = trim(fgets(STDIN));

if (empty($answer)) $answer = 'PREPOPULATED ANSWER';

echo "$answer\n";
?>

Open in new window

0
 
LVL 1

Assisted Solution

by:BasementCat
BasementCat earned 100 total points
ID: 34992292
You can't "pre-populate" STDIN since it's a file handle, but what Ray suggested will work.  The only way to "pre-populate" stdin would be to pipe some input to the program ahead of time, like
echo "hello, world" |yourprogram.php

Open in new window

0
 
LVL 21

Author Comment

by:Julian Matz
ID: 34992794
Thanks, I thought of the if (empty($answer)) solution afterwards. It will work for me. I just have the default answer in brackets after the question.

Thanks for your help.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question