Understudy.net/org

Ok you have made it this far.
You obviously must be bored and
have nothing better to do with your life.

[Previous entry: "Welcome to the backward technological age. ASUS can kiss my arse!"] [Main Index] [Next entry: "How to make millions. Make things convenient."]

11/04/2004 Archived Entry: "Palm Beach County Linux Users Group"

As per the request of the Palm Beach County Linux Users Group, I have placed the information on the bash scripting I used in the meeting last night here.

The subject was Bash Shell scripting. I did a small example with a heavily commented script. Here is the script:

#! /usr/local/bin/bash
# the she-bang for bash. Make sure you know where bash is located
# This is a simple stupid script to create and edit a file.
# This is not a great shell script. It is for training purposes only.
# written by Brendhan Horne with help from people who actually know
# how to script. Thanks to #linuxhelp (irc.oftc.net).

# start script with "read," a bash builtin ( a command built into bash ) that
# reads input. The -p switch places the items in quotes on the screen in a
# fashion similar to echo(see below).
# filename accepts the input from you and creates a varible you use as the new
# filename. So filename is a variable.
read -p "What do you want to name your file?" filename

# echo puts out to the screen what you typed in for a filename
# the $ in front of filename is called a parameter expression and with the $
# it will accept the variable that comes after it.
echo $filename

# This is the same as the first read command. This time however we are creating
# a variable called pathname. Variables can be named or identified in many
# ways.In this case it if for simple description of what the variable actually
# is. The $PWD is a "point which directory" command that shows your current
# location. The brackets [] and greater than > are just used to seperate the $PWD
# from the question.
read -p "Where do want the file to be save? [$PWD] > " pathname

# This is an if statement. Example if this happens do this. Here if the user presses
# enter it will take the current directory and use it as pathname. It does it by
# using a true return. If the STRING ( a line of typed input ) is empty, then use $PWD
# as pathname variable. If STRING is empty = true. If STRING isn't empty = false.
# then just ignore. The "help test" at the command line will show examples. The -z
# STRING = true if string is empty. The [[]] are known as a test expression. The
# test in this cases is to see if the -z $pathname is true. The ; means continue to
# the next part.
if [[ -z $pathname ]]; then pathname=$PWD;

# They fi is if spelled backwards and closes the if statement. You open a statement
# your must close it.
fi

# Same as the first echo. This time using the second variable called pathname
echo $pathname

# This uses the touch command to create the file. The varibles here are used
# to identify where the file will go and what it will be called. Note there
# is no seperation between the two variables. They each function seperatly
# because of the paramer expression $.
touch $pathname/$filename

# while executes command or function that follows it. Starts a loop and keeps
# it going as long as the return is true (aka 0).
# while is followed by a command or condition. We want the loop to keep going
# so we tell the loop it is true (:). True in this case is a command not a function. Thus the loop keeps going. The script
# continues with (;) which means after the first command or condition is meet
# go to the next. As the script continues while wants to be told to do
# something the "do" tells it to do the next thing.
while : ; do

# In the loop part of this script we continue with getting input from users.
# Just like in the beginning of this script. Here we again have a read
# (builtin) to place a statement on screen and accept input from the user and
# turn it into a variable (answer).
read -p "Would you like to edit the file?" answer

# case is a lazy workaround. Instead of a bunch of "if," "else," and
# "then" builtins I used case. The variable answer follows case. "in"
# follows the variable for case.
case "$answer" in

# The Y|y|yes means accept any of those keyboard inputs as a equal to yes.
# This is known as a constant. You must close a constant with ")" .
Y|y|yes)

# This part come into play if you said yes to editing the file. The $ parameter
# expression calls the enviromental setting and asks for your editor. If you
# don't have editor set it will choose vi. It then grabs the variables from
# earlier in the script and directs the editor to the file that touch created.
${EDITOR:-vi} "$pathname/$filename"

# Stop processing cases here is what break does. This is placed here because
# the if it wasn't it would go to the next statement whether it matched or
# not. If it matches it breaks, if it doesn't it continues. This break happens
# because the editor has been called. Break mean exit loop now in this case.
break

# The ;; means the end of this section and go to the next pattern (constant).
;;

# The N|n|no means accept any of those keyboard inputs as a equal to no.
# This is just like the yes constant shown earlier.
N|n|no)

# This exit is pretty self explanatory. If the user answers no then close the
# loop and exit the script.
exit

# The ;; means the end of this section and go to the next pattern (constant).
;;

# If something other than any of the accepted keyboard inputs happens do this.
# * is the wildcard or meta-charecter which means any keyboard input other than
# what is accepted as yes or no. If keyboard input is something other than yes
# or no output to screen (echo) "you entered the wrong answer."
*) echo you entered the wrong answer

# The ;; means the end of this section and go to the next pattern.
;;

# This is case spelled backwards. That ends the case function and move it to
# the next function.
esac

# This is the next function (done). It doesn't kill the script. It does
# represent the end of it. However the while loop returns true because of (:)
# and the loop continues. So the way out of the script is to answer yes or no.
done

Take the script and save it as a file with a name you like ( we will call our example "mfile").

Then chmod the file to make it executable by typing:
chmod +x mfile

You will need to either place this in a accepted PATH (type echo $PATH at the coomand line) or you must type ./ infront of the file name.
./mfile

This should cause the file to execute. You should see:
What do you want to name your file?

The comments in the script are there for educational purposes.

I recommend reading the following man pages:
man builtins
man touch
man chmod

Editors you can work with:
nano
pico
ee
vi
vim
emacs
Scite (X only)
nedit (X only)
and plenty of others.

The websites I recommended were:
http://tldp.org/LDP/abs/html/
http://linuxcommand.org/writing_shell_scripts.php#contents
If you have questions about the class go ahead and ask them here.

Powered By Greymatter