# Control $random output for motd?



## ypoora1 (Oct 20, 2014)

So i'm making my MOTD, all fancy with ASCII art and colors and such. Now in one section, i want to print a quote that's randomly picked from a file and then printed in on every MOTD print. I'm already using a dynamic MOTD to show server info and uptime. Now the problem is i'm using "sed -n '$RANDOMp' /etc/motd-msg" to pick a random line and print it, but my file doesn't have well into 30K lines of quotes so more often than not it comes up blank. Is there any way to tie the output of $RANDOM to the number of lines in /etc/motd-msg?


----------



## blobster21 (Oct 20, 2014)

If you want to limit that to a certain maximum, you can just compare against the modulus of that maximum + 1. 
For example, the following will yield results between 0 and 9:

echo $(($RANDOM %10))
5
echo $(($RANDOM %10))
9

source


----------



## ypoora1 (Oct 20, 2014)

blobster21 said:


> If you want to limit that to a certain maximum, you can just compare against the modulus of that maximum + 1.
> For example, the following will yield results between 0 and 9:
> 
> echo $(($RANDOM %10))
> ...


How do i go about integrating this onto my sed command? sorry, total noob at this.


----------



## blobster21 (Oct 20, 2014)

You could try to substitute your $RANDOM variable with the one provided before :


```
sed -n '$(($RANDOM %number_of_lines))p' /etc/motd-msg
```

don't forget to set %number_of_lines according to the real number of lines in your quotes file.


----------



## ypoora1 (Oct 20, 2014)

blobster21 said:


> You could try to substitute your $RANDOM variable with the one provided before :
> 
> 
> ```
> ...


This does not work, i get "sed: -e expression #1, char 2: unknown command: `('". I guess i could always resort to writing a new quote in myself every day.


----------



## blobster21 (Oct 20, 2014)

Please disregard my previous post, try this one instead :


```
sed -n $((1+$RANDOM%`wc -l /etc/motd-msg | cut -f 1 -d ' '`))p /etc/motd-msg
```

I tested it successfully against a 70000+ word list and another tiny one, and got different words randomly choosen each time.


----------



## ypoora1 (Oct 20, 2014)

It prints  "wc -l /etc/motd-msg | cut -f 1 -d ' '". is there something wrong with my MOTD? I have #!/bin/bash echo -e "text `command` " sorting, using /usr/local/bin/dynmotd.


----------



## ypoora1 (Oct 20, 2014)

ypoora1 said:


> It prints  "wc -l /etc/motd-msg | cut -f 1 -d ' '". is there something wrong with my MOTD? I have #!/bin/bash echo -e "text `command` " sorting, using /usr/local/bin/dynmotd.


...nevermind. i need sleep.


----------



## ypoora1 (Oct 20, 2014)

..


ypoora1 said:


> ...nevermind. i need sleep.


.or i don't need sleep and actually putting the word count in makes it print the same with 10 instead of wc.


----------



## blobster21 (Oct 20, 2014)

Hopefully you'll get it working the way you want 







Feel free to share your ascii art here, i like that too.


----------



## ypoora1 (Oct 20, 2014)

blobster21 said:


> Hopefully you'll get it working the way you want
> 
> 
> 
> ...








It looks like this so far. Pretty happy except for the quotes bit


----------



## blobster21 (Oct 21, 2014)

How about a simple 


> sort -R /etc/motd-msg | tail -1



After all, you're not married with sed


----------



## ypoora1 (Oct 21, 2014)

blobster21 said:


> How about a simple
> 
> 
> After all, you're not married with sed


Worked a treat. How it decides according to the number of lines is beyond me, but now anyone with permission can dump quotes to motd-msg and they will display randomly. Thanks ^^


----------

