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

Lecture 4

UNIX shells:
Why it is called shell??
Layer between the hardware and the user Command Line interface
User Shell HW

Function:
ls * ls f1 f2 f3 common in all shells differ in syntax from one shell and another

Interpreter

Shell scripts (programs)

Features

History

Auto Completion
Bash Ksh tab esc \ esc = you must be in vi mode all occurance

Redirection
Default action of the command
Command takes the standard input from the keyboard and sends the standard output to the terminal window. You can modify the default action of the standard input, standard output, and standard error within the shell by redirecting stdin, stdout, and stderr.

Input redirection:
Forces a command to read the input from a file instead of from the keyboard. Using the less than (

<) metacharacter

command < filename

Output redirection:
Sends the output from a command into a file instead of sending the output to the screen. As the command generates error messages, these messages are sent to the standard error. Usually error messages are sent to the terminal screen.

Using the greater than ( ) metacharacter to direct the standard output to a file instead of printing the output to the screen. If file does not exist, the system creates it. If the file exists, the redirection overwrites the contents of the file.

>

command > filename

>>

Append instead of overwrite

Redirecting Standard Error


No error messages appear on the screen. command 2> /dev/null Redirecting the standard output to a file and the standard error to the same file. command 1> filename 2>&1 Redirecting standard error to the file. command 2>filename

Pipe Character

If there is output will be put in the buffer


Buffer

only if required input ow wont look at it

The piping has no meaning if there is no output before or the after need no input bash-3.00$ cal | Tue Nov 10 09:46:23 EET 2009 date

-bash-3.00$ date | Tue Nov 10 10:25:45 EET 2009

cat

-bash-3.00$ cat | date Tue Nov 10 10:26:34 EET 2009 here suspend waiting for input when enter -bash-3.00$ touch f1 Both will be touched | touch f2

-bash-3.00$ touch f1 | Tue Nov 10 10:25:45 EET 2009 -bash-3.00$ date |

date

touch f2

File will be touched but no date output will be appeared -bash-3.00$ Usage error ls | rm

-bash-3.00$ /etc/passwd

ls /etc/passwd |

cat

Aliasing
alias is within the shell

To make an alias:
alias alias c=clear ls=ls l [no option] alias name view all aliases view certain alias delete all aliases delete certain alais

To display all alias:


alias alias

To delete all alias:


unalias -a unalais certain alias

To ignore alias:

\ls or /usr/bin/ls to ignore alias

Startup and initialization file

User initialization file The primary purpose of the user initialization files is to define the characteristics of a users work environment, such as the command-line prompt, the environment variables, and the windowing environment. The commands in the initialization file is executed :

At Login su username User startup file It is executed whenever you start new shell
Initialization file Startup file

No startup file

Environmental variables

The shells support two types of variables: Environment variables Variables that provide information about the users environment to every shell program that is started. Local variables Variables that affect only the current shell. Any subshell started would not have knowledge of these variables unless it is exported Environment variables

LOGNAME Define the users login name HOME Sets the path to the users home directory. It is the default argument for the cd command. SHELL Sets the path to the default shell. $ Hold the id no. of current shell 0 Hold the name of the current shell PATH Sets the default path that the shell searches to find commands. MAIL Sets the path to the users mailbox. TERM Defines the terminal. LPDEST Sets the users default printer. PWD Defines the current working directory. PS1 Defines the shell prompt for the Bourne or Korn shell.

Ex:

To set any variables:

VARIABLE = value
X=123 To export the variable:

export VARIABLE = value


Or

export VARIABLE
You can export the variable while assigning it or after you assigned it Once you export a variable it can be seen by another sub shells not another terminals once you close the terminal it will no more being exists

To view specific variable:

echo printenv

$VARIABLE VARIABLE

bash-3.00# echo $$ 1572 bash-3.00# echo $SHELL /sbin/sh bash-3.00# ps -fp $$ UID PID PPID C STIME TTY root 1572 1557 0 11:21:44 pts/3 echo $0 echo $? Assignment bash-3.00# x=123 bash-3.00# y=x bash-3.00# echo $x 123

TIME CMD 0:00 bash

To view all variables:

env printenv export set

List all environmental variable (having value) + exported local variable

List all environmental variable (having value) + exported and un exported local variable

File name globing


* : Zero or more matching ? : any character matching []: Range matching [a-c] as example

Example
ls ls /usr/bin/*sh /usr/bin/w*

Job control
Any command you run on terminal you cannot write another command unless the previous one is executed So if a command take a lot of time in execution would you wait for it or open new terminal or Run this command in back ground while your terminal control this command in back ground Ex:

sleep
-

100

The prevent command will prevent you from writing any command for 100 second Run this command in back ground as you know that it will take long time

sleep
-

100 &

If you dont add & from the beginning and then 2 numbers will be displayed the job id [as when more than one job is running in back ground they are put in stack ]and the process id [ we deal with this number in process management]

- sleep - ctl + z : - bg jobid : - fg jobid : - jobs :

100

Stop the process in the back ground Run the job in the background Run this job back to the foreground List all the jobs running with their job id

Process management
View process: Options: ps : List the info [pid- process name- consume how much from cpu ] about process running on terminal ps -f : full information about process ps -e : Prints information about every process on the system ps -u username : List the process of certain user ps -p processid : Prints information about this process [ps p $$] In order to view the process on your system use

ps

Know process id of process:

pgrep
Send signals process:

processname

Kill -l : view all the signals available Kill -signal no pid : Send the signal to process 2 23 25 15
Interrupt[ctrl+c] exit without saving can be ignored Stop running [ctrl+z] Continue running default if no signal is specified with kill exit after saving can be ignored

Terminate without saving and cannot be ignored

pkill -signal no pname : Send the signal to process by the name pkill -u username : To terminate the processes owned by this
user

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