Tutorial unix filetype pdf




















A user must have execute access to the bin directory in order to execute the ls or the cd command. Changing Permissions To change the file or the directory permissions, you use the chmod change mode command. There are two ways to use chmod — the symbolic mode and the absolute mode.

Using chmod in Symbolic Mode The easiest way for a beginner to modify file or directory permissions is to use the symbolic mode. With symbolic permissions you can add, delete, or specify the permission set you want by using the operators in the following table.

Each permission is assigned a value, as the following table shows, and the total of each set of permissions provides a number for that set. All the permissions mentioned above are also assigned based on the Owner and the Groups.

NOTE: The super user, root, has the unrestricted capability to change the ownership of any file but normal users can change the ownership of only those files that they own. Changing Group Ownership The chgrp command changes the group ownership of a file.

As a regular user, you do not have read or write access to this file for security reasons, but when you change your password, you need to have the write permission to this file. Programs that do not have the SUID bit set are run with the permissions of the user who started the program. This is the case with SGID as well. Normally, programs execute with your group permissions, but instead your group will be changed just for this program to the group owner of the program.

A capital letter S in the execute position instead of a lowercase s indicates that the execute bit is not set. An important Unix concept is the environment, which is defined by environment variables. Some are set by the system, others by you, yet others by the shell, or any program that loads another program.

A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data. These variables retain their values until we come out of the shell. When you log in to the system, the shell undergoes a phase called initialization to set up the environment. Otherwise, this file is skipped.

No error message is displayed. Your home directory is the directory that you start out in after you log in. The file. You can add as much shell customization information as you want to this file. Open it using the vi editor and check all the variables set for your environment. Setting the Terminal Type Usually, the type of terminal you are using is automatically configured by either the login or getty programs. Sometimes, the auto configuration process guesses your terminal incorrectly.

If your terminal is set incorrectly, the output of the commands might look strange, or you might not be able to interact with the shell properly. The PATH variable specifies the locations in which the shell should look for commands. You can change this variable to be anything you want. As soon as you change it, it'll be used by the shell from that point on.

There are quite a few escape sequences that can be used as value arguments for PS1; try to limit yourself to the most critical so that the prompt does not overwhelm you with information. When you issue a command that is incomplete, the shell will display a secondary prompt and wait for you to complete the command and hit Enter again.

Indicates the home directory of the current user: the default HOME argument for the cd built-in command. Indicates the search path for commands. It is a colon-separated PATH list of directories in which the shell looks for commands. Indicates the current working directory as set by the cd PWD command. Increments by one each time an instance of bash is started.

SHLVL This variable is useful for determining whether the built-in exit command ends the current session. TERM Refers to the display type. TZ Refers to Time zone. So far, we have tried to understand the Unix OS and the nature of its basic commands.

In this chapter, we will learn some important Unix utilities that can be used in our day-to-day life. Printing Files Before you print a file on a Unix system, you may want to reformat it to adjust the margins, highlight some words, and so on. Most files can also be printed without reformatting, but the raw printout may not be that appealing.

Many versions of Unix include two powerful text formatters, nroff and troff. The pr Command The pr command does minor formatting of files on the terminal screen or for a printer. For example, if you have a long list of names in a file, you can format it onscreen into two or more columns.

Once you are ready with formatting using the pr command, you can use any of these commands to print your file on the printer connected to your computer. Along with the command lpr, you can use -Num for the same. Here printer is the printer name. The lpstat and lpq Commands The lpstat command shows what's in the printer queue: request IDs, owners, file sizes, when the jobs were sent for printing, and the status of the requests. Use lpstat -o if you want to see all output requests other than just your own.

If the printer is disabled or running out of paper, you may see different messages on this first line. The lprm command terminates all lpr requests. You can specify either the ID of the request displayed by lp or lpq or the name of the printer. Sending Email You use the Unix mail command to send and receive mail.

List should be a comma- separated list of names. To stop, simply type dot. You can connect two commands together so that the output from one program becomes the input of the next program.

Two or more commands connected in this way form a pipe. To make a pipe, put a vertical bar on the command line between two commands. When a program takes its input from another program, it performs some operation on that input, and writes the result to the standard output.

It is referred to as a filter. The grep Command The grep command searches a file or files for lines that have a certain pattern. The simplest use of grep is to look for a pattern consisting of a single word.

It can be used in a pipe so that only those lines of the input files containing a given string are sent to the standard output. Let us now use a regular expression that tells grep to find lines with "carol", followed by zero or other characters abbreviated in a regular expression as ". There are many options that control the sorting — Option Description -n Sorts numerically example: 10 will sort after 2 , ignores blanks and tabs.

More than two commands may be linked up into a pipe. Taking a previous pipe example using grep, we can further sort the files modified in August by the order of size. The pg and more Commands A long output can normally be zipped by you on the screen, but if you run text through more or use the pg command as a filter; the display stops once the screen is full of text. Let's assume that you have a long directory listing. At the bottom of the screen is the more prompt, where you can type a command to move through the sorted text.

Once you're done with this screen, you can use any of the commands listed in the discussion of the more program. When you execute a program on your Unix system, the system creates a special environment for that program. This environment contains everything needed for the system to run the program as if no other program were running on the system. Whenever you issue a command in Unix, it creates, or starts, a new process. When you tried out the ls command to list the directory contents, you started a process.

A process, in simple terms, is an instance of a running program. The operating system tracks processes through a five-digit ID number known as the pid or the process ID. Each process in the system has a unique pid.

Pids eventually repeat because all the possible numbers are used up and the next pid rolls or starts over. At any point of time, no two processes with the same pid exist in the system because it is the pid that Unix uses to track each process. It gets its input from the keyboard and sends its output to the screen. You can see this happen with the ls command. While a program is running in the foreground and is time-consuming, no other commands can be run start any other processes because the prompt would not be available until the program finishes processing and comes out.

If the background process requires any keyboard input, it waits. The advantage of running a process in the background is that you can run other commands; you do not have to wait until it completes to start another! That first line contains information about the background process - the job number and the process ID.

You need to know the job number to manipulate it between the background and the foreground. The second is a prompt for another command. This works when the process is running in the foreground mode. If a process is running in the background, you should get its Job ID using the ps command. Each user process in the system has a parent process.

Most of the commands that you run have the shell as their parent. Check the ps -f example where this command listed both the process ID and the parent process ID.

Then the parent can do some other task or restart a new child as needed. However, sometimes the parent process is killed before its child is killed. In some cases, these processes are called orphan processes. When a process is killed, a ps listing may still show the process with a Z state.

This is a zombie or defunct process. The process is dead and not being used. These processes are different from the orphan processes. They have completed execution but still find an entry in the process table. A daemon has no controlling terminal.

If you do a "ps -ef" and look at the tty field, all daemons will have a? To be precise, a daemon is a process that runs in the background, usually waiting for something to happen that it is capable of working with. For example, a printer daemon waiting for print commands. The top Command The top command is a very useful tool for quickly showing processes sorted by various criteria. It is an interactive diagnostic tool that updates frequently and shows information about physical and virtual memory, CPU usage, load averages, and your busy processes.

This number is different from the process ID and is used because it is shorter. In addition, a job can consist of multiple processes running in a series or at the same time, in parallel. Using the job ID is easier than tracking individual processes. When you work in a distributed environment, you need to communicate with remote users and you also need to access remote Unix machines. There are several Unix utilities that help users compute in a networked, distributed environment.

This chapter lists a few of them. The ping Utility The ping command sends an echo request to a host available on the network. Using this command, you can check if your remote host is responding well or not. This utility helps you upload and download your file from one computer to another computer. The ftp utility has its own set of Unix-like commands. Once you are authenticated, you can access the home directory of the login account and you would be able to perform various commands.

Uploads more than one file from the local machine to the remote mput file list machine. Downloads more than one file from the remote machine to the local mget file list machine.

By default, you will receive a prompt to upload prompt off or download files using mput or mget commands. Lists all the files available in the current directory of the remote dir machine.

It should be noted that all the files would be downloaded or uploaded to or from the current directories. If you want to upload your files in a particular directory, you need to first change to that directory and then upload the required files. Password: User amrood logged in. Telnet is a utility that allows a computer user at one site to make a connection, login and then conduct work on a computer at another site.

Once you login using Telnet, you can perform all the activities on your remotely connected machine. Connected to amrood. The host can be either local or remote. Finger may be disabled on other systems for security reasons. No Plan. There are many ways to edit files in Unix. Editing files using the screen-oriented text editor vi is one of the best ways.

This editor enables you to edit lines in context with other lines in the file. An improved version of the vi editor which is called the VIM has also been made available now. You can use the vi editor to edit an existing file or to create a new file from scratch. You can also use this editor to just read a text file. A tilde represents an unused line.

If a line does not begin with a tilde and appears to be blank, there is a space, tab, newline, or some other non-viewable character present. You now have one open file to start working on. Before proceeding further, let us understand a few important concepts.

In this mode, whatever you type is interpreted as a command. Everything that's typed in this mode is interpreted as input and placed in the file. To enter text, you must be in the insert mode for which simply type i. To come out of the insert mode, press the Esc key, which will take you back to the command mode. You open a file using the vi editor. Start by typing some characters and then come to the command mode to understand the difference.

Getting Out of vi The command to quit out of vi is :q. Once in the command mode, type colon, and 'q', followed by return. If your file has been modified in any way, the editor will warn you of this, and not let you quit. To ignore this message, the command to quit out of vi without saving is :q!. This lets you exit vi without saving any of the changes. You can combine the above command with the quit command, or use :wq and return. The easiest way to save your changes and exit vi is with the ZZ command.

When you are in the command mode, type ZZ. The ZZ command works the same way as the :wq command. For example, if you wanted to save the file you were working on as another filename called filename2, you would type :w filename2 and return. Moving within a File To move around within a file without affecting your text, you must be in the command mode press Esc twice. You need to pay attention to capitalization when using the commands. For example, 2j moves the cursor two lines down the cursor location.

There are many other ways to move within a file in vi. Remember that you must be in the command mode press Esc twice. For example, 2x deletes two characters under the cursor location and 2dd deletes two lines the cursor is on. It is recommended that the commands are practiced before we proceed further. Change Commands You also have the capability to change characters, words, or lines in vi without deleting them. Here are the relevant commands — Command Description cc Removes the contents of the line, leaving you in insert mode.

Changes the word the cursor is on from the cursor to the lowercase w cw end of the word. Replaces the character under the cursor. Overwrites multiple characters beginning with the character currently R under the cursor. You must use Esc to stop the overwriting. Replaces the current character with the character you type. Afterward, s you are left in the insert mode. Deletes the line the cursor is on and replaces it with the new text. S After the new text is entered, vi remains in the insert mode.

Copies the current word from the character the lowercase w cursor is yw on, until the end of the word. P Puts the yanked text before the cursor. A count of j commands join many lines. U Restores the current line to the state it was in before the cursor entered the line. Typing 'u' again will re-do the change. J Joins the current line with the next one. A count joins that many lines. When you start these commands, the command just typed will be shown on the last line of the screen, where you type the particular string to look for.

The n and N commands repeat the previous search command in the same or the opposite direction, respectively. Some characters have special meanings. Matches a single character. The character search searches within one line to find a character entered after the command. The f and F commands search for a character on the current line only.

The t and T commands search for a character on the current line only, but for t, the cursor moves to the position before the character, and T searches the line backwards to the position after the character.

Set Commands You can change the look and feel of your vi screen using the following :set commands. Once you are in the command mode, type :set followed by any of the following commands.

Command Description :set ic Ignores the case when searching :set ai Sets autoindent :set noai Unsets autoindent :set nu Displays lines with line numbers on the left side Sets the width of a software tabstop. To run a command, you only need to go to the command mode and type :! For example, if you want to check whether a file exists before you try to save your file with that filename, you can type :!

You can press any key or the command's escape sequence to return to your vi session. The result of this command is that all occurrences on the cursor's line are changed.

Press Esc twice at any time to ensure that you are in command mode. These are case-sensitive. Unix A Shell provides you with an interface to the Unix system. It gathers input from you and executes programs based on that input. When a program finishes executing, it displays that program's output. Shell is an environment in which we can run our commands, programs, and shell scripts. There are different flavors of a shell, just as there are different flavors of operating systems.

Each flavor of shell has its own set of recognized commands and functions. While the prompt is displayed, you can type a command. Shell reads your input after you press Enter. It determines the command you want executed by looking at the first word of your input. A word is an unbroken set of characters. Spaces and tabs separate words.

Bourne shell was the first shell to appear on Unix systems, thus it is referred to as "the shell". For this reason, it is the shell of choice for writing scripts that can be used on different versions of Unix. In this chapter, we are going to cover most of the Shell concepts that are based on the Borne Shell. Shell Scripts The basic concept of a shell script is a list of commands, which are listed in the order of execution.

A good shell script will have comments, preceded by sign, describing the steps. There are conditional tests, such as value A is greater than value B, loops allowing us to go through massive amounts of data, files to read and store data, and variables to read and store data, and the script may include functions.

We are going to write many scripts in the next sections. It would be a simple text file in which we would put all our commands and several other required constructs that tell the shell environment what to do and when to do it. Shell scripts and functions are both interpreted. This means they are not compiled. Example Script Assume we create a test. Note all the scripts would have the. Before you add anything else to your script, you need to alert the system that a shell script is being started.

This is done using the shebang construct. It's called a shebang because the symbol is called a hash, and the! Of course, most scripts are more complex than the above one. The shell is, after all, a real programming language, complete with variables, control structures, and so forth. No matter how complicated a script gets, it is still just a list of commands executed sequentially.

A variable is nothing more than a pointer to the actual data. The shell enables you to create, assign, and delete variables. The reason you cannot use other characters such as! Variables of this type are called scalar variables.

A scalar variable can hold only one value at a time. Shell enables you to store any value you want in a variable. After a variable is marked read-only, its value cannot be changed.

Once you unset a variable, you cannot access the stored value in the variable. Here is a simple example that demonstrates how the command works —! You cannot use the unset command to unset variables that are marked readonly. It is not available to programs that are started by the shell. They are set at the command prompt.

Some programs need environment variables in order to function correctly. Usually, a shell script defines only those environment variables that are needed by the programs that it runs. Some of these variables are environment variables whereas others are local variables. In one of our previous chapters, we understood how to be careful when we use certain non- alphanumeric characters in variable names. This is because those characters are used in the names of special Unix variables.

These variables are reserved for specific functions. These variables correspond to the arguments with which a script was invoked. All the arguments are double quoted. All the arguments are individually double quoted.

The exit status of the last command executed. The process number of the current shell. The process number of the last background command. Both the parameters specify the command-line arguments. Exit status is a numerical value returned by every command upon its completion.

As a rule, most commands return an exit status of 0 if they were successful, and 1 if they were unsuccessful. Some commands return additional exit statuses for particular reasons.

For example, some commands differentiate between kinds of errors and will return various exit values depending on the specific type of failure. A shell variable is capable enough to hold a single value. These variables are called scalar variables. Shell supports a different type of variable called an array variable. This can hold multiple values at the same time.

Arrays provide a method of grouping a set of variables. Instead of creating a new name for each variable that is required, you can use a single array variable that stores all the other variables. All the naming rules discussed for Shell Variables would be applicable while naming arrays. Defining Array Values The difference between an array variable and a scalar variable can be explained as follows.

Suppose you are trying to represent the names of various students as a set of variables. Following is the simplest method of creating an array variable. This helps assign a value to one of its indices. Following is an example to understand the concept —! Following example will help you understand the concept —!

We will discuss in detail about Bourne shell default shell in this chapter. Arithmetic Operators The following arithmetic operators are supported by Bourne Shell. Not Equality - Compares two numbers, if! All the arithmetical calculations are done using long integers. Unix - Shell Arithmetic Operators Example Here is an example which uses all the arithmetic operators —!

Relational Operators Bourne Shell supports the following relational operators that are specific to numeric values. These operators do not work for string values unless their value is numeric.

For example, following operators will work to check a relation between 10 and 20 as well as in between "10" and "20" but not in between "ten" and "twenty". It is very important to understand that all the conditional expressions should be placed inside square braces with spaces around them.

Unix - Shell Relational Operators Example Here is an example which uses all the relational operators —! This inverts a true condition into! This is logical OR. This is logical AND. String Operators The following string operators are supported by Bourne Shell. File Test Operators We have a few operators that can be used to test various properties associated with a Unix file. Here most of the operators are very similar to what we have in C Programming language.

Operator Description -r file Checks if file is readable; if yes, then the condition becomes true. Most of the operators are very similar to what we have in the C Programming language. While writing a shell script, there may be a situation when you need to adopt one path out of the given two paths.

So you need to make use of conditional statements that allow your program to make correct decisions and perform the right actions. Unix Shell supports conditional statements which are used to perform different actions based on different conditions. Syntax if [ expression ] then Statement s to be executed if expression is true fi The Shell expression is evaluated in the above syntax. If the resulting value is true, given statement s are executed. If the expression is false then no statement would be executed.

Most of the times, comparison operators are used for making decisions. It is recommended to be careful with the spaces between braces and expression. No space produces a syntax error. If expression is a shell command, then it will be assumed true if it returns 0 after execution. If it is a Boolean expression, then it would be true if it returns true. Syntax if [ expression ] then Statement s to be executed if expression is true else Statement s to be executed if expression is not true fi The Shell expression is evaluated in the above syntax.

If the expression is false, then no statement will be executed. Here statement s are executed based on the true condition, if none of the condition is true then else block is executed. The case However, this is not always the best solution, especially when all of the branches depend on the value of a single variable.

Unix Shell supports case Shell supports case Syntax The basic syntax of the case The interpreter checks each case against the value of the expression until a match is found. If nothing matches, a default condition will be used.

The statement s following the matching pattern executes. If no matches are found, the case statement exits without performing any action. There is no maximum number of patterns, but the minimum is one. When statement s part executes, the command ;; indicates that the program flow should jump to the end of the entire case statement. This is similar to break in the C programming language. A loop is a powerful programming tool that enables you to execute a set of commands repeatedly.

It is usually used when you need to manipulate the value of a variable repeatedly. Syntax while command do Statement s to be executed if command is true done Here the Shell command is evaluated. If command is false then no statement will be executed and the program will jump to the next line after the done statement. If the value of a is less than 10, this test condition has an exit status of 0. In this case, the current value of a is displayed and later a is incremented by 1. Unix Shell - The for Loop The for loop operates on lists of items.

It repeats a set of commands for every item in a list. Syntax for var in word1 word Each time the for loop executes, the value of the variable var is set to the next word in the list of words, word1 to wordN.

Sometimes you need to execute a set of commands until a condition is true. If the resulting value is false, given statement s are executed. For example, to create a file called list1 containing a list of fruit, type. Using the above method, create another file called list2 containing the following fruit: orange, plum, mango, grapefruit.

Read the contents of list2. So to add more items to the file list1 , type. You should now have two files. One contains six fruit, the other contains four fruit. We will now use the cat command to join concatenate list1 and list2 into a new file called biglist.

What this is doing is reading the contents of list1 and list2 in turn, then outputting the text to the file biglist. For example, to sort the list of fruit, type.



0コメント

  • 1000 / 1000