五周第四次课(1月11日)
8.6 管道符和作业控制
8.7/shell变量
8.8 shell变量
8.9 环境变量配置文件
扩展
bashrc和bash_profile的区别 http://ask.apelearn.com/question/7719
简易审计系统: http://www.68idc.cn/help/server/linux/2014042190951.html
关于PROMPT_COMMAND环境变量的含义 http://www.linuxnote.org/prompt_command-environment-variables.html
env命令:
用于显示系统中已存在的环境变量,以及在定义的环境中执行指令。该命令只使用"-"作为参数选项时,隐藏了选项"-i"的功能。若没有设置任何选项和参数时,则直接显示当前的环境变量。
如果使用env命令在新环境中执行指令时,会因为没有定义环境变量"PATH"而提示错误信息"such file or directory"。此时,用户可以重新定义一个新的"PATH"或者使用绝对路径。
语法
env(选项)(参数)
选项
-i:开始一个新的空的环境;
-u<变量名>:从当前环境中删除指定的变量。
参数
变量定义:定义在新的环境中变量,定义多个变量定义用空格隔开。格式为“变量名=值”;
指定:指定要执行的指令和参数。
实例
[[email protected] ~]# env
hostname=LinServ-1
TERM=linux
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=192.168.2.111 2705 22
SSH_TTY=/dev/pts/0
USER=root
LS_COLORS=no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:*.xpm=01;35:*.png=01;35:*.tif=01;35:
mail=/var/spool/mail/root
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
INPUTRC=/etc/inputrc
pwd=/root
LANG=zh_CN.UTF-8
SHLVL=1
HOME=/root
logname=root
SSH_CONNECTION=192.168.2.111 2705 192.168.2.2 22
LESSOPEN=|/usr/bin/lesspipe.sh %s
G_BROKEN_FILENAMES=1
_=/bin/env
Connecting to 192.168.183.128:22...
Connection established.
To escape to local shell, press ‘Ctrl+Alt+]‘.
Last login: Wed Jan 10 23:08:05 2018
[[email protected] ~]# ls
234 anaconda-ks.cfg.1 新建文本文档_(2).txt
[[email protected] ~]# ls |wc -l
3
[[email protected] ~]# find ./ -type f
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./.bash_history
./.ssh/.suthorized_keyx.swp
./.ssh/.suthorized_keyx.swo
./.ssh/.suthorized_keyx.swn
./.ssh/authorized_keys
./.ssh/suthorized_keyx
./.ssh/known_hosts
./.ssh/id_rsa
./.ssh/id_rsa.pub
./anaconda-ks.cfg.1
./234/adxxx
./新建文本文档_(2).txt
./.viminfo
[[email protected] ~]# find ./ -type f |wc -l
18
[[email protected] ~]# vim 新建文本文档_\(2\).txt
[1]+ 已停止 vim 新建文本文档_\(2\).txt
[[email protected] ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 38G 1.5G 37G 4% /
devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 2.0G 8.6M 2.0G 1% /run
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sdb 20G 33M 20G 1% /mnt
/dev/sda1 497M 124M 373M 25% /boot
tmpfs 407M 0 407M 0% /run/user/0
[[email protected] ~]# w
21:23:44 up 35 min, 1 user, load average: 0.00, 0.01, 0.03
USER TTY FROM [email protected] IDLE JCPU PCPU WHAT
root pts/0 192.168.183.1 20:49 0.00s 0.11s 0.00s w
[[email protected] ~]# fg
vim 新建文本文档_\(2\).txt
[1]+ 已停止 vim 新建文本文档_\(2\).txt
[[email protected] ~]# fg
vim 新建文本文档_\(2\).txt
[1]+ 已停止 vim 新建文本文档_\(2\).txt
[[email protected] ~]# vim aa.txt
[2]+ 已停止 vim aa.txt
[[email protected] ~]# jods
-bash: jods: 未找到命令
[[email protected] ~]# jobs
[1]- 已停止 vim 新建文本文档_\(2\).txt
[2]+ 已停止 vim aa.txt
[[email protected] ~]# fg2
-bash: fg2: 未找到命令
[[email protected] ~]# fg 2
vim aa.txt
[[email protected] ~]# vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 3823284 876 165956 0 0 13 1 21 27 0 0 100 0 0
0 0 0 3823144 876 165988 0 0 0 0 61 87 0 0 100 0 0
0 0 0 3823144 876 165988 0 0 0 0 58 95 0 0 100
[[email protected] ~]# fgprocs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 3819456 876 166380 0 0 0 0 85 100 0 0 100 0 0
vmstat 1
0 0 0 3819456 876 166380 0 0 0 0 49 59 0 0 100 0 0
0 0 0 3819456 876 166380 0 0 0 0 29 42 0 0 100 0 0
0 0 0 3819456 876 166380 0 0 0 0 32 41 0 0 100 0 0
0 0 0 3819456 876 166380 0 0 0 0 30 44 0 0 100 0 0
^C
[[email protected] ~]# jobs
[[email protected] ~]# env
XDG_SESSION_ID=1
HOSTNAME=localhost.localdomain
TERM=xterm
SHELL=/bin/bash
HISTSIZE=10000
SSH_CLIENT=192.168.183.1 51247 22
SSH_TTY=/dev/pts/0
USER=root
MAIL=/var/spool/mail/root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
PWD=/root
LANG=zh_CN.UTF-8
HISTCONTROL=ignoredups
SHLVL=1
HOME=/root
LOGNAME=root
SSH_CONNECTION=192.168.183.1 51247 192.168.183.128 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/0
_=/usr/bin/env
[[email protected] ~]# set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_COMPLETION_COMPAT_DIR=/etc/bash_completion.d
BASH_LINENO=()
BASH_REMATCH=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="2" [2]="46" [3]="1" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION=‘4.2.46(1)-release‘
COLUMNS=92
COMP_WORDBREAKS=$‘ \t\n"\‘><=;|&(:‘
DIRSTACK=()
EUID=0
GROUPS=()
HISTCONTROL=ignoredups
HISTFILE=/root/.bash_history
HISTFILESIZE=10000
HISTSIZE=10000
HISTTIMEFORMAT=‘%Y/%m/%d %H:%M:%S‘
HOME=/root
HOSTNAME=localhost.localdomain
HOSTTYPE=x86_64
ID=0
IFS=$‘ \t\n‘
LANG=zh_CN.UTF-8
LESSOPEN=‘||/usr/bin/lesspipe.sh %s‘
LINES=41
MAILCHECK=60
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
fi
}
dequote ()
{
eval printf %s "$1" 2> /dev/null
}
quote ()
{
local quoted=${1//\‘/\‘\\\‘\‘};
printf "‘%s‘" "$quoted"
}
quote_readline ()
{
local quoted;
_quote_readline_by_ref "$1" ret;
printf %s "$ret"
}
[[email protected] ~]# set |grep 111
a=111
[[email protected] ~]# set |less
[[email protected] ~]# a1=2
[[email protected] ~]# echo $a1
2
[[email protected] ~]# a_1=3
[[email protected] ~]# echo $a_1
3
[[email protected] ~]# _a1=4
[[email protected] ~]# echo $_a1
4
[[email protected] ~]# 1aa=2
-bash: 1aa=2: 未找到命令
[[email protected] ~]# 2aa=3
-bash: 2aa=3: 未找到命令
[[email protected] ~]# a=a b c
-bash: b: 未找到命令
[[email protected] ~]# echo $a
111
[[email protected] ~]# a=‘a b c‘
[[email protected] ~]# echo $a
a b c
[[email protected] ~]# a=“a b c“
-bash: b: 未找到命令
[[email protected] ~]# a=“a b c”
-bash: b: 未找到命令
[[email protected] ~]# a=“a b c”
-bash: b: 未找到命令
[[email protected] ~]# a="a b c"
[[email protected] ~]# echo $a
a b c
[[email protected] ~]# a="a$bc"
[[email protected] ~]# echo $a
a
[[email protected] ~]# a=‘a$bc‘
[[email protected] ~]# echo $a
a$bc
[[email protected] ~]# a=1
[[email protected] ~]# b=2
[[email protected] ~]# echo $a$b
12
[[email protected] ~]# a=‘a$bc‘
[[email protected] ~]# echo $c
[[email protected] ~]# echo $a
a$bc
[[email protected] ~]# echo $a$b
a$bc2
[[email protected] ~]# c="a$bc"
[[email protected] ~]# echo $c
a
[[email protected] ~]# c="a$b"c
[[email protected] ~]# echo $c
a2c
[[email protected] ~]# c=‘a$b‘c
[[email protected] ~]# echo $c
a$bc
[[email protected] ~]# c=a"b"c
[[email protected] ~]# echo $c
abc
[[email protected] ~]# w
22:26:11 up 1:37, 2 users, load average: 0.00, 0.01, 0.04
USER TTY FROM [email protected] IDLE JCPU PCPU WHAT
root pts/0 192.168.183.1 20:49 3.00s 0.17s 0.00s w
root pts/1 192.168.183.1 22:25 18.00s 0.00s 0.00s -bash
[[email protected] ~]# echo ¥SSH_TTY
¥SSH_TTY
[[email protected] ~]# echo $SSH_TTY
/dev/pts/0
[[email protected] ~]# aming=liunx
[[email protected] ~]# echo #aming
[[email protected] ~]# echo #aming
[[email protected] ~]# aming=liunx
[[email protected] ~]# echo #aming
[[email protected] ~]# vim .bash_profile
[[email protected] ~]# ..bash_profile ^C
[[email protected] ~]# vim .bashrc
[[email protected] ~]# vim /etc/bashrc
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[[email protected] network-scripts]# echo $PS1
[\[email protected]\h \W]\$
[[email protected] network-scripts]# PS1=‘[\[email protected]\h \W]\$
[[email protected] network-scripts]#ls
ifcfg-ens33 ifdown-ipv6 ifdown-tunnel ifup-isdn ifup-TeamPort
ifcfg-lo ifdown-isdn ifup ifup-plip ifup-tunnel
ifcfg-有线连接_1 ifdown-post ifup-aliases ifup-plusb ifup-wireless
ifdown ifdown-ppp ifup-bnep ifup-post init.ipv6-global
ifdown-bnep ifdown-routes ifup-eth ifup-ppp network-functions
ifdown-eth ifdown-sit ifup-ib ifup-routes network-functions-ipv6
ifdown-ib ifdown-Team ifup-ippp ifup-sit
ifdown-ippp ifdown-TeamPort ifup-ipv6 ifup-Team
[[email protected] network-scripts]#
PS1=‘[\[email protected]\h \w]\$
‘
[[email protected] /etc/sysconfig/network-scripts]#
cd
[[email protected] ~]#
PS1=‘/
> ^C
[[email protected] ~]#
PS1=‘\[\033[01;32m\[email protected]\h\[/003[00m\]:[\033[01;36m\]\2\[\033[00m]\\$‘
[email protected]/003[00m:[\2]$
[email protected]/003[00m:[\2]$
原文地址:https://www.cnblogs.com/yikebaicai/p/8271067.html