Shell Variables

11 251 1
Shell Variables

Đ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

Shell Variables Objectives Upon completion of this module, you should be able to: • Set and unset shell and environment variables for the shell • Change the PATH environment variable for the shell • Use the which command to determine which version of a command is being used • Use the whereis command to search for instances of a command on a system Discussion – How would it be useful to customize shell? Introduction 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. Although there are many shells : Bourne shell, Korn shell, C shell, Bourne Again shell…,this module focuses on bash shell (Bourne Again SHell ). Shell Variables Overview 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. 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, as discussed in Module 11, ‘‘Initialization Files,” or by exporting it on the command line. Note – See the man pages on bash for variable definitions. Local Shell Variables A user-defined variable enables you to determine both the name of the variable and its value. For example, a pathname could be assigned for the on-line dictionary used in text editing programs. By convention, shells use capital letters for shell variable names. The first command format in the following examples sets the variable based on a name and value selected by the user, while the unset command removes the variable from the current shell and subshells: Command Format VARIABLE=value unset VARIABLE Setting a Local Variable $ DT=/usr/dict $ echo $DT /usr/dict $ cd $DT $ pwd /usr/dict $ unset DT $ echo $DT $ cd $ cd $DT $ pwd /home/user2 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 above example, 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. Displaying Shell Variables Variables and their values can be displayed by typing the set command. Command Format set Displaying Variables This example displays the shell variables of user1 $ set BASH=/bin/bash BASH_ENV=/home/user1/.bashrc BASH_VERSINFO=([0]=”2” [1]=”05a” [2]=”0” [3]=”1” 4=”release” [5]=”i686-pc-linux-gnu”) BASH_VERSION=”2.05a.0(1)-release ” COLORS=/etc/DIR_COLORS COLUMNS=80 DIRSTACK=() EUID=500 GROUPS=() HISTFILE=/home/user1/.bash_history HISTFILESIZE=1000 HOME=/home/user1 HOSTNAME=user1 HOSTTYPE=i686 IFS=$’ \t\n’ INPUTRC=/etc/inputrc LANG=en_US.iso885915 LESSOPEN=’|/usr/bin/lesspipe.sh %s’ LINES=25 LOGNAME=user1 LS_COLORS= <output omitted> MATCHTYPE=i686-pc-linux-gnu MAIL=/var/spool/mail/user1 MAILCHECK=60 OPTERR=1 OPTIND=1 OSTYPE=linux-gnu PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/sbin: /usr/bin:/home/user1/bin PIPESTATUS=([0]=”0”) PPID=1038 PS1=’[\u@\h \W]\$’ PS2=’>’ PS4=’+’ PWD=/home/user1 REMOTEHOST=192.168.20.126 SHELL=/bin/bash SHELLOPTS=braceexpand:hashall:histexpand:monitor:histor y:interactive-comments:emacs SHLVL=1 SUPPORTED=en_US.iso885915:en_US:en TERM=vt100 UID=500 USER=vominh USERNAME=root _=PATH langfile=/home/vominh/.i18n mc () { mkdir -p $HOME/.mc/tmp 2>/dev/null; chmod 700 $HOME/.mc/tmp; MC=$HOME/.mc/tmp/mc-$$; /usr/bin/mc -P "$@" >"$MC"; cd "`cat $MC`"; /bin/rm -f "$MC"; unset MC } Note – In your current shell, the display maybe differ from this example. Environment Variables Your computing environment is composed of special information, such as the location of your mailbox and the type of terminal you have. The Linux system software provides several default environment variables such as PS1, HOME, LOGNAME, SHELL, and PATH. These variables have been defined by the shell to have a specific function. The values of these customizable variables can be changed to suit the user’s needs. You can temporarily change your environment variables at the command line. This affects only the current shell. When you exit the shell where the environment variable has been assigned, that environment variable is terminated or set back to its default value. Permanent changes are made by modifying the initialization files, discussed in Module 11, ‘‘Initialization Files.” Note – Any shell variable can be made available as an environment variable. Exporting Variables Exporting variables in an initialization file enables the variables to be used by the system, processes, scripts, users, and all shells. Exporting variables at the command line makes the variables available to the current shell and all of its child processes. A variable set in a subshell and exported will be available to any shells opened afterwards but will not be exported to the parent shell. It is good practice to use the echo command to check if a value for a variable already exists before setting a new one. This protects accidental overwriting of a previously set value. Command Format VARIABLE=value;export VARIABLE or export VARIABLE=value Creating Environment Variables $ LPDEST=staffp; export LPDEST $ echo $LPDEST staffp $ EXINIT=’set showmode’; export EXINIT $ echo $EXINIT set showmode $ unset LPDEST $ echo $LPDEST $ The LPDEST variable defines the default printer. EXINIT sets a last line option for vi. Note – Setting the EXINIT variable will override any settings saved in a .exrc file to customize the behavior of the vi editor. Displaying 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 LESSOPEN=|/usr/bin/lesspipe.sh %s USER=user1 LS_COLORS=no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd =40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=01;32 :*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01;32:*.bat=01;32 :*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31: *.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz =01;31:*.bz2=01;31:*.bz=01;31:*.tz=01;31:*.rpm=01;31:*.cpio= 01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:*.xpm= 01;35:*.png=01;35:*.tif=01;35: MAIL=/var/spool/mail/user1 INPUTRC=/etc/inputrc BASH_ENV=/home/user1/.bashrc LANG=en_US.iso885915 LOGNAME= user1 SHLVL=1 SHELL=/bin/bash USERNAME=root HISTSIZE=1000 TERM=vt100 HOME=/home/ user1 PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/sbin:usr/s bin:/home/user1/bin _=/usr/bin/env 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 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: $ PATH=/usr/bin:/usr/openwin/bin:/etc:. $ export PATH $ echo $PATH /usr/bin:/usr/openwin/bin:/etc:. $ PATH=$PATH:/usr/ucb $ echo $PATH /usr/bin:/usr/openwin/bin:/etc:.:/usr/ucb $ export PATH The which Command An incorrectly defined PATH variable can result in users not being able to access the right version of a command or software. Many user problems can be traced to an incorrectly defined PATH variable. 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. This can be very useful if a command is not giving the expected output. Sometimes the reason for this can be that the system is using an unexpected version of the command. If the pathname is not displayed or is incorrect, based on your requirements, modify the PATH variable accordingly. Command Format which filename Determining the PATH Location $ which vi /usr/bin/vi  The whence command, which is built into the Korn shell, does the same thing as which . The whereis Command The whereis command can be used to add appropriate directories to your PATH variable. 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 Using the whereis Command If the whereis command is not found, use /usr/ucb/whereis to run the command. $ /usr/ucb/whereis vi vi: /usr/bin/vi /usr/ucb/vi /usr/man/man1/vi.1 Exercise: Using Shell Variables Exercise objective – In this exercise, you will use the concepts taught in this module to determine the settings of local and environmental variables and create a custom variable. Tasks Complete the following steps: 1. Create a shell variable, NAME, with your name as its value. Display the value of the new variable. 2. Start another shell. Does the subshell recognize the variable NAME? Why or why not? ___________________________________________________________ 3. Exit the subshell. Display the value of the NAME variable. Is the variable set? ___________________________________________________________ 4. Make the NAME variable an environmental variable and open another subshell. Does the subshell recognize the value of NAME this time? ___________________________________________________________ 5. List all of the environment variables. Is NAME an environmental variable? ___________________________________________________________ 6. Determine how your current PATH variable is set. 7. Add the /usr/ucb directory to your path and export the variable. Workshop Labs Use what you have learned so far in this course to work through the following: 1. Making new directories many levels down in the heirarchy can require a lot of typing. Using the information presented in this module about system variables, create a subdirectory in the coffees directory. Exercise Summary Discussion – Take a few minutes to discuss what experiences, issues, or discoveries you had during the lab exercises.  Manage the discussion here based on the time allowed for this module, which was given in the “About This Course” module. If you find you do not have time to spend on discussion, then just highlight the key concepts students should have learned from the lab exercise. • Experiences  Ask students what their overall experiences with this exercise have been. You might want to go over any trouble spots or especially confusing areas at this time. • Interpretations  Ask students to interpret what they observed during any aspects of this exercise. • Conclusions  Have students articulate any conclusions they reached as a result of this exercise experience. • Applications  Explore with students how they might apply what they learned in this exercise to situations at their workplace. Exercise Solutions Complete the following steps: 1. Create a shell variable, NAME, with your name as its value. Display the value of the new variable. $ NAME=name $ echo $NAME 2. Start another Korn shell. $ ksh Does the subshell recognize the variable NAME? Why or why not? $ echo $NAME No, because the variable was only local to the shell it was created in. 3. Exit the subshell. Display the value of the NAME variable. Is the variable set? $ exit $ echo $NAME Yes, as this is the shell in which the variable was originally set. 4. Make the NAME variable an environmental variable and open another subshell. Does the subshell recognize the value of NAME this time? $ export NAME $ ksh $ echo $NAME Yes 5. List all of the environment variables. Is NAME an environmental variable? $ env Yes, NAME should be an environmental variable. 6. Determine how your current PATH variable is set. $ echo $PATH [...]... accomplish or answer the following:  Set and unset shell and environment variables for the Bourne and Korn shells  Change the PATH environment variable for the Bourne and Korn shells  Use the which command to determine which version of a command is being used  Use the whereis command to search for instances of a command on a system Think Beyond Setting variables at the command line every time you log... Beyond Setting variables at the command line every time you log in can be tedious How could you make these variable definitions a permanent part of your environment? How might a system administrator define variables for all users on a system? . unsetting shell and environment variables. Although there are many shells : Bourne shell, Korn shell, C shell, Bourne Again shell ,this module focuses on bash shell. Again SHell ). Shell Variables Overview When you first log in to the host, you are placed in a predefined shell. If you type sh (Bourne shell) , ksh (Korn shell) ,

Ngày đăng: 02/10/2013, 09:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan