zoukankan      html  css  js  c++  java
  • Shell脚本初探

    一、Shell编程概述

    简介

    Shell是一种命令行解释器,为用户和操作系统之间提供通信的一种接口,它接受来自用户输入的命令,并将其转换为一系列的系统调用送到内核执行,并将结果输出给用户。

    Shell也是一种编程工具,称为脚本语言,与编译型语言不同,脚本语言又被称作解释型语言,运行时翻译,执行一条语句时才翻译。

    Shell脚本以#!开头指明解释器的具体位置(#!/bin/bash),其他位置使用#注释;

    运行方式:1、 bash file.sh   2、 ./file.sh  (需要有可执行权限)  3、 file.sh  (可执行权限,当前目录包含在$PATH中)

    调试: bash -x file.sh,在脚本中使用  set -x 和 set +x 可以把需要调试的部分包含进来;

    内建命令

    内建命令:bash自身提供的命令,执行过程调用当前Shell进程的一个函数

    外部命令:执行时触发磁盘IO,需要fork出一个单独的进程来执行,执行完再退出

    [root@VM_139_12_tlinux ~]# which cd           //内建命令,不是可执行文件
    /usr/bin/which: no cd in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
    [root@VM_139_12_tlinux ~]# type cd        //判断是否是内建命令
    cd is a shell builtin
    [root@VM_139_12_tlinux ~]# type ifconfig    
    ifconfig is /sbin/ifconfig

    ------

    执行程序:.(点号)

    root@192.168.100.254:/data/redd/test# ./fact.sh 5
    bash: ./fact.sh: Permission denied
    root@192.168.100.254:/data/redd/test# . ./fact.sh 5
    120
    root@192.168.100.254:/data/redd/test# . fact.sh 5 
    120
    root@192.168.100.254:/data/redd/test# source ./fact.sh 6     //使用source亦可
    720
    root@192.168.100.254:/data/redd/test# source fact.sh 6 
    720
    root@192.168.100.254:/data/redd/test#

    ------

    别名:alias

    [root@VM_9_26_tlinux /data]# alias      
    alias grep='grep --color=auto'
    alias l.='ls -d .* --color=auto'
    alias ll='ls -l --color=auto'
    alias ls='ls --color=auto'
    alias vi='vim'
    alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
    [root@VM_9_26_tlinux /data]# cat ~/.bashrc   //在用户家目录的.bashrc中设置可永久生效
    # .bashrc
     
    # User specific aliases and functions
     
    #alias rm='rm -i'
    #alias cp='cp -i'
    #alias mv='mv -i'
     
    # Source global definitions
    if [ -f /etc/bashrc ]; then
            . /etc/bashrc
    fi
     
    if [ "$TERM" != "dumb" ] && [ -x /usr/bin/dircolors ]; then
        eval `dircolors /etc/DIR_COLORS`
        alias ls='ls --color=auto'
        alias grep='grep --color=auto'
    fi
    [root@VM_9_26_tlinux /data]# alias hello='ll'      //只在当前shell环境下有效
    [root@VM_9_26_tlinux /data]# hello
    total 156
    drwxr-xr-x 2 root root  4096 Aug 13 03:21 container2host
    -rwxr-xr-x 1 root root  6677 Dec 31  2014 daily.pl
    drwxr-xr-x 3 root root  4096 May 23  2014 dcagent
    drwxr-xr-x 4 root root  4096 Jun 23 07:27 home
    drwxr-xr-x 4 root root  4096 May 26  2014 install
    drwxr-xr-x 3 root root  4096 Jul 27 14:14 lighttpd_logs
    drwxr-xr-x 3 root root  4096 Aug 20 15:48 log
    drwxr-xr-x 2 root root  4096 Sep  2 05:30 log_copy_dir
    drwx------ 2 root root 16384 May 22  2014 lost+found
    drwxr-xr-x 4 root root  4096 Sep  2 11:47 release
    drwxr-xr-x 2 root root  4096 May 26  2014 setup_rs
    -rw-r--r-- 1 root root 15389 Aug 13 03:21 setup_rs_last_log
    -rw-r--r-- 1 root root 69543 Aug 13 03:21 setup_rs.log
    drwxr-xr-x 3 root root  4096 Aug 20 15:34 stat
    drwxr-xr-x 2 root root  4096 Feb  3  2015 temp
    [root@VM_9_26_tlinux /data]# unalias hello          //只在当前shell有效
    [root@VM_9_26_tlinux /data]# hello       
    bash: hello: command not found
    [root@VM_9_26_tlinux /data]# alias he='ls'
    [root@VM_9_26_tlinux /data]# he
    container2host  daily.pl  dcagent  home  install  lighttpd_logs  ......
    [root@VM_9_26_tlinux /data]# unalias -a           //删除当前shell环境下所有别名
    [root@VM_9_26_tlinux /data]# he
    bash: he: command not found
    [root@VM_9_26_tlinux /data]#

    ------

    任务前后台切换:bg、fg、jobs

    ------

    打印: echo

    root@192.168.100.254:/data/redd/test# echo "hello word"  //默认换行打印
    hello word
    root@192.168.100.254:/data/redd/test# echo -n "hello word"  //不换行
    hello wordroot@192.168.100.254:/data/redd/test# 
    root@192.168.100.254:/data/redd/test# echo "hello	word
    "   //默认不解释打印转义字符
    hello	word
    
    root@192.168.100.254:/data/redd/test# echo -e "hello	word
    " //解释转义字符
    hello   word
    
    root@192.168.100.254:/data/redd/test# 

    ------

    将参数作为shell的输入并执行命令:eval

     ------

    执行命令以取代当前的shell:exec

    内建命令exec不会启动新的Shell,只是用要被执行的命令替换当前的Shell进程,并且将老进程的环境清理掉,而且exec命令后的其他命令将不再执行。为了避免这种情况,将exec命令放到脚本中,由主脚本去调用这个脚本,在子脚本中执行到exec后,该子脚本进程就被替换成相应的exec的命令。

    另,source命令和点号不会为脚本新建shell,只是将脚本包含的命令在当前shell执行。

      

    ------

    变量导出:export

    用户登录到系统后,将启动一个shell,可以创建并运行脚本。通常,登录shell是父shell,则在该脚本中运行的shell是子shell。父shell中创建变量时,这些变量并不会被其子shell进程所知,即变量默认情况下是私有的,使用export可以将变量导出,使子shell也可以使用该变量。

    注意,子shell中对该变量的修改并不会影响父shell,即变量导出相当于值传递。

    ------

    从标准输入读取一行到变量:read

    root@192.168.100.254:/root# read       
    23
    root@192.168.100.254:/root# echo $REPLY
    23
    root@192.168.100.254:/root# read       
    hello world
    root@192.168.100.254:/root# echo $REPLY
    hello world
    root@192.168.100.254:/root# read -p "enter a value:" N
    enter a value:ok
    root@192.168.100.254:/root# echo $N
    ok
    root@192.168.100.254:/root# 

    ------

    向左移动位置参数:shift

    脚本在运行时可接受参数,第一个参数为$1,第二个为$2......,第n个为$n,所有参数记作$@或$*,参数总个数记作$#,脚本名记作 $0

    另,$@和$*的区别

    在不用双引号包含时,都是一样的,以IFS划分字段

    在使用双引号包含时,$@以IFS(默认空格)划分各个参数,$*将所有参数视作一个整体

    #!/bin/bash
    until  [ -z "$1" ]
    do
         echo "$@ "
         shift    
    done

    运行之

    root@192.168.100.254:/data/redd/test# . shift.sh A B C
    A B C
    B C
    C
    root@192.168.100.254:/data/redd/test# 

    ------

    控制shell执行程序的资源:ulimit

    Provides control over the resources available to the shell and to processes started by it, on systems that allow such control.用来控制shell及它所产生的进程的资源。

    The value of limit can be a number in the unit specified for the resource or one of the special values hard, soft, or unlimited, which stand for the current hard limit, the current soft limit, and no limit, respectively.

    root@192.168.100.254:/data/redd/test# ulimit -a
    core file size          (blocks, -c) 0           //core文件上限
    data seg size           (kbytes, -d) unlimited  //数据段最大值
    scheduling priority             (-e) 0      //调度优先级
    file size               (blocks, -f) unlimited  //创建文件的最大值
    pending signals                 (-i) 62822    //挂起的信号数量
    max locked memory       (kbytes, -l) 64         //最大锁定内存值
    max memory size         (kbytes, -m) unlimited  //最大可用常驻内存值
    open files                      (-n) 100001     //最大打开文件数
    pipe size            (512 bytes, -p) 8      //管道最大缓冲区的值
    POSIX message queues     (bytes, -q) 819200   //消息队列的最大值
    real-time priority              (-r) 0      //程序的实时性优先级
    stack size              (kbytes, -s) 10240    //栈大小
    cpu time               (seconds, -t) unlimited  //最大CPU占用时间
    max user processes              (-u) 1024     //用户最大进程数目
    virtual memory          (kbytes, -v) unlimited    //最大虚拟内存
    file locks                      (-x) unlimited    //文件锁

    一般用法

    ulimit -HSTabcdefilmnpqrstuvx [limit]  //不带limit查看对应的值,-S设置软限制,-H设置硬限制,默认同时设置
    ulimit -c ulimited           //设置最大core文件大小
    ulimit -S -n 4096            //单独设置软限制

    二、Bash Shell的安装

    三、Shell编程基础

    四、测试和判断

    五、循环

    六、函数

    七、重定向

    八、脚本范例

    来源:Linux系统命令及Shell脚本实践指南

    雷锋:LinuxTone.Org:::>/ebooks/

  • 相关阅读:
    context-annotation
    bean-annotation
    K-means算法
    基于概率的分类-贝叶斯分类
    Application
    ConfigurableApplicationContext
    相关性分析
    方差分析
    Java 大写金额转换成数字
    linux 遍历文件添加index
  • 原文地址:https://www.cnblogs.com/itree/p/4774738.html
Copyright © 2011-2022 走看看