PDA

View Full Version : Random Question


soup4you2
August 5th, 2003, 00:25
Ok here's 2 questions for ya..

1. how can i run a command in a script and take it's output and assign it to a variable within the script.

2. how can i assign a random file in a directory a variable?

Thanks in advance

frisco
August 5th, 2003, 02:22
Ok here's 2 questions for ya..

1. how can i run a command in a script and take it's output and assign it to a variable within the script.


Use backticks:

#!/bin/sh
OSTYPE=`uname`
echo $OSTYPE



2. how can i assign a random file in a directory a variable?


I don't understand this. Can you give me an example of what you are wanting to do?

soup4you2
August 5th, 2003, 10:01
i was sitting back last night drunk trying to think of something to entertain myself..

so i thought i would write a quick script to run the cowsay port and have it say the output of the sex port, while loading a random cow every time.. then assign it a cron job to update the motd with the output every hour..

and yes i have this much free time

so right now i have

thoughts=`sex`
echo $thoughts
cowsay -s $thoughts

with gives the cow the output of sex... now i need to have it pick out a random file in the /usr/local/share/cows dir.

[code:1:cbaf816635]
_________________________________________
/ "Cheese it, the cops!" squealed the \
| friendly construction worker as the |
| handsome ex-celibate fondled her |
| jiggling knockers and inserted his |
| searing middle leg into her scarred jam |
\ jar. /
-----------------------------------------
\ ^__^
\ (**)\_______
(__)\ )\/\
U ||----w |
|| ||
[/code:1:cbaf816635]

frisco
August 5th, 2003, 11:20
so i thought i would write a quick script to run the cowsay port and have it say the output of the sex port, while loading a random cow every time.. then assign it a cron job to update the motd with the output every hour..

Never used the cowsay or sex ports before and only cowsay has a port on OpenBSD. Assuming that running 'sex' outputs something witty, then this should work.

[code:1:2c09249142]
#!/bin/sh

num=`ls /usr/local/share/cows | wc -l |
awk '{srand(); print int(rand() * $1) + 1 } ' `
file=`ls /usr/local/share/cows | head -$num | tail -1`

sex | cowsay -f $file
[/code:1:2c09249142]

I use something similar to cycle through my movies and keep something random playing all the time.

soup4you2
August 5th, 2003, 12:25
it keeps returning a value of 1

so it's staying on the first cowfile in the directory..

frisco
August 5th, 2003, 12:33
Which part?

if you run
awk '{srand(); print int(rand() * 100) + 1 } '
a few times, does that return diff values?

if you run
num=`ls /usr/local/share/cows | wc-l`
echo $num
does that return > 1?

soup4you2
August 5th, 2003, 12:39
your help got me on the right track.. i got it working..

The Sex Cow
[code:1:d16c7d3e54]
function getcow {
max=$(ls /usr/local/share/cows $1 2>/dev/null | wc -l)
n=$(echo "$RANDOM % $max + 1" | bc 2>/dev/null)
ls /usr/local/share/cows $1 | head -n $n 2>/dev/null | tail -n 1
}

sex | cowthink -f $(getcow)
[/code:1:d16c7d3e54]


if you run
num=`ls /usr/local/share/cows | wc-l`
echo $num
does that return > 1?


No it counts the files it returns 50 it's the awk line that keeps returning 1

frisco
August 5th, 2003, 13:33
Well shucks, when did $RANDOM get thrown into every shell out there? Thanks for pointing that out.

soup4you2
August 5th, 2003, 15:12
Well shucks, when did $RANDOM get thrown into every shell out there? Thanks for pointing that out.

never even knew it existed... a little birdie told me about it today...

well if anybody gets bored they can visit

http://bsdhound.com/cow.php

Vile
October 4th, 2003, 05:34
I went there and this is the web page it generated...

[code:1:247186f749]<code><pre>
</pre></code>
<p><a href="cow.php">Refresh</a></p>[/code:1:247186f749]

not very interesting... ?

v902
October 4th, 2003, 12:34
That's because this topic is exactly a month old ;) Might want ot check your dates when you post ;)

elmore
October 4th, 2003, 12:55
Still the page is there, but there's just now cow showing up. :?:

Give us the cow soup! :!:

soup4you2
October 4th, 2003, 14:20
I went there and this is the web page it generated...

[code:1:160372bcce]<code><pre>
</pre></code>
<p><a href="cow.php">Refresh</a></p>[/code:1:160372bcce]

not very interesting... ?

ahh thats because i no longer have cowsay or sex installed at the present moment