zoukankan      html  css  js  c++  java
  • shell编程——内部变量

    常用的内部变量有:echo, eval, exec, export, readonly, read, shift, wait, exit 和 点(.)

    echo:将变量名指定的变量显示到标准输出
    [root@localhost ~]# echo test
    test
    shift:使所有的位置参数依次向左移动一个位置,并使位置参数$#(个数)减1,直到减为0
    [root@localhost ~]# cat a.sh 
    echo "$1,$2,$3,$4"
    echo "参数个数:$#"
    shift
    echo "$1,$2,$3,$4"
    echo "参数个数:$#"
    [root@localhost ~]# sh a.sh 1 2 3 4
    1,2,3,4
    参数个数:4
    2,3,4,
    参数个数:3
    export:把变量带入子shell,让子进程继承父进程中的环境变量。
    语法:export 变量名=value
    -bash-4.1$ echo $LANG
    zh_CN.UTF-8
    -bash-4.1$ export LANG=zh_CN.GB2312
    -bash-4.1$ echo $LANG
    zh_CN.GB2312
    
    注意:子shell不能使用export把它的变量向上带入父shell
    read:从标准输入读取字符串,并传给指定变量。
    可以在函数中用(local 变量名)的方式申明局部变量,测试脚本内容如下:
    [root@localhost collect]# cat test.sh 
    #!/bin/bash
    # this script is created by dengtr.
    # e_mail:
    # qqinfo:
    # function:
    # version:
    ################################################
    test() {
    local name
    echo "please input your name:"
    read name
    echo "this is $name"
    }
    
    test;
    
    执行脚本:
    [root@localhost collect]# sh test.sh 
    please input your name:
    
    #此时脚本等待用户输入,我在这输入tom,结果如下:
    [root@localhost collect]# sh test.sh 
    please input your name:
    tom
    this is tom
  • 相关阅读:
    VS2010/MFC编程入门之四(MFC应用程序框架分析)
    VS2010/MFC编程入门之三(VS2010应用程序工程中文件的组成结构)
    shell脚本学习一
    docker安装rabbitmq
    docker安装activemq
    docker搭建数据库主从复制
    在docker容器中安装vim命令进行编辑文件
    docker的常用操作
    docker上安装eslaticsearch
    centos7上面关闭防火墙
  • 原文地址:https://www.cnblogs.com/dengtr/p/5027369.html
Copyright © 2011-2022 走看看