非常教程

Linux参考手册

Shell 教程

Shell 教程

Shell 教程

Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。

Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。

Ken Thompson 的 sh 是第一种 Unix Shell,Windows Explorer 是一个典型的图形界面 Shell。

Shell 在线工具


Shell 脚本

Shell 脚本(shell script),是一种为 shell 编写的脚本程序。

业界所说的 shell 通常都是指 shell 脚本,但读者朋友要知道,shell 和 shell script 是两个不同的概念。

由于习惯的原因,简洁起见,本文出现的 "shell编程" 都是指 shell 脚本编程,不是指开发 shell 自身。


Shell 环境

Shell 编程跟 JavaScript、php 编程一样,只要有一个能编写代码的文本编辑器和一个能解释执行的脚本解释器就可以了。

Linux 的 Shell 种类众多,常见的有:

  • Bourne Shell(/usr/bin/sh或/bin/sh)
  • Bourne Again Shell(/bin/bash)
  • C Shell(/usr/bin/csh)
  • K Shell(/usr/bin/ksh)
  • Shell for Root(/sbin/sh)
  • ……

本教程关注的是 Bash,也就是 Bourne Again Shell,由于易用和免费,Bash 在日常工作中被广泛使用。同时,Bash 也是大多数Linux 系统默认的 Shell。

在一般情况下,人们并不区分 Bourne Shell 和 Bourne Again Shell,所以,像 #!/bin/sh,它同样也可以改为 #!/bin/bash

#! 告诉系统其后路径所指定的程序即是解释此脚本文件的 Shell 程序。


第一个shell脚本

打开文本编辑器(可以使用 vi/vim 命令来创建文件),新建一个文件 test.sh,扩展名为 sh(sh代表shell),扩展名并不影响脚本执行,见名知意就好,如果你用 php 写 shell 脚本,扩展名就用 php 好了。

输入一些代码,第一行一般是这样:

实例

#!/bin/bash
echo "Hello World !"

运行实例 »

#! 是一个约定的标记,它告诉系统这个脚本需要什么解释器来执行,即使用哪一种 Shell。

echo 命令用于向窗口输出文本。

运行 Shell 脚本有两种方法:

1、作为可执行程序

将上面的代码保存为 test.sh,并 cd 到相应目录:

chmod +x ./test.sh  #使脚本具有执行权限
./test.sh  #执行脚本

注意,一定要写成 ./test.sh,而不是 test.sh,运行其它二进制的程序也一样,直接写 test.sh,linux 系统会去 PATH 里寻找有没有叫 test.sh 的,而只有 /bin, /sbin, /usr/bin,/usr/sbin 等在 PATH 里,你的当前目录通常不在 PATH 里,所以写成 test.sh 是会找不到命令的,要用 ./test.sh 告诉系统说,就在当前目录找。

2、作为解释器参数

这种运行方式是,直接运行解释器,其参数就是 shell 脚本的文件名,如:

/bin/sh test.sh
/bin/php test.php

这种方式运行的脚本,不需要在第一行指定解释器信息,写了也没用。

Shell 教程

Linux目录

1.Linux 用户和用户组管理
2.Linux 文件与目录管理
3.Linux 磁盘管理
4.Shell 变量
5.Shell 教程
6.linux yum 命令
7.Shell 函数
8.Shell 流程控制
9.Shell test 命令
10.Shell echo命令
11.Linux cksum命令
12.Linux chown命令
13.Linux chmod命令
14.Linux chgrp命令
15.Linux chattr命令
16.Linux cat命令
17.Linux cut命令
18.Linux indent命令
19.Linux gitview命令
20.Linux git命令
21.Linux find命令
22.Linux file命令
23.Linux diffstat命令
24.Linux 命令大全
25.Linux diff命令
26.Linux cmp命令
27.Linux more命令
28.Linux mktemp命令
29.Linux mdir命令
30.Linux mdel命令
31.Linux mc命令
32.Linux mattrib命令
33.Linux lsattr命令
34.Linux locate命令
35.Linux less命令
36.Linux ln命令
37.Linux rcp命令
38.Linux patch命令
39.Linux paste命令
40.Linux od命令
41.Linux mv 命令
42.Linux mtoolstest命令
43.Linux mtools命令
44.Linux mren命令
45.Linux mread命令
46.Linux mmove命令
47.Linux mcopy命令
48.Linux cp命令
49.Linux which命令
50.Linux umask命令
51.Linux touch命令
52.Linux tmpwatch命令
53.Linux tee命令
54.Linux split命令
55.Linux slocate命令
56.Linux rm命令
57.Linux ex命令
58.Linux egrep命令
59.Linux ed命令
60.Linux csplit命令
61.Linux comm命令
62.Linux colrm命令
63.Linux col命令
64.Linux whereis命令
65.Linux rhmask命令
66.Linux mshowfat命令
67.Linux mtype命令
68.Linux look命令
69.Linux join命令
70.Linux joe命令
71.Linux jed命令
72.Linux ispell命令
73.Linux grep 命令
74.Linux fold命令
75.Linux fmt命令
76.Linux fgrep命令
77.Linux lprm命令
78.Linux wc命令
79.Linux uniq 命令
80.Linux expr命令
81.Linux tr命令
82.Linux spell命令
83.Linux sort命令
84.Linux sed 命令
85.Linux rgrep命令
86.Linux pico命令
87.Linux uucp命令
88.Linux uupick命令
89.Linux uuto命令
90.Linux tftp命令
91.Linux ncftp命令
92.Linux ftp命令
93.Linux bye命令
94.Linux lpd命令
95.Linux lpq命令
96.Linux lpr命令
97.Linux mlabel命令
98.Linux edquota命令
99.Linux du命令
100.Linux dirs命令