Posted: . At: 9:10 AM. This was 4 years ago. Post ID: 14557
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


Very interesting shell scripting example.


This simple shell script will pick a random sentence from a text file. This is made up of multiple sentences from parts of the text file merged into one long sentence.

words.sh
1
2
3
4
5
#!/bin/sh
 
numwords=$(shuf -i 14-88 -n 1)
words=$(shuf -n $numwords 122-0.txt | cut -d$'' -f2)
echo -n $words ; echo

This is the result of the script. This is various sentences from the text file merged together.

jason@jason-desktop:~/Documents$ ./words.sh
John went to each computer, creating the necessary lisp code on each one for the installation of Gentoo, and then inserting the installation disks. After preparations were finished, he began the installation on every laptop. As the last laptop began, a massive gust of wind ripped through the chamber, blowing out all the torches. A faint red light appeared in the middle of the pentagram, suspended in the air. The entire underground complex began to quake under the brunt of the awesome power. Red bolts of electricity exploded from the center of the pentagram. The outline of a figure slowly appeared, hovering above the pentagram. John turned towards Matthew. "I thought you'd put up more of a fight than this, 'neckbeard lord'! This is pathetic!" John began to smile. John stepped into the chamber, and the door quickly slammed shut, trapping him inside. And just like that, John was gone. Off to be the true lord. The chamber was a small, spherical room. The walls were gray stone, with torches attached to them, lighting the room. In the middle of the room, there was a pentagram, with Thinkpads at each point of the pentagram. They are the only substance known to exist that can withstand the force of the summoning. In front of each one lay a jewel case, containing a Gentoo installation disk. Each computer had been primed for the summoning. "I don't know if I'll ever come back!" "Have strength, John," said Matthew. "You are the true neckbeard lord". "This is the master key, constructed from the bones of the master ricer lord from times long past. I am the head of the order, and only I can bear it." Matthew inserted the key into the door, and immediately, channels of multicolored light appeared, connecting to the other keyholes. The door unlocked and slowly opened. John's true Gentoo power had been released. Stallman!" Stallman prepared a final Bulldozer blast. He charged it fully and held it above John's head. John was interrupted by the sensation of a trunk door plowing into his massive ass cheeks, and finally coming to a close with a click. But, there was one burning, obvious fact that made it all so bittersweet.

Edit the script to look like this and the text will fold at the 80 character limit, this makes it more readable.

jason@jason-desktop:~/Documents$ cat words.sh 
#!/bin/sh
 
numwords=$(shuf -i 10-96 -n 1)
words=$(shuf -n $numwords stallman.txt | cut -d$'' -f2)
echo -n $words | fold -w 80 ; echo

This is what it will look like.

jason@jason-desktop:~/Documents$ ./words.sh
Fin. John's true Gentoo power had been released. "Anything you say will be enter
ed into the life console. Say it now." Stallman's body fractured into a thousand
 golden pieces, and with a flash of light, he was gone. The chamber was a small,
 spherical room. The walls were gray stone, with torches attached to them, light
ing the room. In the middle of the room, there was a pentagram, with thinkpads a
t each point of the pentagram. They are the only substance known to exist that c
an withstand the force of the summoning. In front of each one lay a jewel case, 
containing a Gentoo installation disk. Each computer had been primed for the sum
moning. He grabbed the wrist of the hand that Stallman was using to crush his ha
nd, tore him from his iron grip, and hurled him into a wall of the chamber. The 
force of the collision caused the ceiling of the previous chamber to fracture. T
he complex was beginning to collapse. "Wait a second, if those video cards were 
really back here, this entire state would have already become molten la-" "Only 
one. But, could you promise a dying man that it will be fufilled?" "Goodbye, Joh
n. Thank you for fulfilling my life." John stepped into the chamber, and the doo
r quickly slammed shut, trapping him inside. "Well then, if you believe that you
 are so entitled to my power, then prove it." John stepped over the massive hole
 left by the machine, and continued up the stairs, which lead into the kitchen. 
The slightly intense light of the overhead lamp was blinding. After 5 minutes, J
ohn's eyes adjusted to the light. The house had definitely changed since he had 
last entered it, but he couldn't remember how. After all, it had been ten years.
 Strange things sped past John as he walked. From what he could tell, they were 
mostly large computer cases, with powerful LEDs in the front, four wheels, and f
ive panel windows. "These must be the ricer lords," thought John, "better stay o
ut of their way." John looked down at himself as he walked. He had worn the same
 clothes for the past ten years; a sweat stained white t-shirt, cargo pants, and
 sandals with socks. He thought he looked pretty stylish. Matthew walked into th
e store, and about five minutes later, emerged with two men in black robes. They
 walked up to the back of the car, and carefully removed the laptop from the tru
nk, as to not upset John. The two men in robes began chanting over the laptop, a
s if to apologize to the gods for breaking the sacred act of compilation. Matthe
w held the power button on the computer until it deactivated. John instantly awo
ke, screaming.

This could be a very useful scripting tip.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.