If you havn't read the beginner tutorial, I suggest you stop now and read it first. If you have read it, congratulations! You can officially write what I call a "dumb" script. I say it's a dumb scrip because it contains no logic. It simply goes from the top of the script to the bottom executing code as it goes.
Logic is pretty easy to implement once you know the key words. The most basic logical expression is if. Logical expressions help direct the flow of logic within a script. A good example of that is at the beginning of this tutorial. <b>If</b> you havn't read the beginner tutorial... In a shell script that would look something like this:

Code:

if [[ ! $readBeginningTutorial ]] then exit fi

An if statement's syntax is pretty easy, it <u><b>must</b></u> start with the word if, followed by a condition ($a = $b), on the next line it <u><b>must</b></u> have the word then. The next few lines are commands to execute if the above mentioned condition is met. The if statement is then closed using the word fi (if backwards). The brackets ([[ ]]) surrounding the condition are not always needed, it depends on the condition. I personally always use the brackets just to help me keep things uniform. That way I can easily spot al my conditions in my scripts. I think thats enough babbling about the if statement. Let's actually write some if statements so you can see what all the stuff I said above is actually doing. For out first example, let's do something easy. Don't forget the needed stuff at the beginning of your script!

Code:

#!/bin/bash

if [[ 1 == 2 ]] then echo "You will never see this while executing this script!"; fi

The above example checked to see if 1 was equal to 2. Since that condition can never be met, the echo command will never execute. Here's another example:

Code:

#!/bin/bash

if [[ 1 == 2 ]] then echo "This will never execute"; fi echo "This will execute no matter what the condition is" echo "because it is outside of the if statement."

I know what you are thinking. "That's great and all, but how can I check to see if it's either true or false?". Answer: elif. Elif is bash's version of Else If. A quick example of that:

Notice only the commands in the else portion execute. An else and/or an elif are skipped if the if condition is met. Example:

Code:

if [[ 1 == 1 ]] then echo "This is all that executes" echo "Well, besides this" echo "But that is because it's all in the" echo "Same block of code" elif [[ 2 == 2 ]] then echo "Even though is condition is met" echo "None of this will print because" echo "The if condition was already met" else echo "This is skipped" fi

I personally don't care much for elif. If I have more than true or false for a condition, I'll use a case statement. A case statement checks one variable for multiple possible conditions. Example:

After reading this tutorial this far, you're probably saying "So?". If you already knew the enclosed information, you have probably written a few programs in the past. I wrote this tutorial for someone with no background experience in programming in mind. For the programmers that read this, I would just pay attention to the syntax. Now let's put this tutorial to use. I think we should write a little menu driven script. We will use if-then-else and case. Along with some bonus material (a loop) thrown in for kicks. Open a terminal window and type:

Code:

cd echo "#!/bin/bash" > menuScript

Now that the first (not to mention most important) line of your script is written, open it in your favorite editor (fyi: the script is named "menuScript"). I usually use vi, but kate is my favorite GUI editor. Under the first line let's make some variables.

Code:

#!/bin/bash

### start variables ### ans="" choice="" ### end variables ###

After the variables, I suggest writing the functions we'll use.

Code:

### start functions ### function yesno { passedVar=$1

##################################################### use part of the variable passed. start with the 0 position and the# length is one (first letter).#################################################### ans=${passedVar:0:1}

Now you can start the main body of the script. Keep in mind you can obviously create more functions, thus writing less in your main body, not to mention cutting down on repeating alot of the same code. This is called modularization (this essence of object oriented coding). If you write your functions well, you can usually just copy and paste the same functions from one script to another (something I love to do). Or you can create a function library, I may get into that in another tutorial). Back on track, let's write our main body:

Ofcourse there is so very much more you could do with the example I gave you. That is up to you. Notice I didn't even use the function I wrote, that also is for you. See if you can't come up with a way to incorporate that function into this script, or another script you may write. Hope you have enjoyed reading this. Keep an eye out for the next installment of my bash tutorials (there only going to get harder )

if [[ 1 == 1 ]]then echo "This is all that executes" echo "Well, besides this" echo "But that is because it's all in the" echo "Same block of code"elif [[ 2 == 2 ]]then echo "Even though is condition is met" echo "None of this will print because" echo "The if condition was already met"else echo "This is skipped"fi

First observation applies there.

----

Code:

### start functions ###function yesno{ passedVar=$1

##################################################### use part of the variable passed. start with the 0 position and the# length is one (first letter).#################################################### ans=${passedVar:0:1}

That's actually wrong. You are checking if the character « 1 » is equivalent to the character « 2 ».You're not performing an algebraic comparison.

With the [[ ... ]] syntax, you would use:

Code:

if [[ 1 -eq 2 ]]; then

But, in bash(1), we use the (( ... )) syntax for arithmetic, so the correct code is:

Code:

if ((1 == 2)); then

Meh, I think that's NOT entirely correct..... EITHER way is correct.Personally I ALWAYS use the [[ when doing a TEST, as I'm scanning my code it's very quick to see what is a test, and what is actually calculations.And if i'm using an "if" then I'm probably "testing" something .... NOT actually doing math calculations per se. In bash, as in perl , there is more than one way to do things.

Why ?? Because I'm doing a "comparision TEST", not perfoming a "calculation" that I will assign to a variable for use later. So my question is why would I want to start using two different styles of a "test" ?? That's just plain confusing when it doesn't need to be.

2. Let's talk about this line: a. You're using backticks, i.e. ``, which is deprecated, and shouldn't be used in bash(1). We use $(...) instead. (it's POSIX compliant too) b. You should quote the expansion of ans, since you don't know what crap can be entered. (what if it's an asterisk?) c. You're piping echo(0) to tr(1), so a string to tr(1). It runs tr(1) in a subshell, and this behavior can be avoidedthanks to the <<< redirection.

As for #1 ... if you don't care if it's posix compliant (IE: your writing your script for a specific machine) ... it DOESNT MATTER. Some people like to be able to find their functions very quickly, and oddly enough, the word "function" makes it stand out pretty well. (I personally don't use it, but it's not like it breaks some cardinal rule if you do use it.)

#2 a. ------- personally, I STILL use backticks, it shows me quickly that it is command substitution and not a mathamatical expression..my own personal preference, but ALSO if you noted when this was written.......... 2005, that bash 3.0 was out, and the $(....) didn't work correctly until 3.2b. probably would be best yes.c. Again....... EITHER way works, jbsnakes way is much clearer to those NEW to bash, your way might be faster.......but lets face it, does that REALLY matter ? If it does, your probably using the wrong language for the job, as bash never laid any claims to being the fastest that I recall

Why ?? Because I'm doing a "comparision TEST", not perfoming a "calculation" that I will assign to a variable for use later.So my question is why would I want to start using two different styles of a "test" ?? That's just plain confusing when it doesn't need to be.

Indeed, you can assign variables directly within the (( ... )) syntax, but I just did a test there, not an assignment,and I don't think that [[ 1 -gt 2 ]] is clearer than ((1 > 2)).

But it's your choice not to use it.

----

crouse wrote:

a. ------- personally, I STILL use backticks, it shows me quickly that it is command substitution and not a mathamatical expression..my own personal preference,but ALSO if you noted when this was written.......... 2005, that bash 3.0 was out, and the $(....) didn't work correctly until 3.2

I didn't find any information about this in 3.2's changelog,but something about `` being fixed in 4.0, unfortunately.

c. Again....... EITHER way works, jbsnakes way is much clearer to those NEW to bash, your way might be faster.......but lets face it, does that REALLY matter ?If it does, your probably using the wrong language for the job, as bash never laid any claims to being the fastest that I recall

Don't take this so seriously, I just give advices.You follow them or not. After understanding why you should, of course.

I just know that now bash(1) beginners who are reading this tutorialknow about this useful redirection, and that's cool for them.

I just didn't wan't people to think that there is a set in stone way to do things, as there is not, and one persons view of how things should be done isn't always the "right" way.Sometimes it is just a matter of personal preference.

Who is online

Users browsing this forum: No registered users and 5 guests

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum