Wednesday, April 21, 2010
What is cloud computing?
Here are some important things that i have found about CLOUD COMPUTING
Cloud computing is Internet-based computing, whereby shared resources, software and information are provided to computers and other devices on-demand, like a public utility.
(http://en.wikipedia.org/wiki/Cloud_computing)
That's how the Wiki says... you have already used these concepts in your day to day life, for example Gmail,Yahoo,You-tube. They don't require server or software. you just need an internet connection. It's getting closer to our day-to-day life, isn't it??. This quote caught my mind 'If you only need milk , would you buy a cow ?',
Simply why don't you go for Milk without having a cow.. in simple words that's what the CLOUD COMPUTING concept is....
Cloud computing is Internet-based computing, whereby shared resources, software and information are provided to computers and other devices on-demand, like a public utility.
(http://en.wikipedia.org/wiki/Cloud_computing)
That's how the Wiki says... you have already used these concepts in your day to day life, for example Gmail,Yahoo,You-tube. They don't require server or software. you just need an internet connection. It's getting closer to our day-to-day life, isn't it??. This quote caught my mind 'If you only need milk , would you buy a cow ?',
Simply why don't you go for Milk without having a cow.. in simple words that's what the CLOUD COMPUTING concept is....
Friday, April 9, 2010
How To Deploy Gadgets in WSO2 Gadgets repository
How To Deploy Gadgets in WSO2 Gadgets repository 
Deploying Gadgets in the repository is quite straightforward. However this is an administrator's privilege. You need to login to WSO2 Gadget Server as the administrator at (https://localhost:9443/carbon) and navigate to the registry browser.

WSO2 Gadget Server is by default shipped with some of the WSO2 Governance Registry features. By using those registry/repository, a well equipped enterprise gadget repository can be maintained.

Next you should browse to /registry/resource/system/gadgets. In the registry and at this location upload your gadget XML file as an XML resource.

Once the gadget XML is uploaded, you should navigate to the Gadget Repository in the administrators view and open the Add Gadget form (Gadget Repository → Add New Gadget),


Fill the Add New Gadgets form accordingly and for the gadgets URL, you should provide the registry URL for the resource, which is /registry/resource/system/gadgets/feedReader.xml and save the created gadget entry. Now the newly created gadget will be added to the repository, and all the users will be able to find this gadget simply by browsing the repository.
NOTE: If the Gadget XML is hosted in another Web Server, you can also add that to the repository by simply adding its HTTP:// URL as the gadget URL.
If you are a normal user and just want to test your gadget in WSO2 Gadget Server, you can host your Gadget XML in a public location and provide its HTTP URL (The XML file should be able to access via HTTP) to the portal's add-gadget by URL location.
Deploying Gadgets in the repository is quite straightforward. However this is an administrator's privilege. You need to login to WSO2 Gadget Server as the administrator at (https://localhost:9443/carbon) and navigate to the registry browser.

WSO2 Gadget Server is by default shipped with some of the WSO2 Governance Registry features. By using those registry/repository, a well equipped enterprise gadget repository can be maintained.

Next you should browse to /registry/resource/system/gadgets. In the registry and at this location upload your gadget XML file as an XML resource.

Once the gadget XML is uploaded, you should navigate to the Gadget Repository in the administrators view and open the Add Gadget form (Gadget Repository → Add New Gadget),


Fill the Add New Gadgets form accordingly and for the gadgets URL, you should provide the registry URL for the resource, which is /registry/resource/system/gadgets/feedReader.xml and save the created gadget entry. Now the newly created gadget will be added to the repository, and all the users will be able to find this gadget simply by browsing the repository.
NOTE: If the Gadget XML is hosted in another Web Server, you can also add that to the repository by simply adding its HTTP:// URL as the gadget URL.
If you are a normal user and just want to test your gadget in WSO2 Gadget Server, you can host your Gadget XML in a public location and provide its HTTP URL (The XML file should be able to access via HTTP) to the portal's add-gadget by URL location.
Saturday, April 3, 2010
Web Mashups and Web Gadgets
Web Mashups and Web Gadgets Dashboard...
As companies look for more ways of attracting visitors to their web sites and generating business online, personalization is emerging as an important component of web experience management.The idea behind personalization is straightforward. Instead of having one web site that is the same for all visitors, you give each visitor a unique personal flavor of the site that matches their interests and preferences. This ensures that your web site delivers optimal web experience to each visitor. As a result, visitors stay on your site longer, return to it more often, and are more inclined to use the site to conduct business with your company.
Web gadget mashups are quickly gaining in popularity as a powerful personalization tool. With the gadget mashup functionality enabled on web your site, you can give each site visitor a personal area, where they can quickly assemble their personal pages by combining a few gadgets. Typically, each gadget provides access to a separate source of information or to a different application. For instance, one gadget can be used to display financial news. Another gadget can be used to show the weather forecast at visitor’s location. Yet another gadget can be used to display a mortgage calculator. And so on. Your company can provide some of these gadgets. The majority of them, however, will be provided by others. The personal pages your visitors build become mashups of content and applications from different sources.

There are many examples of this technology in action. Among the publicly available web sites, igoogle.com and netvibes.com are probably the most well known ones. A number of media companies, such as TV broadcasters and newspapers, have set up their own gadget mashups. BBC has a great one where one can mix and match gadgets with information from the numerous areas on BBC web sites. As a result, each BBC visitor can create a personal news page, which is a great example of personalization in action. Instead of buying a newspaper and reading 10% of its content, one can now create a personalized news dashboard at BBC and read all of it with a cup of coffee in the morning.
Web Mashups as a Business Tool
There are a few ways of participating in the web gadget ecosystem. First, you can be a provider of web gadgets. Second, you can be a provider of a web gadget dashboard. Third, you can be doing both.The first one is a no-brainer. As a web site owner, you want traffic on your web site. You can use either paid advertising or referrals. Making engaging content or application available as a web gadget is an excellent web referrals mechanism. As the gadget starts appearing on the various gadget dashboards, it will start drawing traffic to your site.
UNIQLO, a Japanese casual clothes retailer, is a great example in this category. The retailer has a web site that exhibits their latest products. However, they are just one of the many clothing retailers online. As a promotion action, the company started an innovative web project aptly named UNIQLOCK. It is nothing but a clock that is displayed in the web browser. The clock has an interesting twist to it though. Every few seconds it is replaced with a full-window view of ballet dancers doing what it is that they normally do and wearing the company’s products. The project became an instant hit on the Internet. One can go to YouTube and see many trial videos of aspiring UNIQLOCK dancers. It was also covered in many blog posts. Cleverly, the company made the clock available as a web gadget. This gadget is now sitting in numerous web dashboards and blog entries and drawing traffic to the company web site. This is viral marketing at its best and web gadget mashups are one of the enabling components.
Becoming a provider of a gadget dashboard can be an equally powerful personalization and marketing tool. This may be not be as obvious at first. I often hear doubts from people who think that by offering a dashboard they may be competing with Google, Yahoo, or BBC. These doubts are often unfounded. It is not a matter of competing with a popular content aggregator. It is, instead, a matter of delivering effective personalization to your web site visitors.
If your web site contains many distinct areas with content in different categories, you can make this content available as web gadgets, one gadget per area. The home page of your web site can then become a gadget dashboard where registered users can create their personal combinations of gadgets as they deem necessary or convenient.
The beauty of a gadget dashboard on your site is that you can reserve one of the gadget slots for displaying promotional materials. Content in this gadget can be driven by content in other gadgets. For instance, a promotion for a soccer ball can be shown next to a gadget showing soccer scores. A promotion for a raincoat can be shown next to a gadget showing local weather with a rain forecast. Not only do you deliver improved web experience to your web site visitors, you also achieve better click through rates on your web ads.

Serving Gadgets
Let us first consider what is required to become a provider of web gadgets. There are several approaches.The simplest one is to make your web site content available as an RSS feed. There are many RSS readers available as web gadgets. It is possible to add such a gadget to a gadget dashboard and point it to your site’s feed. All the updates that you post on your site will automatically be displayed in this gadget.
If you are lucky and your content is good, you may also see a few gadgets developed by web enthusiasts with the sole purpose of displaying feeds from your web site. There are many such gadgets available for CNN, New York Times, and other feed providers.
The problem with the simplest approach, of course, is that it requires your readers to think and work in order to display your content. They have to find an RSS gadget and configure it to process an RSS feed from your web site.

An alternative approach is to use a Web Content Management (WCM) system that can automatically render your web content as gadgets compatible with a number of popular gadget dashboards such as igoogle, netvibes, and others. Modern WCM systems can easily repurpose content for different channels such as web, print, email, and mobile. Gadget dashboards for them is just another content delivery channel. If your WCM system does not support gadgets out of the box, it can perhaps be customized to do so.
Finally, your can ask a Web application developer or a designer to wrap your content in JavaScript and XML required to make it look and work as a Web gadget. You will most certainly have to follow this route if you want to achieve engaging user experience similar to the one delivered by UNIQLO. Such gadgets are usually built as rich Internet applications (RIA) that dynamically access content stored in a Web content management system.
Hosting Gadgets
In order to host a gadget dashboard on your site you need a gadget server. This server can be deployed side-by-side with your web server or your web content management system. It performs several important functions.
First, it maintains a catalog of web gadgets that are available to the visitors of your site. This is the so-called gadget white list. There may be millions of gadgets available on the Internet. Some of these gadgets may have inappropriate content. Some of them may divert traffic to competing sites. You, therefore, need to limit the set of gadgets that can appear on your site. A gadget server gives you the tool that makes this possible.
Second, the gadget server maintains a list of gadgets for each of the users registered on your site. As users build their personal gadget dashboards on your site, all the relevant information is stored and processed by the gadget server. When a visitor returns to your site, his or her gadget dashboard is rendered automatically. A good gadget server can also guess a list of gadgets that must be shown to an unregistered user. This list can be deduced from the visitor’s geographic location or from their past behavior on your site.
Third, the gadget server maintains settings and preferences entered by the site visitors for the various gadgets. Two different visitors, for instance, may both choose a weather gadget for their dashboards. One of them may set the gadget to show the weather in New York, while the other one may configure the gadget to show the weather in San Francisco. Their preferences will be stored and managed by the gadget server.
Fourth, the gadget server must provide a platform for rendering gadgets in a web browser. This is a very sophisticated component that handles the dashboard layout, secure inter-gadget communication, drag-and-drop and rearrangement of gadgets in the browser, security, web single sign-on, and many other important aspects of gadget mashups.
These are just some of the gadget server facilities. There may be many others depending on the focus of the vendor offering the gadget server such as integration with social networks, integration with WCM systems, support for server side gadget specific or user specific storage, etc.
Summary
Web gadgets and gadget dashboards are a very powerful personalization mechanism and an effective business tool. You can use gadgets placed in popular gadget dashboards, such as igoogle or netvibes, to draw traffic to your site. A gadget dashboard offered on your web site can be used by the site visitors to assemble personalized web pages with the information and layout that reflects their interests and personal preferences. It can also be used to enrich user experience by attracting gadgets from third party content and application providers.Sunday, March 28, 2010
Web Portal (WSO2 )
WSO2 Gadget Server
Installing on Microsoft Windows
Note: The WSO2 Gadget Server release has been successfully tested only on Windows XP & Windows 2003 server. You can use the install.bat file in the Gadget Server distribution to install Gadget Server as a Windows NT service. This installation script is available at CARBON_HOME/bin. Here are the steps:
- Download the WSO2 Gadget Server binary distribution.
 - Extract the zip archive.
 - Set the JAVA_HOME environment variable to your Java installation.
 - From the command prompt run bin/install.bat
 
Installing on Linux
- Download the WSO2 Gadget Server binary distribution.
 - Extract the zip archive. e.g., unzip wso2gs-1.0.zip -d /opt
 - Set the JAVA_HOME environment variable to your Java installation using the export command or by editing /etc/profile. e.g., export JAVA_HOME="/opt/j2sdk"
 - Execute the WSO2 Gadget Server starting script. e.g., cd /opt/wso2gs-1.0/bin; sh wso2server.sh
 - Check your WSO2 Gadget Server instance using the URL https://localhost:8443/carbon/
 
WSO2 Mashup Server
Installing using Binary Distribution
- Download the WSO2 Mashup Server binary distribution or get it from your friend.
 - Extract the zip archive.
 - Set the JAVA_HOME environment variable to your Java installation(C:\Program Files\Java\jdk1.6.0_02.)
 - Click on the File called Install.bat (inside the bin folder install.bat)
 - Select option 1(for Installation as Windows NT Service)
 - After the installation Execute the WSO2 Mashup Server Batch file (wso2server)
 - Check your WSO2 Mashup Server instance using the URL https://localhost:9443/carbon/
 
By now you all know what is a web portal and also get some idea by watching this videos...Guys let's start working...
Here I found something nice..you can see what's a real web portal and you can customize it very easily, have a look on this also...
http://www.netvibes.com/
Jonathan Marsh, VP of Business Development of WSO2, recently released three screencasts explaining the features of WSO2 Mashup Server and how you can use these features in building your own Mashups.
The first screencast targeted at newbies to introduce the mashup  concept and WSO2 Mashup Server and the next two videos present you the  topics like “Scraping Web Pages” and “Service Composition” which are  useful in building advance mashups.
- Hello World
 - Scraping Web Pages
 - Service Composition
 
WSO2 Gadget Server
If you want to deploy the Mashup server on Apache Tomcat.. click on this Link
Deploying WSO2 Mashup Server on Apache Tomcat
Tuesday, January 26, 2010
SHELL SCRIPTING (it'll be helpful friends.. check diz out!!)
Bash what?
         Bash what?
           Okay, I grant that this page might represent a leap from the familiar to the alien without much warning. Here are some explananatory notes:            
         - Under Linux, there are some powerful tools that for all practical purposes are unavailable under Windows (I can imagine all the old Linux hands saying "Duh!").
 - One of these tools is something called "shell programming". This means writing code that a command shell executes.
 - There is something like this under Windows, but as usual, the Windows version is a weak imitation.
 - The most common Linux shell is named "Bash". The name comes from "Bourne Again SHell," which, in turn ... (imagine a lengthy recursion terminating in a caveman's grunt).
 - There are many other shells available. Unless there is a compelling reason not to, I recommend that people stick to the Bash shell, because this increases the chance that your scripts will be portable between machines, distributions, even operating systems.
 - I'll be showing some very basic examples of Bash shell programming on this page, and I want to say at the outset that shell programming is an art, not a science. That means there is always some other way to do the same thing.
 - Because shell programming is an art, please don't write to say, "Wow, that was a really inefficient way to do such-and-such." Please do write (message page) to report actual errors.
 - If this page seems too sketchy and elementary for your taste, you can choose from among the more advanced resources in this list.
 
Introduction
         - Early computers had a teletype machine with a keyboard for I/O. Later, glass terminals became the norm, but the behavior was much the same — a keyboard, a screen, a text display. A program was responsible for mediating the transaction between the operator and the machine, and as the years passed this program (the command interpreter or shell) became more sophisticated.
 - At this stage the command shell has become rather too sophisticated, typically having a dozen ways to do any particular thing. In this page I will try to limit myself to describing a handful of useful operations, based not on listing everything that can be done, but on solving specific problems. There are some links at the bottom of this page for those wanting more depth.
 
Preliminaries
         - There are two primary ways to use the shell: interactively and by writing shell scripts.                
- In the interactive mode, the user types a single command (or a short string of commands) and the result is printed out.
 - In shell scripting, the user types anything from a few lines to an entire program into a text editor, then executes the resulting text file as a shell script.
 - It is often the case that an interactive session becomes a shell scripting session, once things get too complicated for simple interactive line entries, or because a specific sequence of commands appears to be generally useful and worth preserving.
 
 - In a modern Linux environment the user can have more than one shell open at a time, either by moving between a sequence of independent "virtual terminals" in a text-only environment, or by opening any number of shell windows in the X Windows environment.
 - The advantage of having more than one shell available is that one shell can be used for testing one command at a time, while another might provide a text editor for assembling single commands into a shell program.
 - I don't want to get too distribution-specific, but if you are not hosting X Windows and want more than one simultaneous shell session, with many current distributions you can switch between "virtual terminals" by pressing Ctrl+Alt+F(n), n typically between 1 and 6.
 - In an environment that supports X Windows, simply open any desired number of command shell windows and move between them.
 
Simple Stuff
         - First, a convention. I'll list things for you to type in this format: $ date I will list the computer's reply like this: Tue Dec 23 10:52:51 PST 2003 Notice the "$" symbol in the user entry above. This is a generic shell prompt, and yours will almost certainly look different (but it will include a similar symbol). I'll be using one of two prompts (this is a common convention, worth remembering): I'll use "$" to refer to a normal user session, and "#" to refer to a root session.
 - NOTE: Avoid using root sessions and permissions unless it is required. Misused root authority can cause very serious harm to your system. Since this is a tutorial in which you will want to experiment with different commands, limit the chance for harm by doing so as an ordinary user.
 - To put this another way, enter this example:                # whoami
root If your session produced the result shown above, please — log out and become an ordinary user. - In shell programming, spaces matter. If you see spaces between words and characters in these examples, be sure to include the spaces.
 - In shell programming, case matters also. If you don't get the results shown on this page, look at the case of your entries.
 
Where are you?
         - As you may be aware, a Linux filesystem is in the form of a large tree with many branches called "subdirectories". When you issue a shell command, it is often necessary to know where you are in the "tree". Type this example:                $ pwd
/path/path/path When you try this example ("pwd" means "print working directory"), your current working directory will be printed. - You can decide where you are in the tree. Type this example:                $ cd ~
$ pwd
/home/username The symbol "~" is a special shortcut character that can be used to refer to your home directory. You could have typed this — $ cd /home/username — and accomplished the same result, but if you think about it, the "~" character is more portable. Later, when you are writing shell scripts, you might want a command that moves to any user's home directory. 
Listing Files
         -                Directories contain files, and you can list them in a simple, compact format:                $ ls
filename filename filename ... Or you can list them in more detail: $ ls -la
(detailed list, one file per line) And, very important, to find out what a command's options are, use the "man" (manual) command: $ man ls
(manual page for "ls") NOTE: The "man" command allows you to learn a command's options. You still have to remember the command's name. To find files by name: $ find . -name '*.jpg'
(list of files with .jpg suffix in current and all child directories) To create a text diagram of the directory tree: $ tree -d .
(diagram of the directory tree from the current directory) The "tree" command is less useful now that directory trees have become so complicated, and now that most distributions support X Windows and sophisticated filesystem browsers. 
Examining Files
         -                There are a number of things you can do to find out more about the files in the list. Here are just a few:                The "file" command tries to identify files by examining their contents:                $ file tux_small.png
tux_small.png: PNG image data, 128 x 151, 8-bit/color RGB, non-interlaced The next example uses the obscurely named "cat" command. It prints the contents of a file. Unfortunately if the file's contents are not readable, they get printed anyway. $ cat zipcodes.txt
(prints the entire contents of a file named "zipcodes.txt") If a file is too long to be viewed on one page, you can say: $ more zipcodes.txt
(prints file one screenful at a time) You can also use "grep" to print only those parts of a file you are interested in: $ grep 10001 zipcodes.txt
(prints only those lines that have the character string "10001" in them) The "grep" command is very useful, unfortunately it has a difficult-to-remember name. Be sure to: $ man grep 
Pipelines and Redirection
         - You can use a pipeline (symbolized by "|") to make the output of one command serve as the input to another command. This idea can be used to create a combination of commands to accomplish something no single command can do.                Enter this command:                $ echo "cherry apple peach"
cherry apple peach Okay, let's say we want to sort these words alphabetically. There is a command "sort", but it sorts entire lines, not words, so we need to break this single line into individual lines, one line per word. Step one: pipe the output of "echo" into a translation (tr) command that will replace spaces with linefeeds (represented by "\n"): $ echo "cherry apple peach" | tr " " "\n"
cherry
apple
peach Success: each word appears on a separate line. Now we are ready to sort. Step two: add the sort command: $ echo "cherry apple peach" | tr " " "\n" | sort
apple
cherry
peach Let's try reversing the order of the sort: $ echo "cherry apple peach" | tr " " "\n" | sort -r
peach
cherry
apple - Remember: A pipeline ("|") takes the output of one command and makes it the input to another command.
 - Normally the output from commands is printed on the screen. But using the symbol ">", you can redirect the output to a file:                $ date > RightNow.txt
$ cat RightNow.txt
Tue Dec 23 14:43:33 PST 2003 The above example used ">" to replace the content of any existing file having the name "RightNow.txt". To append new data to an existing file, use ">>" instead: $ date >> RightNow.txt
$ cat RightNow.txt
Tue Dec 23 14:43:33 PST 2003
Tue Dec 23 14:46:10 PST 2003 - Remember: Use ">" to overwrite any existing file, use ">>" to append to any existing file. In both cases, if no file exists, one is created.
 - Many commands have inputs as well as outputs. The input defaults to the keyboard, the output defaults to the screen.
 - To redirect the output to a file, use ">" or ">>" as shown above.
 - To make the output of a command serve as the input of another command, use "|".
 - To make the contents of a file serve as the input to a command, use "<":                $ wc <>
2 12 58 As is so often the case in shell programming, there is at least one other way to produce the above result: $ cat RightNow.txt | wc
2 12 58 
Shell Script Basics
         - A shell script is a plain-text file that contains shell commands. It can be executed by typing its name into a shell, or by placing its name in another shell script.
 - To be executable, a shell script file must meet some conditions:                
- The file must have a special first line that names an appropriate command processor. For this tutorial, the following will work in most cases: #!/bin/bash If this example doesn't work, you will need to find out where your Bash shell executable is located and substitute that location in the above example. Here is one way to find out: $ whereis bash
 - The file must be made executable by changing its permission bits. An example: $ chmod +x (shell script filename)
 
 - A shell script file may optionally have an identifying suffix, like ".sh". This only helps the user remember which files are which. The command processor responsible for executing the file uses the executable bit, plus the file's first line, to decide how to handle a shell script file.
 - One normally executes a shell script this way: $ ./scriptname.sh This special entry is a way to tell the command processor that the desired script is located in the current directory. Always remember: if you cannot get your shell script to run, remember this trick to provide its location as well as its name.
 
First Shell Script
         - This will get you past the details of writing and launching a simple script.                
- Choose a text editor you want to use. It can be a command-line editor like emacs, pico or vi, or an X Windows editor if you have this option.
 - Run your choice of editor and type the following lines:                    
NOTE: Be sure to place a linefeed at the end of your script. Forgetting a terminating linefeed is a common beginner's error.
#!/bin/bash
echo "Hello, world."
 - Save the file in the current working directory as "myscript.sh".
 - Move from the text editor to a command shell.
 - From the command shell, type this: $ chmod +x myscript.sh
 - To execute the script, type this:                    $ ./myscript.sh
Hello, world. 
 - These steps will become second nature soon enough.
 
Tests and Branching
         - Bash shell scripts can perform, and act on, various kinds of tests. This will be just the most basic introduction — see the reference material at Useful Links for more on this rather baroque topic.
 - To follow these and other examples that involve multiline shell scripts, please set up to edit and run a test script file (let's call it "myscript.sh") that you can use to enter and test various options. And remember that the examples won't include the all-important first line of the script (see script examples above) — it will be assumed to exist in each case. Also, to save time, you may want to copy some of the shell code examples from this page directly into your editor.
 - Here is an example of a test and branch:                
Run the test script: $ ./myscript.sh
if [ -e . ]
then
echo "Yes."
else
echo "No."
fi
Yes. We created a test (the part of the script between "[" and "]") which tested whether a particular element existed ("-e"). Because the symbol "." in this context means the current directory, the test succeeded. Try replacing the "." with something that is not present in the current directory, example "xyz". See how the outcome changes. It is important to realize that "[" is an alias for the command "test". The script could have been written as:
NOTE: Be sure to read the "test" man page to find out all the different tests that are available: $ man test
if test -e .
then
echo "Yes."
else
echo "No."
fi
Before we move on, there is a perversity about tests in Bash shells that I want to discuss. It turns out, because of a historical accident that now might as well be cast in concrete, when a test is conducted or a command returns a result value, the numerical value for "true" is 0, and "false" is 1. Those of you who have some programming experience will likely find this reversal of intuition as annoying as I do. Here is a way to get the result of the most recent logical test (and to show the weird reversal described above): $ test -e .
$ echo $?
0 $ test -e xyz
$ echo $?
1 Please remember this reversal, because it confounds the process of thinking through, and constructing, logical tests. For example, you may want to write a shortcut form for a test that only acts on one kind of result: $ test -e . && echo "Yes."
Yes. This sort of shorthand relies on some knowledge of logical processing — if the left-hand part of an AND test yields "true", then the right-hand part must also be evaluated, and so it is. But the numerical "true" value for the left-hand test is 0, which would argue for the opposite logic. Just to show how perverse this all is, here is an example of Bash logical testing that comes out the opposite way: $ echo $(( 0 && 0 ))
0 $ echo $(( 1 && 0 ))
0 $ echo $(( 0 && 1 ))
0 $ echo $(( 1 && 1 ))
1 Yes, just as you would expect. So do be on guard against this shell "gotcha", which only affects the outcome of tests and command result values. It probably will not surprise you to learn that no one mentions this strange anomaly in the official Bash documentation. A couple of rules about logical operators used as branches:- If you write "test && command", the command will only be executed if the test succeeds.
 - If you write "test || command", the command will only be executed if the test fails.
 
Yes. $ false || echo "Yes."
Yes. Notice that the outcomes are entirely in keeping with one's intuition about such logical comparisons, and all is well as long as you don't think about the fact that true equals 0. :) Here's another scheme commonly seen in shell script programming and interactive sessions: $ command1 && command2 && command3 && command4 This line of code will not run the next command in the sequence unless the prior command has returned "true", meaning no errors. It is a way to avoid running a command if a required prior outcome is not present. 
Loops and Repetition
         -                Here are some examples of loop operators:                
In this example, the "*" is expanded by the shell to a list of all the files in the current directory, then each filename is applied to the loop control area. In such a construct, any whitespace-delimited list will do:
for fn in *; do
echo "$fn"
done
$ ./myscript.sh
for fn in tom dick harry; do
echo "$fn"
done
tom
dick
harry
This method will work for any list that uses spaces as delimiters. But what happens if you must parse a list that must be delimited by linefeeds instead of spaces, such as the case of a list of filenames or paths that contain spaces as part of their names? You can solve such a problem this way (there are other solutions):
This example uses an option to "ls" (note: the option is "-" followed by the numerical digit "1", not a lowercase "L") that formats file listings with one name per line, then this list is pipelined to a routine that reads lines until there are no more to read. This meets the requirement that linefeeds become the delimiters between list elements, not spaces. There is plenty more to this topic. Please refer to the list of Useful Links for more.
ls -1 | while read fn; do
echo "$fn"
done
 
Using Numbers in Scripts
         -                Contrary to a sometimes-expressed view, numbers can easily be accommodated in scripts. Example:                
$ ./myscript.sh
n=1
while [ $n -le 6 ]; do
echo $n
let n++
done
1
2
3
4
5
6
Notice the "let" command, which treats its argument in a way meant to accommodate numbers. Here is a somewhat more complex example:
$ ./myscript.sh
y=1
while [ $y -le 12 ]; do
x=1
while [ $x -le 12 ]; do
printf "% 4d" $(( $x * $y ))
let x++
done
echo ""
let y++
done
1 2 3 4 5 6 7 8 9 10 11 12
2 4 6 8 10 12 14 16 18 20 22 24
3 6 9 12 15 18 21 24 27 30 33 36
4 8 12 16 20 24 28 32 36 40 44 48
5 10 15 20 25 30 35 40 45 50 55 60
6 12 18 24 30 36 42 48 54 60 66 72
7 14 21 28 35 42 49 56 63 70 77 84
8 16 24 32 40 48 56 64 72 80 88 96
9 18 27 36 45 54 63 72 81 90 99 108
10 20 30 40 50 60 70 80 90 100 110 120
11 22 33 44 55 66 77 88 99 110 121 132
12 24 36 48 60 72 84 96 108 120 132 144
 
Coping with user input
         -  Here is an example that relies on user input to decide what to do. It exploits a shell feature as an easy way to create a menu of choices:                
When run, it looks like this: $ ./myscript.sh
PS3="Choose (1-5):"
echo "Choose from the list below."
select name in red green blue yellow magenta
do
break
done
echo "You chose $name."
As written, this menu code won't catch some kinds of errors (like a number that is out of range). In any application where the user choice must fall into defined bounds, be sure to perform a test on the result before using it. Example:
Choose from the list below.
1) red
2) green
3) blue
4) yellow
5) magenta
Choose (1-5):4
You chose yellow.
if [ "$name" = "" ]; then
echo "Error in entry."
exit 1
fi
 
An advanced example with numbers and user input
         -                Here is an example guessing game that ties together some of the elements we've covered so far:                
Please study this example carefully, and refer to the reference materials in Useful Links to understand some of the methods.
secretNumber=$(( ((`date +%N` / 1000) % 100) +1 ))
guess=-1
while [ "$guess" != "$secretNumber" ]; do
echo -n "I am thinking of a number between 1 and 100. Enter your guess:"
read guess
if [ "$guess" = "" ]; then
echo "Please enter a number."
elif [ "$guess" = "$secretNumber" ]; then
echo -e "\aYes! $guess is the correct answer!"
elif [ "$secretNumber" -gt "$guess" ]; then
echo "The secret number is larger than your guess. Try again."
else
echo "The secret number is smaller than your guess. Try again."
fi
done
 
Creating and using arrays
         -                Shell arrays are relatively easy to construct. Example:                
Run this example: $ ./myscript.sh
array=(red green blue yellow magenta)
len=${#array[*]}
echo "The array has $len members. They are:"
i=0
while [ $i -lt $len ]; do
echo "$i: ${array[$i]}"
let i++
done
Now, before you decide this is a silly, rather useless example, replace one line of the script and run it again:
The array has 5 members. They are:
0: red
1: green
2: blue
3: yellow
4: magenta
See what difference this makes (and think of all the kinds of lists you might create for this line).
array=(`ls`)
 
Strings and substrings
         -  It's useful to be able to take strings apart and put them together in different ways. Here is how to select a substring from a string:                
In this example, the variable "substring" contains the word "substring". Remember this rule:
string="this is a substring test"
substring=${string:10:9}
The string starting position is zero-based.
substring=${string_variable_name:starting_position:length}
 
Searching and Replacing Substrings within Strings
         -                In this method you can replace one or more instances of a string with another string. Here is the basic syntax:                
The string "beta" now contains an edited version of the original string in which the first case of the word "test" has been replaced by "replace". To replace all cases, not just the first, use this syntax:
alpha="This is a test string in which the word \"test\" is replaced."
beta="${alpha/test/replace}"
Note the double "//" symbol. Here is an example in which we replace one string with another in a multi-line block of text:
beta="${alpha//test/replace}"
Run this example: $ ./myscript.sh
list="cricket frog cat dog"
poem="I wanna be a x\n\
A x is what I'd love to be\n\
If I became a x\n\
How happy I would be.\n"
for critter in $list; do
echo -e ${poem//x/$critter}
done
Silly example, huh? It should be obvious that this search & replace capability could have many more useful purposes.
I wanna be a cricket
A cricket is what I'd love to be
If I became a cricket
How happy I would be.
I wanna be a frog
A frog is what I'd love to be
If I became a frog
How happy I would be.
I wanna be a cat
A cat is what I'd love to be
If I became a cat
How happy I would be.
I wanna be a dog
A dog is what I'd love to be
If I became a dog
How happy I would be.
 
More obscure but useful string operations
         -  Here is a way to isolate something useful from a large, even multi-line, string. As above, this method relies on enclosing a variable name in curly braces, then aplying a special operator to achieve a particular result.                Here is a list of four such operators:                
- Operator "#" means "delete from the left, to the first case of what follows."                   
$ x="This is my test string."
$ echo ${x#* }
is my test string.
 - Operator "##" means "delete from the left, to the last case of what follows."                   
$ x="This is my test string."
$ echo ${x##* }
string.
 - Operator "%" means "delete from the right, to the first case of what follows."                   
$ x="This is my test string."
$ echo ${x% *}
This is my test
 - Operator "%%" means "delete from the right, to the last case of what follows."                   
$ x="This is my test string."
$ echo ${x%% *}
This
 
There's lots of information, more than we need. Let's say for the sake of argument that I want the IP of "lo", the loopback interface. I could specify this:
$ x=`/sbin/ifconfig`
$ echo $x
eth0 Link encap:Ethernet HWaddr 00:0D:56:0C:8D:10
inet addr:192.168.0.1 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::20d:56ff:fe0c:8d10/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:253339 errors:0 dropped:0 overruns:0 frame:0
TX packets:423729 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:36150085 (34.4 MiB) TX bytes:496768499 (473.7 MiB)
Base address:0xecc0 Memory:fe4e0000-fe500000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:109394 errors:0 dropped:0 overruns:0 frame:0
TX packets:109394 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:12372380 (11.7 MiB) TX bytes:12372380 (11.7 MiB)
But, while gobbling text from the left, this search would stop at the IP address of eth0, not the desired interface. So I can specify it this way:
$ y=${x#*inet addr:}
As a last step I'll trim off all remaining text to the right:
$ y=${x#*lo *inet addr:}
Leaving only the desired address. It seems the "#" and "%" operators, and their variants, are able to accept a rather complex argument and sort through the content of large strings, including strings with line breaks. This means I can use the shell to directly filter content in some simple cases where I might have considered using sed or Perl.
$ y=${y%% *}
 - Operator "#" means "delete from the left, to the first case of what follows."                   
 
Bash Version 3
                    I have always thought the inability to test for the presence of a string or pattern (without using grep, sed or something similar) was a conspicuous weakness in shell programming. Bash version 3, present on must current Linux distributions, addresses this lack by allowing regular expression matching.
Let's say we need to establish whether variable $x appears to be a social security number:
if [[ $x =~ [0-9]{3}-[0-9]{2}-[0-9]{4} ]]
then
# process SSN
else
# print error message
fi
Notice the Perlish "=~" syntax and that the regular expression appears within double brackets. A substantial number of regular expression metacharacters are supported, but not some of the Perl-specific extensions like \w, \d and \s.
Another Bash 3 feature is an improved brace expansion operator:
$ echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
for n in {0..5}
do
echo $n
done
0
1
2
3
4
5
Subscribe to:
Comments (Atom)

