Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
04-09-2008, 10:20 PM
|
#1
|
LQ Newbie
Registered: Apr 2008
Posts: 8
Rep:
|
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;
?>
|
|
|
04-10-2008, 12:04 AM
|
#2
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
you can use wc to count lines in a file. check man wc
you can use $RANDOM to generate random number. see here too
|
|
|
03-27-2014, 07:35 PM
|
#3
|
Senior Member
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
|
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
|
|
|
03-27-2014, 09:08 PM
|
#4
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028
|
I think I would change the calculation to a single line and use sed:
Code:
(( number = linecount % RANDOM ))
sed -n "$number p" "$filename"
|
|
1 members found this post helpful.
|
03-28-2014, 02:05 AM
|
#5
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,950
|
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.
|
|
|
03-28-2014, 09:21 AM
|
#6
|
Senior Member
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
|
Quote:
Originally Posted by NevemTeve
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.
|
|
|
03-28-2014, 09:47 AM
|
#7
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,970
|
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.
|
|
1 members found this post helpful.
|
03-28-2014, 09:52 AM
|
#8
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
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.
Last edited by szboardstretcher; 03-28-2014 at 09:54 AM.
|
|
1 members found this post helpful.
|
03-28-2014, 01:09 PM
|
#9
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028
|
Quote:
Originally Posted by szboardstretcher
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 07:45 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|