Bài 3: Objectives
lượt xem 8
download

Bài 3: Objectives

In this lesson, you will learn to: • Define the role and features of the Linux shell • Use the shell as a command interpreter • Create userdefined variables • Use shell environment variables • Create shell scripts in Linux Define the role and features of the Linux shell Use the shell as a command interpreter Create userdefined variables Use shell environment variables
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Bài 3: Objectives
- Objectives In this lesson, you will learn to: • Define the role and features of the Linux shell • Use the shell as a command interpreter • Create userdefined variables • Use shell environment variables • Create shell scripts in Linux
- Introduction to the 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 the shell 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 the shell to read the file and execute the commands in it • Allows manipulation of variables, flowofcontrol and iteration constructs that make programming possible
- The echo Command • The echo Command • Displays messages on the screen • Displays the text, enclosed within doublequotes • 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 $ _
- Executing a Shell Script • A shell script can be executed: • In a new shell by 1. First granting the execute permission to the specified shell script 2. Then invoking its name at the $ prompt $ chmod u+x magic ↵ [Change File Access Permission] $ magic ↵ [Execute the shell script] • In the current shell by using the dot (.) command with the script name in the Bash shell $ . magic ↵ [In the Bash shells]
- Creating Variables • Variables in shell scripts: • are not declared as integers or characters • are treated as character strings • can be mathematically manipulated • do not have to be explicitly declared • can be created at any point of time by a simple assignment of value • The syntax for creating a variable is: = • Variables can be created: • In shell scripts: A variable created within a shell script is lost when the script stops executing. • At the shell prompt: A variable created at the prompt remains in existence until the shell 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 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 the shell script, if any, is executed.
- Local and Global Shell Variables • A local variable is a variable that can be given a different value in the child shell without the parent shell knowing about it, as shown in the following example: $ continent=Africa $ echo "$continent" Africa $ bash [Creates a new shell] $ echo "$continent" [There is no response] $ continent=Asia [Gives new value Asia to continent] $ echo "$continent" Asia Press d $ exit [Displays exit and 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 to the shell. • By changing the values of these variables, a user can customize the environment. • Some of the environment variables are: • HOME: Stores the location of the HOME directory of a user • PATH: Contains a list of colondelimited path names of directories that are to be searched for an executable program • PS1: Contains the shell prompt, the $ symbol. You can change the shell prompt by setting the value of this variable to the desired prompt • PS2: Sets a value for the secondary prompt, which is by default > • LOGNAME: Contains the user’s login name • SHLVL: Contains the shell level that you are currently working in • SHELL: Stores the user’s default shell
- The 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 are to be searched for an executable program • PS1: Contains the shell prompt, $ • PS2: Sets the value of the secondary prompt • LOGNAME: Contains the user’s login name • SHLVL: Contains the shell 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 command • Use conditional execution constructs • Implement iteration constructs • Use functions in shell scripts • Debug shell scripts • Handle parameters in shell scripts using positional parameters • Implement the shift command
- Command Substitution • Command substitution allows you to use more than one command in a single command line. • The expr command is used to evaluate arithmetic expressions as shown below: $ expr 4 + 5 ↵ • The test and [] Command • Evaluates an expression and returns either a true (0) or a false (1) • Can also be replaced with [] • Uses the following syntax: test expression or [ expression ] • Enables you to test multiple conditions in one command using the options -a and -o • When using the wildcard characters the * is used, it should be preceded by a backslash (\), otherwise, the shell will interpret it as a wildcard character.
- The if Construct • Linux provides the if Construct to perform decision making in shell scripts. • The if construct is usually used in 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
CÓ THỂ BẠN MUỐN DOWNLOAD
-
Thực hành Hệ điều hành Mạng – Linux - Bài 3a
10 p |
138 |
48
-
Thực hành Hệ điều hành Mạng – Linux - Bài 3b
4 p |
121 |
47
-
Bài giảng Kỹ thuật phần mềm- Bài 3: Tiến trình phần mềm
0 p |
118 |
44
-
Giáo trình hệ điều hành - Bài 3
32 p |
66 |
19
-
Bài 3: Giám sát vận hành của máy chủ
0 p |
49 |
18
-
LẬP TRÌNH WINDOWS-bài 3
106 p |
47 |
16
-
Bài giảng Hệ điều hành Windows Sever 2003: Bài 3 - Dương Thị Thu Hiền
24 p |
33 |
8
-
Learning Cocoa with Objective-C
461 p |
26 |
7
-
Bài giảng Assembler: Bài 3 - GV.ThS. Phạm Nguyên Văn
36 p |
37 |
7
-
Bài 3: Hướng dẫn sử dụng Windows explorer - Phạm Quốc Tú
5 p |
26 |
4
-
Bài giảng Hệ điều hành Unix /Linux: Bài 3 - Đặng Ngọc Cường
50 p |
23 |
4
-
Bài giảng Bộ môn Công nghệ phần mềm - Bài 3: Kiến trúc phần mềm
27 p |
9 |
3
-
Bài giảng Nguyên lý hệ điều hành: Chương 3 - Phạm Quang Dũng
8 p |
6 |
3
-
Bài giảng Hệ điều hành Linux - Bài 3: Hệ thống tập tin & thiết bị
39 p |
10 |
2
-
Bài giảng Hệ điều hành - Bài 3: Điều phối CPU
62 p |
13 |
2
-
Bài giảng Linux và phần mềm mã nguồn mở: Bài 3 - Trương Xuân Nam
26 p |
4 |
2
-
Bài giảng Hệ điều hành Linux - Bài 3: Một số tiện ích trong Linux
24 p |
8 |
1