Archive for April, 2007

elifUnfortunately, there are several problems with this very (Domain and web hosting)

Monday, April 30th, 2007

elifUnfortunately, there are several problems with this very simple script. It will take any answer except yesas meaning no. We can prevent this by using the elifconstruct, which allows us to add a second condi- tion to be checked when the elseportion of the ifis executed. Try It Out Doing Further Checks with an elifWe can modify our previous script so that we report an error message if the user types in anything otherthan yesor no. We do this by replacing the elsewith elifand then adding another condition. #!/bin/shecho Is it morning? Please answer yes or no read timeofdayif [ $timeofday = yes ] thenecho Good morning elif [ $timeofday = no ]; thenecho Good afternoon elseecho Sorry, $timeofday not recognized. Enter yes or no exit 1fiexit 0How It WorksThis is quite similar to the previous example, but now the elifcommand tests the variable again if thefirst ifcondition is not true. If neither of the tests is successful, an error message is printed and thescript exits with the value 1, which the caller can use in a calling program to check if the script was suc- cessful. A Problem with VariablesThis fixes the most obvious defect, but a more subtle problem is lurking. Let s try this new script, butjust press Enter (or Return on some keyboards) rather than answering the question. We ll get this errormessage: [: =: unary operator expectedWhat went wrong? The problem is in the first ifclause. When the variable timeofdaywas tested, itconsisted of a blank string. So the ifclause looks likeif [ = yes ] which isn t a valid condition. To avoid this, we must use quotes around the variable: if [ $timeofday = yes ] 36Chapter
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision ecommerce web hosting services

Control StructuresThe shell (Web host server) has a set of control

Monday, April 30th, 2007

Control StructuresThe shell has a set of control structures, and once again they re very similar to other programming languages. ifThe ifstatement is very simple: It tests the result of a command and then conditionally executes agroup of statements: ifconditionthenstatementselsestatementsfiTry It Out Using the if CommandAcommon use for ifis to ask a question and then make a decision based on the answer: #!/bin/shecho Is it morning? Please answer yes or no read timeofdayif [ $timeofday = yes ]; thenecho Good morning elseecho Good afternoon fiexit 0This would give the following output: Is it morning? Please answer yes or noyesGood morning$ This script uses the [command to test the contents of the variable timeofday. The result is evaluated ifcommand, which then allows different lines of code to be executed. Notice that we use extra white space to indent the statements inside the if. This isjust a convenience for the human reader; the shell ignores the additional white space. In the following sections, the statements are the series of commands to performwhen, while, or until the condition is fulfilled.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision virtual web hosting services

Web hosting india - File ConditionalResult-d fileTrue if the file is a

Monday, April 30th, 2007

File ConditionalResult-d fileTrue if the file is a directory. -e fileTrue if the file exists. Note that, historically, the -e option has notbeen portable, so -f is usually used. -f fileTrue if the file is a regular file. -g fileTrue if set-group-id is set on file. -r fileTrue if the file is readable. -s fileTrue if the file has nonzero size. -u fileTrue if set-user-id is set on file. -w fileTrue if the file is writable. -x fileTrue if the file is executable. We re getting ahead of ourselves slightly, but following is an example of how we would test the state ofthe file /bin/bash, just so you can see what these look like in use: #!/bin/shif [ -f /bin/bash ] thenecho file /bin/bash exists fiif [ -d /bin/bash ] thenecho /bin/bash is a directory elseecho /bin/bash is NOT a directory fiBefore the test can be true, all the file conditional tests require that the file also exists. This list containsjust the more commonly used options to the testcommand, so for a complete list refer to the manualentry. If you re using bash, where testis built in, use the help testcommand to get more details. We ll use some of these options later in the chapter. Now that we know about conditions, we can look at the control structures that use them. You may be wondering what the set-group-id and set-user-id (also known as set-gidand set-uid) bits are. The set-uid bit gives a program the permissions of its owner, rather than its user, while the set-gid bit gives a program the permissions of itsgroup. The bits are set with chmod, using the s and g options. Remember that set-gidand set-uid flags have no effect when set on shell scripts. 34Chapter
Note: In case you are looking for affordable webhost to host and run your web application check Vision http web server services

We can also write it like (Simple web server) this: if

Monday, April 30th, 2007

We can also write it like this: if [ -f fred.c ] then… fiThe testcommand s exit code (whether the condition is satisfied) determines whether the conditionalcode is run. Note that you must put spaces between the [braces and the condition being checked. You can rememberthis by remembering that [is just the same as writing test, and you would always leave a space after If you prefer putting thenon the same line as if, you must add a semicolon to separate the test from if [ -f fred.c ]; then… fiThe condition types that you can use with the testcommand fall into three types: string comparison, arithmetic comparison, and file conditionals. The following three tables describe these condition types. String ComparisonResultstring1 = string2True if the strings are equal. string1 != string2True if the strings are not equal. -n stringTrue if the string is not null. -z stringTrue if the string is null (an empty string). Arithmetic ComparisonResultexpression1 -eq expression2True if the expressions are equal. expression1 -ne expression2True if the expressions are not equal. expression1 -gt expression2True if expression1is greater than expression2. expression1 -ge expression2True if expression1is greater than or equal toexpression2. expression1 -lt expression2True if expression1is less than expression2. expression1 -le expression2True if expression1is less than or equal toexpression2. ! expressionTrue if the expression is false, and vice versa.
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp web hosting services

Yahoo web hosting - How It WorksThis script creates the variable salutation,

Sunday, April 29th, 2007

How It WorksThis script creates the variable salutation, displays its contents, and then shows how various parame- ter variables and the environment variable $HOMEalready exist and have appropriate values. We ll return to parameter substitution in more detail later in the chapter. ConditionsFundamental to all programming languages is the ability to test conditions and perform different actionsbased on those decisions. Before we talk about that, though, we ll look at the conditional constructs thatwe can use in shell scripts and then look at the control structures that use them. Ashell script can test the exit code of any command that can be invoked from the command line, includ- ing the scripts that you have written yourself. That s why it s important to always include an exitcom- mand at the end of any scripts that you write. The test,or [,CommandIn practice, most scripts make extensive use of the [or testcommand, the shell s Boolean check. On mostsystems, the [and testcommands are synonymous, except that when the [command is used, a trailing]is also used just for readability. Having a [command might seem a little odd, but within the code itdoes make the syntax of commands look simple, neat, and more like other programming languages. # ls -l /usr/bin/[ lrwxrwxrwx 1 root root 4 Oct 13 10:46 /usr/bin/[ -> testWe ll introduce the testcommand using one of the simplest conditions: checking to see if a file exists. The command for this is test -f , so within a script we can writeif test -f fred.cthen… fiThese commands call an external program in some older UNIX shells, but they tendto be built in to more modern ones. We ll come back to this when we look at com- mands in a later section. Since the testcommand is infrequently used outside shell scripts, many Linux userswho have never written shell scripts try to write simple programs and call them test. If such a program doesn t work, it s probably conflicting with the shell s testcom- mand. To find out whether your system has an external command of a given name, try something like which test, to check which testcommand is getting executed, or use ./testto ensure you execute the script in the current directory. 32Chapter
Note: If you are looking for cheap webhost to host and run your apache application check Vision apache web hosting services

It s easy to see the difference between (Best web hosting) $@and

Sunday, April 29th, 2007

It s easy to see the difference between $@and $*by trying them out: $ IFS= $ set foo bar bam$ echo $@ foo bar bam $ echo $* foobarbam $ unset IFS$ echo $* foo bar bamAs you can see, within double quotes, $@expands the positional parameters as separate fields, regard- less of the IFSvalue. In general, if you want access to the parameters, $@is the sensible choice. In addition to printing the contents of variables using the echocommand, we can also read them byusing the readcommand. Try It Out Parameter and Environment VariablesThe following script demonstrates some simple variable manipulation. Once you ve typed the script it as try_var, don t forget to make it executable with chmod +x try_var. #!/bin/shsalutation= Hello echo $salutationecho The program $0 is now running echo The second parameter was $2 echo The first parameter was $1 echo The parameter list was $* echo The user s home directory is $HOME echo Please enter a new greeting read salutationecho $salutationecho The script is now complete exit 0If we run this script, we get the following output: $ ./try_var foo bar bazHelloThe program ./try_var is now runningThe second parameter was barThe first parameter was fooThe parameter list was foo bar bazThe user s home directory is /home/rickPlease enter a new greetingSireSireThe script is now complete$
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision ecommerce web hosting services

Environment VariableDescription$HOMEThe home directory of the current user. (Web server logs)

Sunday, April 29th, 2007

Environment VariableDescription$HOMEThe home directory of the current user. $PATHAcolon-separated list of directories to search for commands. $PS1Acommand prompt, frequently $, but in bash you can use somemore complex values; for example, the string [u@h W]$is apopular default that tells you the user, machine name, and currentdirectory, as well as giving a $ prompt. $PS2Asecondary prompt, used when prompting for additional input; usually >. $IFSAn input field separator; a list of characters that are used to sepa- rate words when the shell is reading input, usually space, tab, andnewline characters. $0The name of the shell script. $#The number of parameters passed. $$The process ID of the shell script, often used inside a script forgenerating unique temporary filenames; for example /tmp/tmp- file_$$. Parameter VariablesIf your script is invoked with parameters, some additional variables are created. Even if no parametersare passed, the preceding environment variable $#still exists but has a value of 0. The parameter variables are listed in the following table. Parameter VariableDescription$1, $2, …The parameters given to the script. $*Alist of all the parameters, in a single variable, separated by the firstcharacter in the environment variable IFS. $@Asubtle variation on $*; it doesn t use the IFS environment variable, so parameters may be run together if IFS is empty. If you want to check out how the program works in a different environment by run- ning the env , try looking at the envmanual pages. Also, we ll showyou later in the chapter how to set environment variables in subshells using theexportcommand. 30Chapter
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision virtual web hosting services

The behavior of variables such as $fooinside quotes (Web hosting company)

Saturday, April 28th, 2007

The behavior of variables such as $fooinside quotes depends on the type of quotes you use. If youenclose a $variable expression in double quotes, it s replaced with its value when the line is executed. Ifyou enclose it in single quotes, no substitution takes place. You can also remove the special meaningofthe $symbol by prefacing it with a . Usually, strings are enclosed in double quotes, which protects variables from being separated by whitespace, but allows $expansion to take place. Try It Out VariablesThis example shows the effect of quotes on the output of a variable: #!/bin/shmyvar= Hi there echo $myvarecho $myvar echo $myvar echo $myvarecho Enter some textread myvarecho $myvar now equals $myvarexit 0This gives the output: Hi thereHi there$myvar$myvarEnter some textHello World$myvar now equals Hello WorldHow It WorksThe variable myvaris created and assigned the string Hi there. The contents of the variable are dis- played with the echocommand, showing how prefacing the variable with a $character expands thecontents of the variable. We see that using double quotes doesn t affect the substitution of the variable, while single quotes and the backslash do. We also use the readcommand to get a string from the user. Environment VariablesWhen a shell script starts, some variables are initialized from values in the environment. These are mally capitalized to distinguish them from user-defined (shell) variables in scripts, which are conven- tionally lowercase. The variables created will depend on your personal configuration. Many are listed manual pages, but the principal ones are listed in the following table.
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp web hosting services

Web hosting directory - VariablesWe don t usually declare variables in the shell

Saturday, April 28th, 2007

VariablesWe don t usually declare variables in the shell before using them. Instead, we create them by simply usingthem (for example, when we assign an initial value to them). By default, all variables are considered andstored as strings, even when they are assigned numeric values. The shell and some utilities will convertnumeric strings to their values in order to operate on them as required. Linux is a case-sensitive system, so the shell considers the variable foo to be different from Foo and both to be different from FOO. Within the shell we can access the contents of a variable by preceding its name with a $. Whenever weextract the contents of a variable, we must give the variable a preceding $. When we assign a value to avariable, we just use the name of the variable, which is created dynamically if necessary. An easy way ofchecking the contents of a variable is to echo it to the terminal, preceding its name with a $. On the command line, we can see this in action when we set and check various values of the variablesalutation: $ salutation=Hello$ echo $salutationHello$ salutation= Yes Dear $ echo $salutationYes Dear$ salutation=7+5$ echo $salutation7+5We can assign user input to a variable by using the readcommand. This takes one parameter, the nameof the variable to be read into, and then waits for the user to enter some text. The read normally com- pletes when the user presses Enter. When reading a variable from the terminal, we don t usually needthe quote marks: $ read salutationWie geht s? $ echo $salutationWie geht s? QuotingBefore we move on, we need to be clear about one feature of the shell: the use of quotes. Normally, parameters in scripts are separated by whitespace characters (e.g., a space, a tab, or a newlinecharacter). If you want a parameter to contain one or more whitespace characters, you must quote theparameter. Note how a string must be delimited by quote marks if it contains spaces. Also notethat there must be no spaces on either side of the equals sign. 28Chapter
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

Anonymous web server - Once you re confident that your script is executing

Saturday, April 28th, 2007

Once you re confident that your script is executing properly, you can move it to a more appropriate tion than the current directory. If the command is just for yourself, you could create a bindirectory home directory and add that to your path. If you want the script to be executable by others, youcould use /usr/local/binor another system directory as a convenient location for adding new pro- grams. If you don t have root permissions on your system, you could ask the system administrator your file for you, although you may have to convince them of its worth first. To prevent other changing the script, perhaps accidentally, you should remove write access from it. The sequence for the administrator to set ownership and permissions would be something like this: # cp first /usr/local/bin# chown root /usr/local/bin/first# chgrp root /usr/local/bin/first# chmod 755 /usr/local/bin/firstNotice that, rather than altering a specific part of the permission flags, we use the absolute form of because we know exactly what permissions we require. If you prefer, you can use the rather longer, but perhaps more obvious, form of the chmodcommand, which would be# chmod u=rwx,go=rx /usr/local/bin/firstCheck the manual page of chmodfor more details. Shell SyntaxNow that we ve seen an example of a simple shell program, it s time to look in greater depth at the pro- gramming power of the shell. The shell is quite an easy programming language to learn, not least becauseit s easy to test small program fragments interactively before combining them into bigger scripts. We the bash shell to write quite large, structured programs. In the next few sections, we ll cover .Variables: strings, numbers, environments, and parameters .Conditions: shell Booleans .Program control: if, elif, for, while, until, case .Lists .Functions .Commands built into the shell .Getting the result of a command .Here documentsIn Linux you can delete a file if you have write permission on the directory that con- tains it. To be safe, you should ensure that only the superuser can write to directoriescontaining files that you want to keep safe. This makes sense because a directory isjust another file, and having write permission to a directory file allows users to addand remove names.
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision php5 hosting services