Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 34 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
34
Dung lượng
305 KB
Nội dung
Objectives
In this lesson, you will learn to:
•
Define the role and features of the Linux shell
•
Use theshell as a command interpreter
•
Create user-defined variables
•
Use shell environment variables
•
Create shell scripts in Linux
Introduction tothe Shell
•
A shell:
•
Is a utility program with
the Linux system that
serves as an interface
between the user and
the kernel
•
Plays an important role
of command
interpretation for the
kernel
•
The adjacent figure shows
the architecture of the Linux
operating system.
Features of a Shell
•
The shell has the following features:
•
Interactive processing
•
Background processing
•
Input/output redirection
•
Pipes
•
Shell scripts
•
Shell variables
•
Programming language constructs
•
Command history
•
Job control
•
File name completion
•
Command completion
Shell as a Command Interpreter
•
The shell:
•
Reads the command
•
Locates the file in the directories containing utilities
•
Loads the utility into memory
•
Executes the utility
Shell as a Command Interpreter
(Contd.)
The shell creates a child shell for the execution of a utility
The shell requests the kernel for any hardware interaction
Unix Shells
•
Some of the popular Unix Shells are:
•
Bourne Shell: sh is the executable filename for this shell
•
C Shell: csh is the executable filename for this shell
•
Korn Shell: The executable filename is ksh
•
Restricted Shell: Is typically used for guest logins
Linux Shells
•
Some of the popular shells available in Linux are:
•
Bash:
•
Is an acronym for ‘Bourne Again Shell’ and is the default shell for
most Linux systems
•
Uses the symbolic link sh
•
Tcsh:
•
Is an acronym for ‘Tom’s C shell’ also known as the TC shell
•
It is an enhancement of the C shell
•
Uses the symbolic link csh
•
Can be executed by using either csh or tcsh at theshell prompt
•
ASH:
•
Is usually suitable on machines that have very limited memory
•
Uses the symbolic link, bsh in Fedora Core 2
Changing the Default Shell
•
The default shell of the user is specified in the /etc/passwd file.
•
The chsh command can be used for changing the default shell for the user
[steve@linuxpc1 /etc]$ chsh ↵
Changing shell for Steve.
Password:
New shell [/bin/bash]: /bin/csh
Shell changed.
•
The full path for the new shell has to be given when changing the shell.
•
After the default shell has been changed from Bash to csh, the entry for the
user, steve, in the passwd file changes as follows
steve:x:503:513:steve walker:/home/steve:/bin/csh
Shell Scripts
•
A Shell script:
•
Stores a sequence of frequently used Linux commands in a file
•
Enables theshellto read the file and execute the commands in it
•
Allows manipulation of variables, flow-of-control and iteration
constructs that make programming possible
The echo Command
•
The echo Command
•
Displays messages on the screen
•
Displays the text, enclosed within double-quotes
•
Puts a newline character at the end of the text by default
$ echo "This is an example of the echo command" ↵
This is an example of the echo command
$ _
[...]... is lost when the script stops executing • At theshell prompt: A variable created at the prompt remains in existence until theshell is terminated Referencing Variables • • • The $ symbol is used to refer to the content of a variable variable1=${variable2} The braces are essentially used to delimit the variable name The command to assign the value of today variable to x variable is: $ x=$today Reading...Executing a Shell Script • A shell script can be executed: • In a new shell by 1 First granting the execute permission tothe specified shell script 2 Then invoking its name at the $ prompt $ chmod u+x magic ↵ [Change File Access Permission] $ magic ↵ [Execute theshell script] • In the current shell by using the dot (.) command with the script name in the Bash shell $ magic ↵ [In the Bash shells] Creating... Reading a Value into a Variable • • • • The read command is used to enter a value from the keyboard into a variable during the execution of a shell script The syntax to use the read command is: $ read The read command, on execution, waits for the user to enter a value for the variable When the user presses key after entering the value, the remaining part of theshell script, if... Contains theshell level that you are currently working in • SHELL: Stores the user’s default shellThe env Command • • The env Command enables you to view the list of all the exported environment variables and their respective values The following are some of the environment variables: • HOME: Stores the location of the home directory of a user • PATH: Contains a list of path names of directories that... debugging of shell scripts by using the following two options: • The –v option: Echoes the statements written in a shell script on the terminal, before actually executing them • The –x option: Echoes the statements in the script preceded by a + symbol, if the statement has successfully been executed To debug theshell script, you can use the sh command with the –v and –x options at theshell prompt:... conjunction with the test command if then [else ] fi Linux also provides the if elif construct, the syntax of which is as follows: if condition(s) then command(s) elif condition then command(s) else command(s) fi The exit Command • • • The exit command is used to stop execution of theshell script and return tothe $ prompt based on the result of the test command The following... returns to parent shell] The export variable is a global variable that is passed on by the export command as an argument to all child shells Environment Variables • • • All exported variables are environment variables, some of which are meaningful totheshell By changing the values of these variables, a user can customize the environment Some of the environment variables are: • HOME: Stores the location... list of path names of directories that are to be searched for an executable program • PS1: Contains theshell prompt, $ • PS2: Sets the value of the secondary prompt • LOGNAME: Contains the user’s login name • SHLVL: Contains theshell level of the current shell • SHELL: Stores user’s default shell Objectives In this lesson, you will learn to: • • • • • • • Use the grave ascent, expr command, and test... to see more files :" read reply done The until Construct • • • The evaluation pattern of the until loop construct is opposite to that of the while loop The until construct continues execution until a testing condition becomes true Therefore, the until false statement is identical tothe while true statement The for Construct • • • • The for construct takes a list of values as input, and executes the. .. fi The bash_profile File • • • • • The bash_profile file is used to set the environment variable that are not set automatically It is a special shell script that is executed as soon as the user logs in The bash_profile file contains the standard settings for the user such as, the type and path of terminal being used The presence of this file is optional If it needs to be executed, it must exist in the . Interpreter
•
The shell:
•
Reads the command
•
Locates the file in the directories containing utilities
•
Loads the utility into memory
•
Executes the utility
Shell. full path for the new shell has to be given when changing the shell.
•
After the default shell has been changed from Bash to csh, the entry for the
user,