shell变量之辨[10]

[内部变量]

#! /bin/bash
function func
{
        echo "$FUNCNAME";
}
function showVar()
{
        eval value=\$$1;
        echo "$1: $value";
}

func
for i in BASH HOME HOSTNAME HOSTTYPE IFS LINENO PATH PWD UID
do
        showVar $i
done

以上这段代码, 其中func是一个函数, 中间直接打印了一个叫做”$FUNCNAME”的东西, 这个就是一个内部变量. 指的是函数本身的名称.

而第二个函数showVar, 是我封装的一个简单函数. 或者叫做”变量的变量”, 比如调用的时候, 参数是BASH, 那么showVar BASH就表示echo “BASH: $BASH”. 那么这样是比较方便地循环打印各个内部变量的~

样例输出如下:

func
BASH: /bin/bash
HOME: /home/work
HOSTNAME: cq01-testing-ps11101.vm.baidu.com
HOSTTYPE: x86_64
IFS:

LINENO: 8
PATH: /home/work/btest/errHunter:/home/work/staf/extbin/atptool/bin/atpfs:/home/work/staf/extbin/atptool/bin/atpreport:/home/work/staf/extbin/atptool/bin/atpkeylib:/home/work/staf/extbin/atptool/bin/atpcaserun:/home/work/safe_psqa/python/bin:/home/work/safe_psqa/bin:/home/work/valgrind/bin:/home/work/btest/bin:/home/work/octopus:/extbin/atptool/bin/atpfs:/extbin/atptool/bin/atpreport:/extbin/atptool/bin/atpkeylib:/home/work/ltrtools/version_test/python/bin:/home/work/tool/mysql/bin/:/home/work/bin:/home/work/bin/sl/bin:/usr/sbin:/usr/bin:/bin/.:/home/work/hadoop-client/hadoop/bin/:/home/work/tools:/home/work/bin/subversion/bin:/home/work/atptool/output/bin/atpcaserun/:/home/work/bin/sl/bin::/home/work/bin/sl/bin:
PWD: /home/work/bian_wei/share
UID: 500

本文链接:https://www.poisonbian.com/post/823.html 转载需授权!

分享到:
原文链接:,转发请注明来源!
「shell变量之辨[10]」评论列表

发表评论