the ${} helps to single it out
so you can do firstPartOfString${somestring}theRestOfTheString
without it misreading what the variable $somestring is.
just good practice to always do it
as far as the ;
that is needed only if you are entering more than one line on one line
hence
Code:
if [ $bob ]; then do something; fi
is the same as
Code:
if [ $bob ]
then
do something
fi
in your example
gregg451 wrote:
if [ tomorrow ]
then
do-something
fi
the largest problem with that is the lack of the $ before the name of the variable "tomorrow"
in bash programming...the only time you don't use the $ before the variable name is when you are assigning the variable
the above example will always come out true...
Code:
<- jb:/home/jbsnake/scripts -> if [ tomorrow ]; then echo true; fi
true
<- jb:/home/jbsnake/scripts ->
ok...you want a good example (hopefully you read my tutorials first

)
write this in a shell script
Code:
#!/bin/bash
bob="thisText"
bill=""
if [ bill ]
then
echo true
else
echo false
fi
if [ $bill ]
then
echo true
else
echo false
fi
if [ bob ]
then
echo true
else
echo false
fi
if [ $bob ]
then
echo true
else
echo false
fi
you should get the lines written back
bash wrote:
<- jb:/home/jbsnake/scripts -> ./testIf
true
false
true
true
<- jb:/home/jbsnake/scripts ->
i didn't use curly brackets so as not to confuse you...
curly brackets are really to contain the variable
since you seem to be familiar with C...you would know that trimming a word is tedious using that language...you basically have to step through the entire string array in order to accomplish what you want
however...bash makes that soooo easy
take a variable called greet
let's assign it to contain the string "hi there"
Code:
greet="hi there"
so if we were to do:
Code:
echo "$greet"
then we get:
hi there
spit back out
but let's say we wanted to personalize our greeting
we would want to say hi but not there
we would want to say something like "hi bob"
but why create a new variable?
why not just say:
Code:
echo "${greet:0:3}bob"
that is taking the same variable and starting at the first position (which is 0) and having a length of 3
so contained within the variable ${greet:0:3} is "hi "
the above would echo back "hi bob"
do you see how the containment works by using the curly brackets?
and the way to get a complete length of our variable is ${#greet}
it seems i have steered off the point a little (but the information i hope if valuable all the same

)
the if statement is so "hard" in bash because of it's versatility
i know you probably think i'm a little coocoo because of how strict the spacing is along with the syntax, but it is extremely versatile
in some cases you don't even need the [] at all
let's say we wanted to test to see if a directory existed
Code:
if test -d /home/jbsnake/scripts
then
echo "it exists"
else
echo "doesn't exist"
fi
that would ofcourse echo "it exists" (can't live without it

)
the brackets actually are there to help simplify the if statement
instead of having to type the word test
you can just type
Code:
if [ -d /home/jbsnake/scripts ]
then
echo "it exists"
else
echo "it doesn't exist"
fi
test was made so you can make it known that you are actually testing something
if you were to want to drop the [], you could
because test can handle it all for you without having to worry too much about spacing
test can be used for testing anything
here's some clips from the man page about test
man test wrote:
-z STRING
the length of STRING is zero
STRING1 = STRING2
the strings are equal
STRING1 != STRING2
the strings are not equal
INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2
INTEGER1 -ge INTEGER2
INTEGER1 is greater than or equal to INTEGER2
INTEGER1 -gt INTEGER2
INTEGER1 is greater than INTEGER2
INTEGER1 -le INTEGER2
INTEGER1 is less than or equal to INTEGER2
INTEGER1 -lt INTEGER2
INTEGER1 is less than INTEGER2
INTEGER1 -ne INTEGER2
INTEGER1 is not equal to INTEGER2
-d FILE
FILE exists and is a directory
-e FILE
FILE exists
-f FILE
FILE exists and is a regular file
-g FILE
FILE exists and is set-group-ID
-h FILE
FILE exists and is a symbolic link (same as -L)
-r FILE
FILE exists and is readable
-w FILE
FILE exists and is writable
-x FILE
FILE exists and is executable
ok...i think that's enough for this post
hope this helps out a little
