第 4 章 Linux shell 程序设计 shell UNIX/Linux Linux shell shell if case while for shell Linux Linux 本章主要介绍 Linux shell( 默认是 bash) 的语法结构 变量定义及赋值引用 标点符号 控制语句等

Similar documents
投影片 1

ebook35-2

Microsoft Word - linux命令及建议.doc

像 客 样 使 命令行 徐 东

第零章、為何要學作業系統

本文由筱驀釹贡献

PowerPoint 演示文稿

Linux操作系统使用

Shell

Microsoft Word - PS2_linux_guide_cn.doc

untitled

ebook15-C

ebook70-22

292 shell shell C shell ~/.cshrc shell ~/.login ~/.logout ~/.history.cshrc shell shell /etc/passwd ~name NIS NIS+ * 0? [abc...] a-za-z0-9

lect03.ppt

Unix®t Œ fi z.PDF

2004 Sun Microsystems, Inc Network Circle, Santa Clara, CA U.S.A. Sun Sun Berkeley BSD UNIX X/Open Company, Ltd. / SunSun MicrosystemsSun

Bourne Shell及shell编程

ebook70-21

Linux Ubuntu Part Linux Ubuntu Linux UNIX...19 Linux...19 Linux Linux...21 Linux GNU FSF Open So urce.

Linux服务器构建与运维管理

腰部酸痛保健法

CC213

Cygwin & vim

ebook70-11

Microsoft Word - 第5章.doc

ebook70-5

ebook8-30

ebook62-1

linux_for_campus

Linux 操作系统课程社区创作

2005 Sun Microsystems, Inc Network Circle, Santa Clara, CA U.S.A. Sun Sun Berkeley BSD UNIX X/Open Company, Ltd. / Sun Sun Microsystems Su

Sun Fire V440 Server Administration Guide - zh_TW

华恒家庭网关方案

C/C++ - 文件IO

untitled

2

Solaris ( ) Solaris Shell awk gawk vi emacs OK mode 1

山东2014第四季新教材《会计基础》冲刺卷第二套

mannal

序 文 藝 社 (Society of Chinese Literature) 在 去 年 暑 假 開 始 籌 措 成 立, 二 零 一 三 年 九 月 正 式 展 開 所 有 活 動 文 藝 社 成 立 的 目 的 旨 在 讓 學 生 學 習 欣 賞 歷 代 名 篇 名 作, 其 中 以 古 典

自由軟體教學平台

ebook 99-1

2004 Sun Microsystems, Inc Network Circle, Santa Clara, CA U.S.A. Sun Sun Berkeley BSD University of California UNIX X/Open Company, Ltd.

Microsoft PowerPoint - 第7章 Linux基本命令

epub 73-5

自由軟體教學平台

CCNA 3 Module 5 Switching Concepts

Sun StorEdge 3000 系列安装、操作和维护手册 (3310)

Microsoft Word - 第5-7章

2005 Sun Microsystems, Inc Network Circle, Santa Clara, CA U.S.A. Sun Sun Berkeley BSD UNIX X/Open Company, Ltd. / Sun Sun Microsystems Su

自由軟體教學平台

C/C++ - 字符输入输出和字符确认

Progperl.PDF

jsj0.nps

Linux 目 录 结 构 与 Windows 将 硬 盘 看 做 C 盘 D 盘 几 个 独 立 的 分 区 不 同,Linux 将 整 个 文 件 系 统 看 做 一 棵 树, 这 棵 树 的 树 根 叫 做 根 目 录, 用 / 表 示 各 个 分 区 通 过 挂 载 (mount) 到 文


PowerPoint Presentation

内容目录 Linux Mint 简介...3 历史...3 特点...3 版本号和开发代号...3 如何获取帮助...4 安装 Linux Mint...4 下载 iso 文件...4 校验 MD5 值...4 刻录 iso 文件...5 引导 Mint 启动...5 安装 Mint...5 Min

中兴Embsys CGS Linux V3.0用户使用手册

Microsoft Word - 武術-定稿.doc

数据结构与算法 - Python基础

060522達文西密碼_全_.PDF

投影片 1

Java Desktop System 呂衄盋 2 呂衄说柔

Spyder Anaconda Spyder Python Spyder Python Spyder Spyder Spyder 開始 \ 所有程式 \ Anaconda3 (64-bit) \ Spyder Spyder IPython Python IPython Sp

WWW PHP

Red Flag Linux Desktop 4.0 Red Flag Linux Desktop 4.0 1

软件测试(TA07)第一学期考试

C/C++ - 字符串与字符串函数

1

Ps22Pdf

ebook70-14

WWW PHP Comments Literals Identifiers Keywords Variables Constants Data Types Operators & Expressions 2

给初学者的入门知识 1. 建立和运行 shell 程序什么是 shell 程序呢? 简单的说 shell 程序就是一个包含若干行 shell 或者 linux 命令的文件. 象编写高级语言的程序一样, 编写一个 shell 程序需要一个文本编辑器. 如 VI 等. 在文本编辑环境下, 依据 shel

《计算机导论》实验教学大纲

Windows RTEMS 1 Danilliu MMI TCP/IP QEMU i386 QEMU ARM POWERPC i386 IPC PC104 uc/os-ii uc/os MMI TCP/IP i386 PORT Linux ecos Linux ecos ecos eco

第5章修改稿

epub 63-3

Untitled

05_資源分享-NFS及NIS.doc

Microsoft Word - 2AF63內文.doc

考 試 日 期 :2016/04/24 教 室 名 稱 :602 電 腦 教 室 考 試 時 間 :09: 二 技 企 管 一 胡 宗 兒 中 文 輸 入 四 技 企 四 甲 林 姿 瑄 中 文 輸 入 二 技 企 管 一

ttian

93年各縣國中教師甄試最新考情.doc

<4D F736F F D20AC4FBDBDA4FBB67DA96CAABA2DA743A67EAFC5AAA95FA7B9BD5A5F2E646F63>

PowerPoint 簡報

ex

第 零 場 (2005) 清 晨 天 未 亮 時, 喪 家 帆 布 棚 下 往 生 者 為 36 歲 王 宏 杰 冷 清 低 調 的 喪 禮 佈 置 孫 明 義 ( 男 36) 向 遺 孀 (34) 遞 上 奠 儀 王 妻 眼 神 空 洞, 臉 上 滿 滿 的 怨 恨 王 妻 用 極 低 的 音 量


2


投影片 1

, 即 使 是 在 昏 暗 的 灯 光 下, 她 仍 然 可 以 那 么 耀 眼 我 没 有 地 方 去, 你 会 带 着 我 么 杜 晗 像 是 在 嘲 笑 一 般, 嘴 角 的 一 抹 冷 笑 有 着 不 适 合 这 个 年 龄 的 冷 酷 和 无 情, 看 着 江 华 的 眼 神 毫 无 温

目錄

免费模板

ebook65-10

內文.tpf

RTX3.2.0标准版 - 技术白皮书

Transcription:

第 4 章 Linux shell 程序设计 shell UNIX/Linux Linux shell shell if case while for shell Linux Linux 本章主要介绍 Linux shell( 默认是 bash) 的语法结构 变量定义及赋值引用 标点符号 控制语句等 本章的主要内容如下 : shell 的主要特点 类型 建立和执行方式 bash 变量的分类 定义形式及引用规则 各种控制语句的格式 功能及流程 bash 中算术运算的使用 bash 函数的构成及使用规则 bash 中的内置命令 4.1 shell 概述 shell UNIX UNIX shell Linux UNIX shell bash 4.1.1 shell 的特点和主要版本 1.shell 的特点 UNIX shell shell shell *? [ ] shell shell cd echo exit pwd kill shell / I/O &

86 Linux 教程 ( 第 4 版 ) shell shell shell UNIX shell shell 2.shell 的种类 Linux shell Bourne shell sh C shell csh Korn shell ksh Bourne Again shell bash 1 Bourne shell AT&T Bell Steven Bourne AT&T UNIX UNIX shell shell Bourne shell shell 2 C shell Bill Joy BSD UNIX sh C Bourne shell C shell Bourne shell 3 Korn shell AT&T Bell David Korn C shell Bourne shell Bourne shell Korn shell 4 Bourne Again shell bash GNU shell Linux shell bash Bourne shell C shell Korn shell 1 2 vi 3 4 Linux shell ash zsh shell bash 4.1.2 简单 shell 程序示例 shell $ ls -l / usr / meng shell shell shell Script shell 例 4.1 shell ex1 $ cat ex1 date pwd cd.. shell

第 4 章 Linux shell 程序设计 87 例 4.2 shell ex2 $ cat ex2 #!/bin/bash # If no arguments, then listing the current directory. # Otherwise, listing each subdirectory. if test $# = 0 then ls. else for i do ls -l $i grep '^d' done fi #!/bin/bash bash bash # shell if else for 0 4.1.3 shell 脚本的建立和执行 1.shell 脚本的建立 shell vi 2. 执行 shell 脚本的方式 shell shell shell $ bash < $ bash <ex1 shell ex1 shell shell bash $ bash $ bash ex2 /usr/meng /usr/zhang

88 Linux 教程 ( 第 4 版 ) shell shell. shell $. shell shell shell shell chmod $ chmod a+x ex2 shell ex2 PATH $ PATH=$PATH:.. ex2 $ ex2 shell shell shell shell shell shell /bin/usr/bin shell shell shell shell shell Mount shell shell 4.2 命令历史 bash /.bash_history History Substitution

第 4 章 Linux shell 程序设计 89 4.2.1 显示历史命令 history history [option] [arg ] history history $ history 81 alias 82 pwd 83 history 1 history history 50 50 history history al al history Enter -a -n -r -w -c 4.2.2 执行历史命令 \ = (

90 Linux 教程 ( 第 4 版 ) 4.1 表 4.1 基本事件指定字格式及其意义!!!-1!n n!-n n!-1!!!string string!ca cat!?string? string!?hist? hist!# $ date 2016 04 02 09:28:06 CST $ pwd /home/mengqc $ ls Desktop ex-1 ex-2 ex-3 ex90 exam15 exam19 m1 m2 myfile usr $ cat m1 echo Hello! $ history 1 date 2 pwd 3 ls 4 cat m1 5 history $!2 pwd 2 /home/mengqc $!c cat m1 echo Hello! $!?w? pwd /home/mengqc 4.2.3 配置历史命令环境 bash.bash_history HISTFILE $ HISTFILE="/home/mengqc/.myhistory" /home/mengqc/.myhistory

第 4 章 Linux shell 程序设计 91 500 HISTSIZE $ HISTSIZE=600 bash 600 HISTSIZE 4.3 名称补全 bash Linux Tab Linux Tab Enter Linux shell Esc shell Esc+? Tab 4.4 别名 4.4.1 定义别名 shell alias alias [name[=value]] name=value name value $ alias

92 Linux 教程 ( 第 4 版 ) alias..='cd..' alias cp='cp -i' = $ alias ll=' ls -l ' $ my=/home/mengqc my $ ll $my $my ll ls -l ll shell ll ll $my ls -l /home/mengqc shell $ alias ll=ls -l bash: alias: '-l' not found $ pwd /home/mengqc $ ll m1 -ln m2 ttt ll ls -l ls 4.4.2 取消别名 unalias name name $ unalias ll $ alias ll ll alias not found alias unalias unalias unalias a

第 4 章 Linux shell 程序设计 93 4.5 shell 特殊字符 shell * 4.5.1 通配符 1. 一般通配符 4 * 0 f* f fa f1 fa2 ffa.s f. / *file.profile.*file.profile /etc*.c /etc.c/etc/*.c? f? f1 fa fb f fabc f12 [] f [abcd] fa, fb, fc fd f1 fa1 fab - f [a-d] f [abcd] f [1-9] f [123456789] * -a [ *? ] abc -a*abc-a? abc! [] f [!1-9].c f 1 9.c fa.c fb.c fm.c * /usr/meng/f?/* / usr/meng f chapter[0-9]* chapter 0 9 chapter chapter0 chapter1 chapter28 chapter123 2. 模式表达式 bash Bourne shell *? [ ] *() 0 file*(.c.o) file file.c file.o file.c.c file.o.o file.c.o file.o.c file.h file.s

94 Linux 教程 ( 第 4 版 ) +() file+(.c.o) file.c file.o file.c.o file.c.c file?() 0 1 file?(.c.o) file file.c file.o file. c. c file. c. o @() file@(.c.o) file.c file.o file file.c.c file.c.o!() file*(.[cho].sh) * + 4.5.2 引号 shell 1. 双引号 $ `\ $ $ 4.4.1 /home/mengqc $my ` \$` "\ \ shell 例 4.3 $ cat ex3 echo "current directory is ` pwd ` " echo "home directory is $HOME" echo "file*.?" echo "directory '$HOME ' " $ ex3 current directory is /home/mengqc/prog home directory is /home/mengqc file*.? directory '/home/mengqc' ex3 echo `pwd` pwd `pwd` echo $HOME HOME $HOME echo echo '$HOME'

第 4 章 Linux shell 程序设计 95 $HOME HOME 2. 单引号 $ str=' echo "directory is $HOME" ' $ echo $str echo "directory is $HOME" echo "directory is $HOME " str echo $HOME $ echo 'The time is ` date `, the file is $HOME/abc ' The time is ` date `, the file is $HOME/abc echo 3. 倒引号 shell shell $ echo current directory is ` pwd ` current directory is /home/mengqc shell `pwd` pwd/home/mengqc `pwd` $ today=`date` $ echo Today is $today Today is 2016 04 02 09:38:39 CST $ users=` who wc -l ` $ echo The number of users is $users The number of users is 5 \ $ Nuser=`echo The number of users is \` who wc -l \` ` $ echo $Nuser The number of users is 5 $ Nuser1=`echo The number of users is `who wc -l` ` 0

96 Linux 教程 ( 第 4 版 ) $ echo $Nuser1 \ echo "Filename is \"$HOME\"\$* " Filename is "/home/mengqc"$* \\ \ 4.5.3 输入 / 输出重定向符 shell stdin stdout stderr shell 1. 输入重定向符 < score C scanf() file1 score file1 score $ score < file1 shell shell cmds $ cat cmds echo your working directory is ` pwd ` echo your name is ` logname ` echo The time is ` date ` who $ bash < cmds shell cmds shell <

第 4 章 Linux shell 程序设计 97 2. 输出重定向符 > $ who > abc who abc who abc who $ cat abc shell shell exp1 echo "The time is `date` " echo "Your name is `logname` " echo "Working directory is `pwd` " echo "It has `ls -l wc -l` files. " $ exp1 > tmp1 $ cat tmp1 The time is 2016 04 02 09:56:22 CST Your name is mengqc Working directory is /home/mengqc It has 26 files. exp1 > I/O cat f1.c > /dev/lp0 f1.c 3. 输出附加定向符 >> $ ps -l >> psfile ps psfile cat psfile >>

98 Linux 教程 ( 第 4 版 ) 4. 即时文件定向符 here document << shell << mail $1 <<! Best wishes to you on your birthday.!!! <<!!!! % mail!!$1 $ wc -l < infile > outfile wc infile outfile 5. 与文件描述字有关的重定向 UNIX/Linux 0 1 2 2> 2>> $ gcc m1.c 2> errfile C m1.c errfile 2 2> >>

第 4 章 Linux shell 程序设计 99 command $>file command file command > file 2>& 1 2>& 1 2>& file file shell 0 9 10 3 9 cmd 9 f1 $ cmd 9> f1 $cmd 3<& 5 3 5 cmd 4.5.4 注释 管道线和后台命令 1. 注释 shell # 4.2 shell #!#! shell C shell #! /bin/csh bash #! /bin/bash shell 2. 管道线 UNIX/Linux ls -l $HOME wc -l grep tail sort wc ls grep m?.c wc -l m C 3. 后台命令

100 Linux 教程 ( 第 4 版 ) C C $ gcc m1.c& & shell shell CPU CPU 4.5.5 命令执行操作符 1. 顺序执行 pwd who wc -l cd /usr/bin pwd ; who wc -l ; cd /usr/bin 2. 逻辑与 && 1 && 2 1 2 1 2 cp ex1 ex10 && rm ex1 ex1 ex10 ex1 0 0 && cmd1 && cmd2 && && cmdn 3. 逻辑或

第 4 章 Linux shell 程序设计 101 1 2 1 2 1 2 cat abc pwd abc && & 4.5.6 成组命令 shell {} ( ) 1.{ } 形式 {} $ { echo "User Report for ` date `. "; who ; } pr 2016-04-02 09:59 Page 1 User Report for 2016 04 02 09:59:26 CST mengqc :0 2016-04-02 09:27 mengqc pts/0 2016-04-02 09:27 mengqc pts/1 2016-04-02 09:27 {} echo who pr {} { } ; {} { echo "Report of users for ` date `. " echo echo "There are ` who wc -l ` users logged in. " echo who sort ; } pr {} 2.( ) 形式 (echo "Current directory is ` pwd `. " cd /home/mengqc ; ls -l ; cp m1 em1 && rm m1 cat em1) pr

102 Linux 教程 ( 第 4 版 ) ()() {} {} shell () shell () shell $ a="current value " ; export a export 4.6.10 $ echo $a current value $ ( a="new value-1 " ; echo $a ) new value-1 $ echo $a current value $ { a="new value-2 " ; echo $a ; } new value-2 $ echo $a new value-2 $ pwd /home/mengqc $ (cd /bin ; pwd ) /bin $ pwd /home/mengqc $ { cd /bin ; pwd ; } /bin $ pwd /bin 4.6 shell 变量 shell shell C shell shell shell 4.6.1 用户定义的变量 1. 变量名 shell