专藏理工大学 1945 ANHUI UNIVERSITY OF SCIENCE TECHNOLOGY Linux开发基础 Development Foundation on Linux OS 方贤进,Ph.D&Associate Prof. 安徽理工大学计算机科学与工程学院 1
1 安徽理工大学 计算机科学与工程学院 Linux开发基础 Development Foundation on Linux OS 方贤进, Ph.D & Associate Prof
安藏理工大学 ANHUI UNIVERSTTY OF SCIENCE TECHNOLOGY Section 1 Shell programming on Linux OS 3
3 Section 1 Shell programming on Linux OS
安藏理工大学 ANHUI UNIVERSITY OF SCIKNCE TECHNOLOGY 1.1 What is Shell? Shell is a command interpreter; Shell is a programming language,which includes variable,keywords and all kinds of control sentences; The generic Shell on Unix OS is Bourne Shell (for short,sh) There are other kinds of shell,C-shell, Korn Shell,etc. 4
4 1.1 What is Shell? ◼ Shell is a command interpreter; ◼ Shell is a programming language, which includes variable, keywords and all kinds of control sentences; ◼The generic Shell on Unix OS is Bourne Shell (for short, sh). ◼There are other kinds of shell,C-shell, Korn Shell, etc
安藏理工大学 ANHUI UNIVERSTTY OF SCIENCE TECHNOLOGY 1.2 How to create and execute shell script? Create a shell Script using VI editor For example: $vi test Where vi is an editor,test is the file name of shell script. 5
5 1.2 How to create and execute shell script? ◼Create a shell Script using VI editor For example: $vi test Where vi is an editor, test is the file name of shell script
安藏理工大学 ANHUI UNIVERSITY OF SCIENCE TECHNOLOGY 1.2 How to create and execute shell script? To execute a shell Script 1Using input redirection.Let "sh"read commands from shell script filename.E.g. $sh test 2 Script filename is used as arguments of "sh"command.E.g.: $sh script filename [script arguments] 3 Script file is set as "execute attribute",then it can execute directly $chmod +x test $./test 6
6 1.2 How to create and execute shell script? ◼ To execute a shell Script ① Using input redirection. Let “sh” read commands from shell script filename. E.g. $sh < test ② Script filename is used as arguments of “sh” command. E,g.: $sh script filename [script arguments] ③ Script file is set as “execute attribute”, then it can execute directly $chmod +x test $./test
安藏理工大学 ANHUI UNIVERSITY OF SCIENCE TECHNOLOGY 1.3 shell中的变量 1.3.1 Environment variables ①可写的环境变量 在用户登陆的过程中执行/etc/profile文件进行初始化 ■各个用户自身的启动文件/home/username/.bash profile 可通过set命令显示当前己经设置的环境变量及其值。 7
7 1.3 shell中的变量 1.3.1 Environment variables ①可写的环境变量 ◼ 在用户登陆的过程中执行/etc/profile文件进行初始化 ◼ 各个用户自身的启动文件/home/username/.bash_profile 可通过set命令显示当前已经设置的环境变量及其值
安藏理工大学 ANHUI UNIVERSITY OF SCIKNCE TECHNOLOGY 1.3 shelli中的变量 1.3.1 Environment variables ②只读的环境变量 环境变量 含义 $0 Shell script filename $1~$9 First-ninth command line arguments $* All value of command line arguments $# The number of command line arguments $$ The current process ID $? The exit status of the last command,"O"indicates "success","1"indicates "failure" $刺 the process ID of the last background process Shift < Shift position argument 8
8 1.3 shell中的变量 1.3.1 Environment variables ②只读的环境变量 环境变量 含义 $0 Shell script filename $1~$9 First-ninth command line arguments $* All value of command line arguments $# The number of command line arguments $$ The current process ID $? The exit status of the last command, “0” indicates “success”, “1” indicates “failure” $! the process ID of the last background process Shift <> Shift position argument
安藏理工大学 ANHUI UNIVERSTTY OF SCIENCE TECHNOLOGY 1.3 shelli中的变量 1.3.2 Shell variables defined by Users ■definition: Variable name=string #delimiter may not be used ■citation √$variable name √As a part of a string #at the end of a string string $variable name #at the head of a string or in the middle of a string string1$(variable name)string2 9
9 1.3 shell中的变量 1.3.2 Shell variables defined by Users ◼definition: Variable name=string #delimiter may not be used ◼citation ✓$variable name ✓As a part of a string #at the end of a string ✓string $variable name #at the head of a string or in the middle of a string ✓string1${variable name}string2
安藏理工大学 ANHUI UNIVERSTTY OF SCIENCE TECHNOLOGY 1.4shel川中的特殊字符 1.4.1 Wildcard characters in Shell ■星号* ■问号? ■一对方括号[] 其作用是匹配该字符组所限定的任何一个字符,例如: f[abcd]可以匹配fa,fb,fc,fd f[a-d]与f[abcd]作用相同 ■感叹号 表示否定,例如fa-d].c表示以f打头、第二个字符不是a-d的.c文件名 10
10 1.4 shell中的特殊字符 1.4.1 Wildcard characters in Shell ◼ 星号* ◼问号? ◼一对方括号[ ] 其作用是匹配该字符组所限定的任何一个字符,例如: f[abcd]可以匹配fa,fb,fc,fd f[a-d]与f[abcd]作用相同 ◼感叹号 表示否定,例如f[!a-d].c表示以f打头、第二个字符不是a-d的.c文件名
安藏理工大学 ANHUI UNIVERSITY OF SCIENCE TECHNOLOGY 1.4shel‖中的特殊字符 1.4.2Shel中的引号 双引号” 由它括起来的字符,除$,`,仍保留其特殊功能外,其余字符通常作为普通 字符 ■单引号” 由它括起来的所有字符或字符串都作为普通字符出现。 ■倒引号 由此括起来的字符串被shel解释为命令行,其执行结果取代整个倒引号部 分。例如: Secho current directory is pwd 如果当前工作目录为home/wang的话则输出结果为 current directory is /home/wang 11
11 1.4 shell中的特殊字符 1.4.2 Shell中的引号 ◼ 双引号”” 由它括起来的字符,除$, `, \仍保留其特殊功能外,其余字符通常作为普通 字符 ◼单引号’’ 由它括起来的所有字符或字符串都作为普通字符出现。 ◼倒引号`` 由此括起来的字符串被shell解释为命令行,其执行结果取代整个倒引号部 分。例如: $echo current directory is `pwd` 如果当前工作目录为/home/wang的话则输出结果为 current directory is /home/wang