Sie sind auf Seite 1von 10

Unix

1. Introduction
Course Contents | Prev : Next

UNIX is an operating system which was first developed in the 1960s, and has been under constant development ever since. By operating system, we mean the suite of programs which make the computer work. It is a stable, multi-user, multi-tasking system for servers, desktops and laptops. UNIX systems also have a graphical user interface (GUI) similar to Microsoft Windows which provides an easy to use environment. However, knowledge of UNIX is required for operations which aren't covered by a graphical program, or for when there is no windows interface available, for example, in a telnet session. There are many different versions of UNIX, although they share common similarities. The most popular varieties of UNIX are Sun Solaris, GNU/Linux, and MacOS X. Learning UNIX (in very less time) is a mammoth task. Its learning curve is steep, but once you learn it, it would help you throughout your life, even in the fields beyond the scope of this course. UNIX is quite fun too. You merely have to follow a policy "just do it". So without any delay lets start off. Before starting the real deal with command line (shell), we will first learn ways to use UNIX in our system. Installing UNIX is very easy now a days. Here are the steps by which you can use UNIX in your system. 2.Unix Commands Course Contents | Prev : Next

So, lets start with our first hands on experience with the UNIX commands. Type "ls" in the command line and press enter. You can see that "ls" command is used to list all the files in the current working directory. Username~$ ls everything.zip research-repo lock searchSuggest.php lock.c mdumpfile.txt newcomp test Username~$

The next point to be noted is, even a simple command like "ls" can have complex sub-functions. For example, type "ls -l" in command line and press enter. Username~$ ls -l total 1192 -rwxr-xr- 1 Username Owner 7182 Apr 8 16:46 a.out -rw-rw-r-- 1 Username Owner 1619 Apr 8 16:46 compress.c -rw-rw-r-- 1 Username Owner 1062 Apr 7 13:09 compress.h -rw-rw-r-- 1 Username Owner 11200 Mar 3 14:29 data1 -rw-rw-r-- 1 Username Owner 4579 Mar 4 14:33 data2 -rw-r--r-- 1 Username Owner 104108 Feb 27 16:37 data3 -rw-rw-r-- 1 Username Owner 52736 Mar 23 18:05 data4 -rw-r--r-- 1 Username Owner 933322 Jun 11 2009 data5 -rw-r--r-- 1 Username Owner 39424 Feb 27 16:37 data6 -rw-r--r-- 1 Username Owner 29850 Mar 23 14:28 data7 -rw-r--r-- 1 Username Owner 44 Apr 8 18:04 data8 Username~$ You can see that "ls -l" is a long listing format and it gives you almost all information regarding the files in the current working directory. Now lets try to dissect the output and see what each field in this output represents.

-rwxr-xr- File permissions 1 - HardLink Username Owner - Owner and User details 7182 - Size of file Apr 8 16:46 - Date/Time of last modification a.out - File name

The number 1 you are seeing at second column is Hard link. Looking more in file permissions, we can see -rw-rw-r--, now these r/w/x have very significant role in UNIX. They define file permissions and can be used to assign permissions for a particular user.

Now you have learned that simple UNIX commands can have sub-functions which be used to produce complicated output. But complications does not stop here, two commands can be used simultaneously to produce highly configured and desired result. For example try ls -l|grep Apr , this command can output all the files which were modified in April or has file name Apr (which is just a bleak possibility). You may learn more about UNIX from

Learn all the eight tutorials Unix Tutorial for Beginners

Learn part 3http://www.ida.liu.se/~TDDI05/labs/LXB%20-%20Linux%20Basics.pdf

To learn more about ls output http://en.wikipedia.org/wiki/Ls#Sample_usage

To learn more about file permissions Filesystem permissions

To learn more about pipeline http://en.wikipedia.org/wiki/Pipeline_(Unix)

There is a good reason why generally people run away from Unix (Terminal). The reason is you need to learn many commands and there is no easy way to do it. To make things easier have a UNIX command cheat-sheet with you. Here is a small UNIX cheat sheet, introducing the very basic and very essential commands for surviving in the UNIX world.

Check out http://www.tuxfiles.org/linuxhelp/linuxcommands.html

Check out http://www.pixelbeat.org/cmdline.html

These commands are just the surface of the ocean. There are many other commands also. But most important thing to keep in mind is not to cram UNIX commands but learn to use them. For now we will see a few single line commands doing wonderful tasks. Username~$ ls -l|cut -f1 -d" " total

-rwxr-xr-x -rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r--rw-r--r--rw-rw-r--rw-r--r--rw-r--r--rw-r--r--rw-r--r-Username~$ As you can see the cut command can use space as delimiter and can diplay nth line. Try yourself, "ls -l|cut -f6 -d" "". Lets see another command which will list number of lines, word count and character count in a particular file, with that it will execute a command just to print header. Username~$ printf " Lines Words Chars Filename\n" && wc compress.c Lines Words Chars Filename 65 99 1619 compress.c Username~$ This command is significant because we ran two commands side by side, this is different from pipelining two commands. We have not redirected output of one command to another command. You yourself can try many commands side by side. (Be cautious while using rm command). TIP - To see what all a Linux command can do, type "man"command. This will show you the manual of the specified command. Using UNIX commands in a better way is an art. When these commands are used in a sequential format, it forms a very powerful language called SHELL SCRIPTING. 3. Unix philosophy Course Contents | Prev : Next

The Unix philosophy is philosophical approaches to developing software based on the experience of leading developers of the Unix operating system.
o

Do one thing and do it well - Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.

Everything is file - Ease of use and security is offered by treating hardware as a file.

Small is beautiful- Smaller the code the better it is.

N text files - Text file is a universal interface. Easy to create, backup and move to another system.

Use shell scripts to increase leverage and portability - Use shell script to automate common tasks across various UNIX / Linux installations.

Chain programs together to complete complex task - Use shell pipes and filters to chain small utilities that perform one task at time.

Choose portability over efficiency.

Keep it Simple, Stupid (KISS). Course Contents | Prev : Next

4. Shell Scripting

What is a Shell Script or shell scripting? Normally shells are interactive. It means the shell will accept command from you (via keyboard) and execute them. However, if you store a sequence of commands to a text file and tell the shell to execute the text file instead of entering the commands, that

is known as a shell program or shell script. A Shell script can be defined as - "a series of command(s) stored in a plain text file". A shell script is similar to a batch file in MSDOS, but it is much more powerful compared to a batch file. Shell scripts are a fundamental part of the UNIX and Linux programming environment. Why shell scripting?
o o

Shell scripts can take input from a user or file and output them to the screen. Whenever you find yourself doing the same task over and over again you should use shell scripting, i.e., repetitive task automation. Creating your own power tools/utilities. Automating command input or entry. Customizing administrative tasks. Creating simple applications. Since scripts are well tested, the chances of errors are reduced while configuring services or system administration tasks such as adding new users.

Practical examples where shell scripting actively used


o o o o o o o o o o

Monitoring your UNIX system. Data backup and creating snapshots. Dumping Oracle or MySQL database for backup. Creating email based alert system. Find out what processes are eating up your system resources. Find out available and free memory. Find out all logged in users and what they are doing. Find out all failed login attempt, if login attempt are continue repeatedly from same network IP automatically block all those IPs accessing your network/service via firewall. User administration as per your own security policies. Find out information about local or remote servers.

Shell scripting is fun. It is useful to create nice (perhaps ugly) things in shell scripting. Here are a few examples of scripts you might use everyday:
o o o o o

Find out today's weather (useful when you are busy in a chat room). Find out what that site is running (just like netcraft). Download RSS feeds and display them as you login or in your email. Find out the name of the MP3 file you are listening to. Monitor your domain expiry date every day.

Advantages
o o o

Easy to use. Quick start, and interactive debugging. Time Saving.

o o

Sys Admin task automation. Shell scripts can execute without any additional effort on nearly any modern UNIX / Linux / BSD / Mac OS X operating system as they are written an interpreted language

Disadvantages
o o o

Compatibility problems between different platforms. Slow execution speed. A new process launched for almost every shell command executed.

With this much information, let's start off with shell scripting. The shell used for explaining the concepts here is the "Bash" shell. Bash is the shell, or command language interpreter, for the Linux operating system. So lets start, to create a shell script: 1. 2. 3. 4. 5. 6. Use a text editor such as vi. Write required UNIX commands and logic in the file. Save and close the file (exit from vi). Make the script executable. You should then of course test the script, and once satisfied with the output, move it to the production environment. The simplest program in Bash consists of a line that tells the computer a command.

Start up your favorite text editor (such as vi): "vi hello.sh" Essential vi Commands
o

Open a file: vi filename

To go into edit mode: press Esc and type i

To go into command mode: press ESC

To save a file: press ESC and type :w filename

To save a file and quit: press ESC and type :wq OR press ESC and type :x

To jump to a line: press ESC and type :the line number

To search for a string: press ECS and type /wordToSearch

To quit vi:

press ECS and type :q Save the following into a file called hello.sh: #!/bin/bash echo "Hello, World!" echo "Knowledge is power." Save and close the file. You can run the script as follows: ./hello.sh Sample outputs: bash: ./hello.sh: Permission denied Saving and Running Your Script The command ./hello.sh displayed an error message on the screen. It will not run script since you've not set execute permission for your script hello.sh. To execute this program, type the following command: Username~$ chmod +x hello.sh Username~$ ./hello.sh Sample Outputs: Hello, World! Knowledge is power. To learn more about SHELL SCRIPTING, check out 1. Linux Shell Scripting Tutorial (v 2.0)

2. Linux Shell Scripting Tutorial (v 1.05r3)

3. Shell Programming PDF

4. Unix Shell Scripting Tutorial PDF

5. Practice Quizzes

Course Contents | Prev : Next

Try out these quizzes.


o

http://networking.ringofsaturn.com/Tools/quizzes.cgi?Action=Take&Quizid=3

http://www.ch.embnet.org/CoursEMBnet/Exercises/Quiz/quix1.html

Try out the questions available in the below given sites to improve your understanding of Basic UNIX.
o

"Exams" section in http://www.cs.wichita.edu/~chang/lecture/cs497c/

http://courses.cs.vt.edu/~cs2204/fall2001/quizzes.html
Course Contents | Prev : Next

6. Assessment

Take the following assessments after completing the course. 1. Unix - Assignment1

2. Unix - Assignment2

3. Unix - Quiz1 To attend the Assignments click on "Assignment" link on site homepage To attend the Quiz click on "Tests & Quizzes" link on site homepage

Das könnte Ihnen auch gefallen