zoukankan      html  css  js  c++  java
  • Shell入门教程:Shell的基本结构

    shell程序的基本组成结构

    shell结构大体是由设定变量、内置命令、shell的语法结构、函数组成。

    使用实例说明:test.sh

    #!/bin/bash
    #说明使用/bin/bash作为这个脚本的解释器
    #定义一个函数
    function my_fun () {
    	echo "Hello, $1,today is $2"
    }
    #定义连个变量
    name=$1
    today=`date`
    #函数调用
    my_fun "$name" "$today"

    上面的这个脚本要想运行还需要做一些操作,首先给予执行权限

    chmod +x test.sh

    然后执行

    ./test.sh john

    输出

    Hello, john,today is Tue Jun? 1 14:51:46 CST 2010

    父shell和子shell

    在执行script之前,身处的环境就是父shell。执行script之时,父shell根据#!/bin/bash,fork出来一个新的shell环境,然后在子shell中执行,执行完毕后子shell结束,任然回到父shell中,这样不会影响到父shell的环境。


     

    这张图片是login shell的流程,当是non-login shell时,只执行方框中的标注的部分。由这张图我们可以知道,在如下几种情况下,执行的流程。

    登陆(login)

    /etc/profile
    ~/.bash_profile

    注销(logout)

    ~/.bash_logout

    执行新shell,分成两种情况

    1.执行交互式的shell

    ~/.bashrc
    /etc/bashrc

    2.执行非交互式的shell,比如执行script会检查 BASH_ENV 变量的内容,如果有定义,则执行。

  • 相关阅读:
    linux 获取外网ip地址
    出现大量rcuob进程
    禁用 ipv6
    centos yum 坏掉 db 损坏
    关于 solusvm
    VMWare 下 Ubuntu 18.04 的文件共享
    安装最新版本 nginx
    大量的Close_wait 发现的 too many open file 错
    Linux安装Desktop 和 vncserver
    MySQL in和limit不能连用的问题
  • 原文地址:https://www.cnblogs.com/52php/p/5669689.html
Copyright © 2011-2022 走看看