linux学习之shell脚本 ------- 基础

来源:互联网 发布:什刹海 知乎 编辑:程序博客网 时间:2024/05/23 02:07

[本文是自己学习所做笔记,欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020]

Shell脚本基本元素:

#!/bin/bash     --- 第一行#     --- 注释变量流程控制结构

  看一个简单的例子,学任何语言,我想大多数情况下都是从helloworld程序开始的,shell也是一门语言,我们也从helloworld开始。

  新建一个名为helloworld.sh的文件,在里面写入:

helloworld.sh

#!/bin/sh#这是一个很简单的打印“hello world”的shell脚本echo "hello world!"
  要执行这样一个简单的脚本,首先我们要给该文件可执行的权限。如下:

chmod u+x helloworld.sh ./helloworld.sh hello world!
  可以看出,通过这样一个简单的脚本就可以将“hello world”打印到屏幕上。

Shell特性: 

  一般而言,shell脚本有以下特性:

  1) 别名

  2) 命令替换

  3) 后台处理

  4) 变量

  5) 管道

  6) 重定向

  7) 模式匹配

  8) 特殊字符

  

  下面挨个介绍:

  1) 别名

   可以通过alias查看当前系统的别名,如我的系统别名如下:

alias egrep='egrep --color=auto'alias fgrep='fgrep --color=auto'alias grep='grep --color=auto'alias l='ls -CF'alias la='ls -A'alias ll='ls -alF'alias ls='ls --color=auto'
   也可以自定义别名,如:

jesson@jesson-HP:~$ alias lh='ls -lh'jesson@jesson-HP:~$ lh总用量 40Kdrwxr-xr-x 2 jesson jesson 4.0K 12月 18 09:47 Desktopdrwx------ 4 jesson jesson 4.0K 12月  2 20:29 developdrwxr-xr-x 8 jesson jesson 4.0K 12月 16 11:27 iNodeClientdrwxr-xr-x 2 jesson jesson 4.0K 12月  6 19:30 公共的drwxr-xr-x 2 jesson jesson 4.0K 12月  6 19:30 模板drwxr-xr-x 2 jesson jesson 4.0K 12月  6 19:30 视频drwxr-xr-x 3 jesson jesson 4.0K 12月 16 14:51 图片drwxr-xr-x 9 jesson jesson 4.0K  1月 15 20:22 文档drwxr-xr-x 6 jesson jesson 4.0K  1月 15 20:45 下载drwxr-xr-x 2 jesson jesson 4.0K 12月  6 19:30 音乐
   既然可以自定义别名,当然,也可以取消别名,其实很简单,直接输入unalias 别名 即可。

  2) 命令替换

   直接看这样一个命令

jesson@jesson-HP:~$ ls -l `cat /etc/shells`-rwxr-xr-x 1 root root 920788  3月 29  2013 /bin/bash-rwxr-xr-x 1 root root 100284  3月 30  2012 /bin/dashlrwxrwxrwx 1 root root      4 12月  6 19:10 /bin/rbash -> bashlrwxrwxrwx 1 root root      4 12月  6 19:10 /bin/sh -> dash
   可以看出,命令的结果是列出当前系统存在的shell。其实,执行过程是这样的,先执行` `中的命令,执行结果如下:

jesson@jesson-HP:~$ cat /etc/shells # /etc/shells: valid login shells/bin/sh/bin/dash/bin/bash/bin/rbash
   这个命令的执行结果打印出该文件的内容,然后,再执行ls -l 输出的每行 ,这样就按行列出所有的shell的详细信息,其实在命令中加入` `的作用就是命令替换。

  3) 后台处理

    一个终端可以同时运行多个后台程序。

   用法: nohup command &

            可以用jobs 查看当前的后台程序。

  4) 变量

    变量是用来存储信息的。如系统变量SHELL,PATH等,当然了也以自己定义变量了。

  5) 管道

   管道是把一个命令的输出连接到另一个命令的输入。如:

jesson@jesson-HP:~$ ls | sortDesktopdevelopiNodeClient公共的模板视频图片文档下载音乐
  可以注意到,这是排序后的输出结果。

  6) 重定向

    重定向与管道相关,可以改变程序的输入来源和输出地点,

jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ls -l  >homefile.txtjesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ll总用量 12drwxrwxr-x 2 jesson jesson 4096  1月 16 00:30 ./drwxrwxr-x 7 jesson jesson 4096  1月 16 00:28 ../-rw-rw-r-- 1 jesson jesson  573  1月 16 00:30 homefile.txtjesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ cat homefile.txt 总用量 40drwxr-xr-x  2 jesson jesson 4096  1月 11 23:44 Desktopdrwxrwxr-x  4 jesson jesson 4096  1月 12 00:41 developdrwxr-xr-x  8 jesson jesson 4096  1月 11 21:51 iNodeClientdrwxr-xr-x  2 jesson jesson 4096  1月 11 21:23 公共的drwxr-xr-x  2 jesson jesson 4096  1月 11 21:23 模板drwxr-xr-x  2 jesson jesson 4096  1月 11 21:23 视频drwxr-xr-x  3 jesson jesson 4096  1月 14 23:26 图片drwxr-xr-x  2 jesson jesson 4096  1月 11 22:46 文档drwxr-xr-x  5 jesson jesson 4096  1月 16 00:26 下载drwxr-xr-x 25 jesson jesson 4096  1月 12 00:48 音乐jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ sort < homefile.txt > homefile.txt.sortjesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ll总用量 16drwxrwxr-x 2 jesson jesson 4096  1月 16 00:31 ./drwxrwxr-x 7 jesson jesson 4096  1月 16 00:28 ../-rw-rw-r-- 1 jesson jesson  573  1月 16 00:30 homefile.txt-rw-rw-r-- 1 jesson jesson  573  1月 16 00:31 homefile.txt.sortjesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ cat homefile.txt.sort drwxrwxr-x  4 jesson jesson 4096  1月 12 00:41 developdrwxr-xr-x 25 jesson jesson 4096  1月 12 00:48 音乐drwxr-xr-x  2 jesson jesson 4096  1月 11 21:23 公共的drwxr-xr-x  2 jesson jesson 4096  1月 11 21:23 模板drwxr-xr-x  2 jesson jesson 4096  1月 11 21:23 视频drwxr-xr-x  2 jesson jesson 4096  1月 11 22:46 文档drwxr-xr-x  2 jesson jesson 4096  1月 11 23:44 Desktopdrwxr-xr-x  3 jesson jesson 4096  1月 14 23:26 图片drwxr-xr-x  5 jesson jesson 4096  1月 16 00:26 下载drwxr-xr-x  8 jesson jesson 4096  1月 11 21:51 iNodeClient总用量 40
         容易看出,利用重定向可以很方便的指定程序的输入来源和输出,如上述例子中
sort < homefile.txt > homefile.txt.sort
sort的来源是homefile.txt文件,而输出也是文件,这里是homefile.txt.sort.

  7) 模式匹配

   如显示以txt为扩展名或以a开头的文件,这种能力即称为模式匹配,在模式匹配中,一般使用正则表达式。

例如:

jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ls -l *.txt-rw-rw-r-- 1 jesson jesson 573  1月 16 00:30 homefile.txt  

  8) 特殊字符

   双引号(""): 用来使Shell无法认出空格,制表符和其他大多数特殊字符,这样“ls -l helloworld.sh” 表示一个值。

   单引号(''): 用来使Shell无法认出所有特殊字符。

   反引号(``): 用来替换命令。

   反斜杠(\): 用来使Shell无法认出其后的特殊字符,使其后的特殊字符失去特殊含义。

   分号(;): 允许在一行放置多个命令。

   &: 后台执行。

   括号(): 创建成组的命令。

   大括号{}: 创建命令块。

   竖杆(|): 管道表示符。

   < >: 重定向表示符。

   * ? []: 模式匹配符。

   $: 变量名的开头。

   #: 注释

   空格,制表符,换行符: 当作空白。

0 0