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

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

29 171 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

Nội dung

Information Technology College of HoChiMinh city Faculty of Information Technology Course: Fundamentals of Linux OS Unit 10 Shell Variables and Initialization Files Lecturer: Võ Tấn Dũng votandung@yahoo.com http://sites.google.com/site/votandungsg/ 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 section: • Local (shell) • Global (environment) • Two key environment variables are the PATH and ENV variables • This module covers setting and unsetting shell and environment variables VÕ TẤN DŨNG 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 VÕ TẤN DŨNG 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 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 Command Format VARIABLE=value unset VARIABLE • 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: VÕ TẤN DŨNG Setting a Local Variable • Example: $ DT=/usr/dict $ echo $DT /usr/dict $ cd $DT $ pwd /usr/dict $ unset DT $ echo $DT $ cd $ cd $DT $ pwd /home/user2 VÕ TẤN DŨNG 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 VÕ TẤN DŨNG Displaying Shell Variables • Variables and their values can be displayed by typing the set command Command Format set • This example displays the shell variables of user1: $ set BASH=/bin/bash BASH_ENV=/home/user1/.bashrc VÕ TẤN DŨNG Environment Variables • Environment variables and their values can be displayed by typing the env command Command Format env • Listing Environment Variables: $ env PWD=/home/user1 REMOTEHOST=192.168.20.126 HOSTNAME=user1 USER=user1 VÕ TẤN DŨNG The PATH Variable • The shell uses the PATH variable to locate commands in directories in the order specified by the PATH statement Command Format PATH= directory:directory:directory export PATH VÕ TẤN DŨNG 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 VÕ TẤN DŨNG First and Second Initial Files • a VÕ TẤN DŨNG 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 home directory • To make the customizations available to all subsequent shells, the variable must be exported • Unlike variables, commands cannot be exported If commands are used in the initialization files, they must be placed in the secondary initialization files in order to be read each time a subshell is opened VÕ TẤN DŨNG 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 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 VÕ TẤN DŨNG 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 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 VÕ TẤN DŨNG 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 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 VÕ TẤN DŨNG 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 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 VÕ TẤN DŨNG 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: % source ~/.login % source ~/.cshrc VÕ TẤN DŨNG 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 VÕ TẤN DŨNG 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 Command Format $ ENV=$HOME/.kshrc; export ENV • 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 END OF UNIT 10 • remember to your homework (see http://sites.google.com/site/votandungsg/) VÕ TẤN DŨNG ... files are executed VÕ TẤN DŨNG First and Second Initial Files • a VÕ TẤN DŨNG Predefined environment variable • When a predefined environment variable is placed in one of the system-wide initialization... subshells: VÕ TẤN DŨNG Setting a Local Variable • Example: $ DT=/usr/dict $ echo $DT /usr/dict $ cd $DT $ pwd /usr/dict $ unset DT $ echo $DT $ cd $ cd $DT $ pwd /home/user2 VÕ TẤN DŨNG echo command... has no meaning outside of the Korn shell VÕ TẤN DŨNG END OF UNIT 10 • remember to your homework (see http://sites.google.com/site/votandungsg/) VÕ TẤN DŨNG

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

TỪ KHÓA LIÊN QUAN

w