1. Trang chủ
  2. » Thể loại khác

Tài Liệu - Võ Tấn Dũng (votandung) Unit10 Linux ITC

29 172 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 29
Dung lượng 2,77 MB

Nội dung

Shell Variables and Initialization Files Lecturer: Võ Tấn Dũng votandung@yahoo.com Information Technology College of HoChiMinh city Faculty of Information Technology Course: Fundamen

Trang 1

Shell Variables and

Initialization Files

Lecturer: Võ Tấn Dũng

votandung@yahoo.com

Information Technology College of HoChiMinh city

Faculty of Information Technology

Course: Fundamentals of Linux OS

Unit 10

Trang 2

Introduction of Shell Variables

• A variable is a placeholder for information to be used by the

system or user Information such as the default printer or a

pathname to a directory can be set up as a variable

• Two categories of variables are discussed in the following

Trang 3

Shell Variables

• When you first log in to the host, you are placed in a

predefined shell

• If you type sh (Bourne shell), ksh (Korn shell), or csh (C

shell) on the command line, a subshell is created

• This process can be repeated to create additional shells To change to the previous shell, type exit

• Shell variables (local and global) can be either user-defined

or built-in, and can be customized by the user or predefined

by the system

Trang 4

Local shell variables

• Initially, when a variable is created, it is only available to its

shell of origin This is a local variable

• If a new subshell is created, the variables created in the parent shell are not available

• However, the parent shell is still running, and when the

subshell is exited, the variables will be available again

• When the shell where the variables were created is exited, the

variables of that shell are terminated

• Local variables are available only to the specific shell where

they are created

• To make a local variable available in all subshells, it must be

exported, either by adding it to an initialization file or by

exporting it on the command line

VÕ TẤN DŨNG

Trang 5

Local Shell Variables (cont.)

• A user-defined variable enables you to determine both the

name of the variable and its value

• By convention, shells use capital letters for shell variable

names

• The first command format sets the variable based on a name

and value selected by the user

• The unset command removes the variable from the current

shell and subshells:

Command Format

VARIABLE=value unset VARIABLE

Trang 6

Setting a Local Variable

Trang 7

echo command

• The echo command simply echoes back to the screen

whatever is passed to it as an argument

• The dollar sign ($) metacharacter preceding a variable

name enables the system to use the value of the variable

and not the name of the variable

• In the previous slide, the echo command with $DT displays

the value of DT to the screen

• Since this is a local variable, if a new subshell is opened,

the variable DT is not available

Trang 8

Displaying Shell Variables

• Variables and their values can be displayed by typing the set

<output omitted>

Trang 9

Environment Variables

• Environment variables and their values can be displayed by

typing the env command

• Listing Environment Variables:

Command Format

env

$ env

PWD=/home/user1 REMOTEHOST=192.168.20.126 HOSTNAME=user1

USER=user1

<output omitted>

Trang 10

The PATH Variable

• The shell uses the PATH variable to locate commands in

directories in the order specified by the PATH statement

VÕ TẤN DŨNG

Command Format

PATH= directory:directory:directory

export PATH

Trang 11

Setting the PATH Variable

• The dot (.) in the PATH variable enables the system to search

the current working directory for commands

• You can also add to the existing path by typing the following:

Trang 12

which Command

• The which command displays the pathname leading to an

accessible command based on your search path

• The output will be the name of the first directory in the PATH

variable that contains the command you are looking for

Trang 13

whereis Command

• Unlike which, which searches the directories found in your

PATH statement, the whereis command searches all of the

directories on the system that usually have executable files

located in them

• The whereis command will display all directories where the

command is located, including the appropriate man page

Command Format

whereis filename

Trang 14

Using the whereis Command

• If the whereis command is not found, use /usr/ucb/whereis to

run the command

VÕ TẤN DŨNG

$ /usr/ucb/whereis vi

vi: /usr/bin/vi /usr/ucb/vi /usr/man/man1/vi.1

Trang 15

Initialization files

• Initialization files contain a series of commands and variable

settings that are executed when a shell is started These files

customize the environment for the system and the user

• There are two levels of initialization files The first level is

system-wide System initialization files are maintained by a

system administrator and reside in the /etc directory

• The second level is user-specific initialization files that

reside in a user’s home directory

• When you log in to your system, the system and user’s

initialization files for the appropriate shell are read, and the

tasks defined in the initialization files are executed

Trang 16

First and Second Initial Files

• a

VÕ TẤN DŨNG

Trang 17

Predefined environment variable

• When a predefined environment variable is placed in one of the

system-wide initialization files, it is recognized globally in any

shell or subshell

• Users can customize many of these variables for their own

environment by placing them in the initialization files in their

Trang 18

Roles of the /etc/profile Script

• When a user logs in, the system reads the /etc/profile file

first, and then it reads the user’s ~/.bash_profile file

• This means that the user’s preferences for variable settings can override by the default settings that appear in the

/etc/profile file

VÕ TẤN DŨNG

Trang 19

User-Specific Initialization Files

• The user-specific initialization file(s) are stored in the home

directory of the user These initialization files can perform all

initial necessary tasks

• The initialization files provide great flexibility to the user for

customizing their environment Generally, these files are set

up as templates by the system administrator, and then

modified by the user

• When you first log in to the system, you are placed in the

default shell as defined by the /etc/passwd entry for your

account

Trang 20

Initialization Files Read Process

• The initialization files are read as shown in the following

table The bash_profile, profile,.login, bashrc,.cshrc,.kshrc files are assumed to be in the user’s home directory

VÕ TẤN DŨNG

Trang 21

Shells and Initialization Files

• The login shell is assigned by the system administrator as part

of creating a user account The login shell determines which

initialization files are read during login

• Bourne Again Shell

• Bourne Shell

• Korn Shell

• C Shell

Trang 22

Bourne Again Shell

and Initialization Files

• The Bourne Again shell uses bash_profile, bashrc files to set

the user’s environment

• When you log in to the system, the bash_profile file is read

Then, the bashrc file is read

• The bash_profile file is only read once, while the bashrc file is read every time a new bash shell is opened

VÕ TẤN DŨNG

Trang 23

Bourne Shell

and Initialization Files

• The Bourne shell uses profile, a user-specific initialization

file to set the user’s environment

• The profile file is only read once during login

Trang 24

Korn Shell

and Initialization Files

• The Korn shell uses two user-specific environment files to set

the user’s environment:

.profile kshrc

• When you log in to the system, the profile file is read

• Then, if the ENV variable in the profile file is assigned and

exported, the kshrc file is read

• The profile file is only read once, while the kshrc file is read

every time a new Korn shell is opened

VÕ TẤN DŨNG

Trang 25

Korn Shell

and Initialization Files (cont.)

• Many Korn shell commands cannot be executed by the Bourne shell and therefore should not be placed in the profile file

These Korn shell specific commands and features should be

placed in the kshrc file

• The contents of the kshrc file can include: a customized

prompt, custom variables, aliases

• To have the system reread the kshrc or profile file after

changes have been made, the user can either log out and log

back in, or type the following from the command line:

$ ~/.kshrc

$ ~/.profile

Trang 26

C Shell

and Initialization Files

• The C shell uses two user-specific environment files to set the

user’s environment: login and cshrc

• Both files are located in the user’s home directory The login

file is read only when a user logs in to the system, whereas the cshrc file is read each time a user invokes the C shell

• To have the system reread the login or cshrc file after

changes have been made, the user can either log out and log

back in, or type the following from the command line:

VÕ TẤN DŨNG

% source ~/.login

% source ~/.cshrc

Trang 27

The ENV Environment Variable

• The ENV variable defines the path to the kshrc file

• This variable must be stored in the profile file to inform the

system that the.kshrc file exists and is to be read when

creating a Korn shell

Command Format

ENV=$HOME/filename; export ENV

Trang 28

Setting the kshrc Variable

• This command sets the environment variable to point to the

.kshrc file in the $HOME directory

• HOME is a variable that is defined by the system to be the

absolute path to the user’s login directory

• When the ENV variable is placed in the profile file and read by other shells (such as the Bourne shell), it does not act as a

pointer to the kshrc file This variable has no meaning outside

of the Korn shell

VÕ TẤN DŨNG

Command Format

$ ENV=$HOME/.kshrc; export ENV

Trang 29

END OF UNIT 10

• remember to do your homework

(see http://sites.google.com/site/votandungsg/)

Ngày đăng: 15/12/2017, 16:54

TỪ KHÓA LIÊN QUAN

w