• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Control $random output for motd?

Joined
Aug 26, 2013
Messages
148 (0.04/day)
Location
NL
System Name Helios
Processor AMD Ryzen 5 2600 @ 4.0
Motherboard MSI X470 Gaming Pro Carbon
Cooling Corsair iCUE H150i RGB Pro XT
Memory 16GB Corsair Vengeance Pro RGB DDR4-3200
Video Card(s) Gigabyte Geforce GTX 1080 G1 Gaming
Storage 256GB Hynix Pc401 & 3TB of HDD storage
Display(s) AOC CU34G2X + 2x Iiyama E2483HS
Case In-Win 303
Audio Device(s) Teufel Concept C 200 USB & Schiit Fulla 2 w/ Beyerdynamic DT 990 Pro 250Ohm
Power Supply Corsair RM1000
Mouse Logitech G502 Proteus Spectrum
Keyboard Logitech G512
Software Windows 10
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?
 
Joined
Oct 24, 2004
Messages
1,294 (0.18/day)
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
 
Joined
Aug 26, 2013
Messages
148 (0.04/day)
Location
NL
System Name Helios
Processor AMD Ryzen 5 2600 @ 4.0
Motherboard MSI X470 Gaming Pro Carbon
Cooling Corsair iCUE H150i RGB Pro XT
Memory 16GB Corsair Vengeance Pro RGB DDR4-3200
Video Card(s) Gigabyte Geforce GTX 1080 G1 Gaming
Storage 256GB Hynix Pc401 & 3TB of HDD storage
Display(s) AOC CU34G2X + 2x Iiyama E2483HS
Case In-Win 303
Audio Device(s) Teufel Concept C 200 USB & Schiit Fulla 2 w/ Beyerdynamic DT 990 Pro 250Ohm
Power Supply Corsair RM1000
Mouse Logitech G502 Proteus Spectrum
Keyboard Logitech G512
Software Windows 10
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
How do i go about integrating this onto my sed command? sorry, total noob at this.
 
Joined
Oct 24, 2004
Messages
1,294 (0.18/day)
You could try to substitute your $RANDOM variable with the one provided before :

Code:
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.
 
Joined
Aug 26, 2013
Messages
148 (0.04/day)
Location
NL
System Name Helios
Processor AMD Ryzen 5 2600 @ 4.0
Motherboard MSI X470 Gaming Pro Carbon
Cooling Corsair iCUE H150i RGB Pro XT
Memory 16GB Corsair Vengeance Pro RGB DDR4-3200
Video Card(s) Gigabyte Geforce GTX 1080 G1 Gaming
Storage 256GB Hynix Pc401 & 3TB of HDD storage
Display(s) AOC CU34G2X + 2x Iiyama E2483HS
Case In-Win 303
Audio Device(s) Teufel Concept C 200 USB & Schiit Fulla 2 w/ Beyerdynamic DT 990 Pro 250Ohm
Power Supply Corsair RM1000
Mouse Logitech G502 Proteus Spectrum
Keyboard Logitech G512
Software Windows 10
You could try to substitute your $RANDOM variable with the one provided before :

Code:
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.
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.
 
Joined
Oct 24, 2004
Messages
1,294 (0.18/day)
Please disregard my previous post, try this one instead :

Code:
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.
 
Joined
Aug 26, 2013
Messages
148 (0.04/day)
Location
NL
System Name Helios
Processor AMD Ryzen 5 2600 @ 4.0
Motherboard MSI X470 Gaming Pro Carbon
Cooling Corsair iCUE H150i RGB Pro XT
Memory 16GB Corsair Vengeance Pro RGB DDR4-3200
Video Card(s) Gigabyte Geforce GTX 1080 G1 Gaming
Storage 256GB Hynix Pc401 & 3TB of HDD storage
Display(s) AOC CU34G2X + 2x Iiyama E2483HS
Case In-Win 303
Audio Device(s) Teufel Concept C 200 USB & Schiit Fulla 2 w/ Beyerdynamic DT 990 Pro 250Ohm
Power Supply Corsair RM1000
Mouse Logitech G502 Proteus Spectrum
Keyboard Logitech G512
Software Windows 10
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.
 
Joined
Aug 26, 2013
Messages
148 (0.04/day)
Location
NL
System Name Helios
Processor AMD Ryzen 5 2600 @ 4.0
Motherboard MSI X470 Gaming Pro Carbon
Cooling Corsair iCUE H150i RGB Pro XT
Memory 16GB Corsair Vengeance Pro RGB DDR4-3200
Video Card(s) Gigabyte Geforce GTX 1080 G1 Gaming
Storage 256GB Hynix Pc401 & 3TB of HDD storage
Display(s) AOC CU34G2X + 2x Iiyama E2483HS
Case In-Win 303
Audio Device(s) Teufel Concept C 200 USB & Schiit Fulla 2 w/ Beyerdynamic DT 990 Pro 250Ohm
Power Supply Corsair RM1000
Mouse Logitech G502 Proteus Spectrum
Keyboard Logitech G512
Software Windows 10
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.
 
Joined
Aug 26, 2013
Messages
148 (0.04/day)
Location
NL
System Name Helios
Processor AMD Ryzen 5 2600 @ 4.0
Motherboard MSI X470 Gaming Pro Carbon
Cooling Corsair iCUE H150i RGB Pro XT
Memory 16GB Corsair Vengeance Pro RGB DDR4-3200
Video Card(s) Gigabyte Geforce GTX 1080 G1 Gaming
Storage 256GB Hynix Pc401 & 3TB of HDD storage
Display(s) AOC CU34G2X + 2x Iiyama E2483HS
Case In-Win 303
Audio Device(s) Teufel Concept C 200 USB & Schiit Fulla 2 w/ Beyerdynamic DT 990 Pro 250Ohm
Power Supply Corsair RM1000
Mouse Logitech G502 Proteus Spectrum
Keyboard Logitech G512
Software Windows 10
..
...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.
 
Joined
Oct 24, 2004
Messages
1,294 (0.18/day)
Hopefully you'll get it working the way you want :)



Feel free to share your ascii art here, i like that too.
 
Joined
Aug 26, 2013
Messages
148 (0.04/day)
Location
NL
System Name Helios
Processor AMD Ryzen 5 2600 @ 4.0
Motherboard MSI X470 Gaming Pro Carbon
Cooling Corsair iCUE H150i RGB Pro XT
Memory 16GB Corsair Vengeance Pro RGB DDR4-3200
Video Card(s) Gigabyte Geforce GTX 1080 G1 Gaming
Storage 256GB Hynix Pc401 & 3TB of HDD storage
Display(s) AOC CU34G2X + 2x Iiyama E2483HS
Case In-Win 303
Audio Device(s) Teufel Concept C 200 USB & Schiit Fulla 2 w/ Beyerdynamic DT 990 Pro 250Ohm
Power Supply Corsair RM1000
Mouse Logitech G502 Proteus Spectrum
Keyboard Logitech G512
Software Windows 10
Hopefully you'll get it working the way you want :)



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


It looks like this so far. Pretty happy except for the quotes bit :p
 
Joined
Aug 26, 2013
Messages
148 (0.04/day)
Location
NL
System Name Helios
Processor AMD Ryzen 5 2600 @ 4.0
Motherboard MSI X470 Gaming Pro Carbon
Cooling Corsair iCUE H150i RGB Pro XT
Memory 16GB Corsair Vengeance Pro RGB DDR4-3200
Video Card(s) Gigabyte Geforce GTX 1080 G1 Gaming
Storage 256GB Hynix Pc401 & 3TB of HDD storage
Display(s) AOC CU34G2X + 2x Iiyama E2483HS
Case In-Win 303
Audio Device(s) Teufel Concept C 200 USB & Schiit Fulla 2 w/ Beyerdynamic DT 990 Pro 250Ohm
Power Supply Corsair RM1000
Mouse Logitech G502 Proteus Spectrum
Keyboard Logitech G512
Software Windows 10
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 ^^
 
Top