LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Please translate this PHP script to BASH (http://www.linuxquestions.org/questions/programming-9/please-translate-this-php-script-to-bash-634297/)

protorox 04-09-2008 09:20 PM

Please translate this PHP script to BASH
 
PHP Code:

<?php
$ss 
file('/sbin/ss.list');
$ssNum count($ss);
$thisSSrand rand(1,$ssNum);
$thisSS rtrim($ss[$thisSSrand]);
print 
$thisSS;
?>


ghostdog74 04-09-2008 11:04 PM

you can use wc to count lines in a file. check man wc
you can use $RANDOM to generate random number. see here too

sag47 03-27-2014 06:35 PM

I realize this is an old post but I thought I would do this as a simple bash exercise since ghostdog74 was kind enough to share the $RANDOM variable. This randomly pulls out a line from a file using bash.

Code:

#!/bin/bash
#Sam Gleske
#Thu Mar 27 19:33:14 EDT 2014
#Ubuntu 12.04.4 LTS \n \l
#Linux 3.8.0-37-generic x86_64
#GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)

filename="/sbin/ss.list"
linecount="$(wc -l "${filename}" | cut -d' ' -f1)"
number="${RANDOM}"
let "number %= ${linecount}"
#this echo line will prepend the line number to the line being echoed.
#echo -n "${number} "
head -n ${number} "${filename}" | tail -n1


grail 03-27-2014 08:08 PM

I think I would change the calculation to a single line and use sed:
Code:

(( number = linecount % RANDOM ))

sed -n "$number p" "$filename"


NevemTeve 03-28-2014 01:05 AM

It would be more flexible if you shouldn't have to know number of lines beforehand. (The program would work on pipes/sockets.)
Hint: read the input line-by-line, and after the nth line decide (1:n chance) whether keep the actual line as 'selected so far' or not. In the end, print the selected line.

sag47 03-28-2014 08:21 AM

Quote:

Originally Posted by NevemTeve (Post 5142662)
It would be more flexible if you shouldn't have to know number of lines beforehand. (The program would work on pipes/sockets.)
Hint: read the input line-by-line, and after the nth line decide (1:n chance) whether keep the actual line as 'selected so far' or not. In the end, print the selected line.

I'm not sure that's possible in this case. Because the goal is to retrieve a random line from within the file. How would one calculate the random line to pull without actually going over the number of lines in the file? If you were to simply use pipes/sockets and stream the file while attempting to randomly pull a line from it there's always a chance of mostly pulling the last line or no line at all because the random number exceeds the number of lines in the file.

grail wins over my attempt :). Good job.

sundialsvcs 03-28-2014 08:47 AM

I still like this post, because I see this sort of thing a lot ... especially from folks who cut their teeth in the Windows environment. They're not used to "the embarrassment of riches" that Unix/Linux enjoys in terms of readily available programming-tools (and that you don't have to pay for). Many of them literally do not know that you can write "a command-line script" in PHP, in Perl, in Python or in any other language ... that, thanks to #shebang, you can write it in anything. That you could literally run that PHP-script in the command line if you were so inclined, with no need to rewrite it and with the full power of PHP at hand.

BASH-scripting was never really intended to do what it's pressed into doing. (The only shell which seriously tried to build-in a programming language was the Korn shell.) You've got a plethora of true programming tools at your beck and call. But many programmers don't realize that.

szboardstretcher 03-28-2014 08:52 AM

You want a random line from a file?

Code:

shuf -n1 filename.txt
I've said it before,.. Code it? it probably already exists as a utility.

grail 03-28-2014 12:09 PM

Quote:

Originally Posted by szboardstretcher (Post 5142839)
You want a random line from a file?

Code:

shuf -n1 filename.txt
I've said it before,.. Code it? it probably already exists as a utility.

True enough ... I only looked at the recoding ;)


All times are GMT -5. The time now is 06:01 AM.