Guys please excuse me for my ignorance, but I see a lot of comments on bash and scripting. Can you in rough explain to me what is it meant by scripting? What is it that you do? I never used terminal for more than just copy and rm and pasting in already written code from Ubuntu forums
But this sounds as I've been doing it wrong
As mentioned, bash scripts are just bash commands with some added stuff for flow control and looping and such to make it more of an actual scripting language.
Here's a bash script I've written which runs when I log in to bash (i.e. I add it to ~/.bashrc) that prints out a cowsay or ponysay with random cows/ponies using fortune as the text. I'll try to explain what it does to give you an idea what you can do with bash scripting:
Code:
#!/bin/bash
if [ -f /usr/bin/fortune ] && [ -f /usr/bin/cowsay ] && [ -f /usr/bin/ponysay ]; then
out=$(shuf -n 1 -e cowsay cowthink ponysay ponythink)
case ${out} in
cowsay | cowthink )
fortune -a | fmt -76 -s | ${out} -$(shuf -n 1 -e b d g p s t w y) -f $(shuf -n 1 -e $( ${out} -l | tail -n +2) ) -n
;;
ponysay | ponythink )
fortune -a | fmt -76 -s | ${out} -b $(shuf -n 1 -e $( ${out} -B ) )
;;
esac
fi
It's pretty simple really, but it needs some explanation. It first checks if /usr/bin/fortune, /usr/bin/cowsay and /usr/bin/ponysay exists. If they all do, it continues with the indentated code between the "if" and "fi" statements. It saves either "cowsay", "cowthink", "ponysay or "ponythink" into the variable "out" using "shuf" to select one of the four randomly. Then it checks what the variable contains with the "case ${out} in" statement, and if it's cowsay or cowthink it runs this command, where "${out}" is either cowsay or cowthink:
Code:
fortune -a | fmt -76 -s | ${out} -$(shuf -n 1 -e b d g p s t w y) -f $(shuf -n 1 -e $( ${out} -l | tail -n +2) ) -n
If "${out}" instead contains "ponysay" or "ponythink", it runs this command:
Code:
fortune -a | fmt -76 -s | ${out} -b $(shuf -n 1 -e $( ${out} -B ) )
These two commands are actually several commands "piped" together using the "|" character. What that does is that it takes the output of one command and feeds it into the input of the next command. So first it runs "fortune -a", which prints out a random quote that is then fed into "fmt -76 -s" to format it to 76 columns. Then it is fed to cowsay/cowthink/ponysay/ponythink which prints it out. This last command has a bunch of "shuf" commands choose randomly between several options to get a random pony/cow every time you run it. This one in particular is interesting:
Code:
cowsay -f $(shuf -n 1 -e $( cowsay -l | tail -n +2) )
"cowsay -f" lets you type in which cow style you want to use. And cowsay -l prints out all the available cow styles. So what I've done here is giving cowsay -f the output of cowsay -l using shuf to select only one of the options randomly (the "tail" command tells it to begin from the second row of the output). That's what happens if you use the $() characters, it runs the command inside the parenthesis and returns the output of the command, rather then the command itself.
Sorry if this is a little confusing. I'm not good at explaining things when I'm tired.
This is just one example of what can be done with bash scripting. If you want to learn more about shell scripting I recommend this tutorial:
http://linuxcommand.org/lc3_writing_shell_scripts.php