LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 04-09-2008, 09:20 PM   #1
protorox
LQ Newbie
 
Registered: Apr 2008
Posts: 8

Rep: Reputation: 0
Question 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;
?>
 
Old 04-09-2008, 11:04 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you can use wc to count lines in a file. check man wc
you can use $RANDOM to generate random number. see here too
 
Old 03-27-2014, 06:35 PM   #3
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
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
 
Old 03-27-2014, 08:08 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
Old 03-28-2014, 01:05 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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.
 
Old 03-28-2014, 08:21 AM   #6
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Quote:
Originally Posted by NevemTeve View Post
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.
 
Old 03-28-2014, 08:47 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
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.
Old 03-28-2014, 08:52 AM   #8
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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 08:54 AM.
 
1 members found this post helpful.
Old 03-28-2014, 12:09 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Originally Posted by szboardstretcher View Post
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Execute bash script inside PHP creatorrr Programming 11 11-21-2007 04:32 PM
run shell script bash from browser with php achilles Programming 10 08-02-2007 03:44 PM
Using PHP, run bash script and see results on Server's Monitor xmrkite Linux - Software 6 10-17-2006 01:28 PM
php and bash script permissions paddyjoy Programming 2 10-04-2006 06:01 PM
Cannot create folders with bash script called from php keyF Linux - Software 4 06-25-2006 10:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration