Вы находитесь на странице: 1из 3

KERNEL

Day Two

A VOYAGE TO THE
N

Part 3

ow its time to learn about the shell in a systematic way. Let us begin todays voyage by writing our first shell script. If you have not been exposed to the commands in the vi editor (my favourite editor after Emacs) you can use the following listing as a reference: To insert a new text: Esc + i To save a file: Esc + : + w To save a file with the file name (save as): Esc + : + w lename To quit the vi editor: Esc + : + q To quit without saving: Esc + : + q! To save and quit the vi editor: Esc + : + wq To search for specified words in forward direction: Esc + /word To continue with the search: n To search for a given word in the backward direction: Esc + ?word To copy the line where the cursor is placed: Esc + yy To paste the text just deleted or copied at the cursor: Esc + p To delete the entire line where the cursor is located: Esc + dd To delete the word from the cursors position: Esc + dw For beginners, I have included the screen shots of the process. Open your terminal and type the code as shown in Figure 1, and save the file using the Esc + : + w filename command (Figure 2). Now try running the script from the terminal and you will see that you get an error:
aasisvinayak@free-laptop:~$ ./first bash: ./first: Permission denied

aasisvinayak@free-laptop:~$ ./first I love Linux

It works! So you have written your first program in the shell. Please note that it is always better to save the shell scripts with the extension .sh, so that it can easily be identified. Now let us try experimenting with user defined variables (UDV). Here (just like #define in C), you will be defining your variable as shown below:
# # Script to test variables # Script written for A Voyage to the Kernel myname=Vinayak myos=ubuntu 8.04 echo My name is $myname echo My os is $myos. It is really good.

You now have an idea regarding the initial steps. Hence, for the sake of saving space, I will not include the commands that are specific to the vi editor. You may include the same wherever it is required. Well, lets try executing the code that we wrote just now:
aasisvinayak@free-laptop:~/Desktop$ /home/aasisvinayak/ Desktop/variable.sh My name is Vinayak My os is ubuntu. It is really good.

Yup! The variables are set. Now let me show you how to get the input from the keyboard and how the program interacts with the user.
#Script to read ones name from key-board #Script written for A Voyage to the Kernel echo Please type your name read name echo Hello $name, Lets be friends!

This is because you have not set the permissions for the file to be executed. You can use chmod 755 or your GUI tool to do this. Now try running it:
aasisvinayak@free-laptop:~$ chmod 755 first

102

AUgUsT 2008

LINUX For YoU

www.openITis.com

a voyage to the Kernel


Once executed, you will see that the program takes the values you entered and prints the statement.
aasisvinayak@free-laptop:~/Desktop$ /home/aasisvinayak/Desktop/ inputname.sh Please type your name Aasis Hello Aasis, Lets be friends!

Figure 1: The vi editor with some text

Now lets understand wild card characters. The following is the wild card expansion listing: ls * This command will show all files ls a* This will show all files with names starting with the letter 'a' ls *.c This is used to list files (names) that end with '.c' ls ib*.c This will list all files that begin with 'ib' and end with '.c' ls ? If you want to list files that have a single letter as the file name, you can use this ls ab? This will display files that begin with ab and have 3 letters in their name Here is an application of wild-card expansion (and it is self-explanatory):
aasisvinayak@free-laptop:~/Desktop$ ls /bin/[x-z]* /bin/zcat /bin/zdiff /bin/zfgrep /bin/zgrep /bin/zmore /bin/zcmp /bin/zegrep /bin/zforce /bin/zless /bin/znew

Figure 2: The vi editor immediately after the file is saved with the filename first

Now I will introduce you to another code that can manipulate the data a user has provided:
# # Script using echo and read command for user interaction # Script written for "A Voyage to the Kernel" # echo "Your good name please :" read name echo "Your age please :" read age newage=`expr $age + 5` echo "Hello $name, in 5 years you will be $newage years old."

Figure 3: The output when you execute the lengthy program


according users input # # while : do clear echo *************************************** echo Menu echo **************************************** echo [1] To show todays date/time echo [2] To show files in current directory echo [3] To show calendar echo [4] To start Vi editor echo [5] Exit echo ============================== echo -n Enter your choice [1-5]: read yourchoice case $yourchoice in 1) echo Today is `date` ,Press any key . . . ; read ;; 2) echo Files in `pwd` ; ls -l ; echo Press any key. . . ; read ;; 3) cal ; echo Press any key . . . ; read ;; 4) vi ;; 5) exit 0 ;; www.openITis.com

And if you have written the code well, you will get a result similar to the one shown below:
aasisvinayak@free-laptop:~$ /home/aasisvinayak/Desktop/usernameage.sh Your good name please : XYZ Your age please : 100 Hello XYZ, in 5 years you will be 105 years old.

Let's move onto a little lengthy, yet simple, program:


# # Script illustrating the way to create simple menus and take action

LINUX For YoU

AUgUsT 2008

103

a voyage to the Kernel


Figure 3 shows what the program looks like when you execute it. Tired of trying the terminal stuff? Lets have some fun (of course, at the terminal itself). Try the following code:
dialog --title Suggestion --backtitle Voyage to the Kernel \ --infobox This dialog box is just to show another utility Please send your suggestion to improve the column at aasisvinayak@gmail.com www.aasisvinayak.com 15 75 ;

Figure 4: A simple program using the dialog utility

Well, before executing this code, make sure that you have the dialog utility installed in your OS. If not, install it and then run the script. The output is shown in Figure 4. Wow! Surprised that the shell can even do such catchy stuff? Well, lets look for a way to receive an input from the user with dialog:
dialog --title Linux for You --backtitle Shell script written for A Voyage to the Kernel --msgbox Thanks for trying this script. Click Ok or press space bar/enter key 8 30

Figure 5 elucidates that your program can respond to the input provided by the user. If you wish to create a program thats a little more advanced, where the script allows the user to enter some text, try the following one:
Figure 5: An interactive dialog-based program
dialog --title For queries - post@aasisvinayak.com --backtitle Script written for A Voyage to the kernel --inputbox Please enter some text 10 75 read $name sel=$name case $sel in 0) echo $name ;; 1) echo Cancel is Press ;; 255) echo [ESCAPE] key pressed ;; esac

You will see an input box, where the user can enter the text (Figure 6). Now, are you in a mood to learn some theory? Just hold on till the next issue.
Figure 6: Another dialog-based program that lets you enter comments
*) echo Opps!!! Please select choice 1,2,3,4, or 5; echo Press any key . . . ; read ;; esac done

By: Aasis Vinayak PG. The author is a hacker and a free software activist who does programming in the open source domain. He is the developer and CEO of the Mozhi Search engine. His research work/publications are available at www.aasisvinayak.com

104

AUgUsT 2008

LINUX For YoU

www.openITis.com

Вам также может понравиться