8. 环境与自定义变量

[root@localhost test]# cat ping02.sh
#!/usr/bin/bash
ping -c1 $1 &> /dev/null
if [ $? -eq 0 ]; then
    echo "ping $1 succeed!"
else
    echo "ping $1 failed"
fi
[root@localhost test]# ./ping02.sh
ping  failed
[root@localhost test]# ./ping02.sh www.sohu.com
ping www.sohu.com succeed!

$1:表示第一个输入变量。

上例实现了不在脚本中定义ping的对象,ping的对象由执行脚本时在外面输入。

linux中父shell与子shell(脚本执行的几种方式)

显示所有环境变量


[root@localhost test]# env
XDG_SESSION_ID=5
HOSTNAME=localhost.localdomain
SELINUX_ROLE_REQUESTED=
PYENV_ROOT=/root/.pyenv
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=192.168.146.1 51091 22
SELINUX_USE_CURRENT_RANGE=
OLDPWD=/root
SSH_TTY=/dev/pts/1
USER=root
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
PYENV_VIRTUALENV_INIT=1
MAIL=/var/spool/mail/root
PATH=/root/.pyenv/plugins/pyenv-virtualenv/shims:/root/.pyenv/shims:/root/.pyenv/bin:/usr/java/jdk-13.0.1/bin:/usr/java/jdk-13.0.1/jre/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/bin:/root/bin
PWD=/root/test
JAVA_HOME=/usr/java/jdk-13.0.1
LANG=en_US.UTF-8
SELINUX_LEVEL_REQUESTED=
HISTCONTROL=ignoredups
PYENV_SHELL=bash
SHLVL=1
HOME=/root
LOGNAME=root
CLASSPATH=:/usr/java/jdk-13.0.1/lib:/usr/java/jdk-13.0.1/jre/lib
SSH_CONNECTION=192.168.146.1 51091 192.168.146.128 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/0
_=/usr/bin/env
[root@localhost test]#

vi /etc/profile  设定全局变量

# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}


if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
        # ksh workaround
        EUID=`/usr/bin/id -u`
        UID=`/usr/bin/id -ru`
    fi
    USER="`/usr/bin/id -un`"
    LOGNAME=$USER
    MAIL="/var/spool/mail/$USER"
fi

# Path manipulation
if [ "$EUID" = "0" ]; then
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
fi

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi

[root@localhost test]# cat public.sh
#!/usr/bin/bash
export ip1="1.1.1.1"
export dir_path="/etc/abc.txt"

[root@localhost test]# cat 1.sh
#!/usr/bin/bash
source public.sh
echo "ip1 is: $ip1"
echo "dir_path is :$dir_path"

[root@localhost test]# . 1.sh
ip1 is: 1.1.1.1
dir_path is :/etc/abc.txt

[root@localhost test]# source 1.sh
ip1 is: 1.1.1.1
dir_path is :/etc/abc.txt

[root@localhost test]# chmod u+x 1.sh public.sh
[root@localhost test]# ./1.sh
ip1 is: 1.1.1.1
dir_path is :/etc/abc.txt

1.sh中调用执行public.sh中声明的全局变量。

vi /etc/profie 在最后一行新增加一个path

[root@localhost test]# vi /etc/profile
[root@localhost test]# source /etc/profile
[root@localhost test]# echo $PATH
/usr/java/jdk-13.0.1/bin:/usr/java/jdk-13.0.1/jre/bin:/usr/java/jdk-13.0.1/bin:/usr/java/jdk-13.0.1/jre/bin:/root/.pyenv/plugins/pyenv-virtualenv/shims:/root/.pyenv/shims:/root/.pyenv/bin:/usr/java/jdk-13.0.1/bin:/usr/java/jdk-13.0.1/jre/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/bin:/root/bin:/bin:/new/test/bin:/bin:/new/test/bin

脚本里面用大括号将变量与其它字符分割

发布了423 篇原创文章 · 获赞 134 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/f2157120/article/details/105613773