Linux学习
一、管道符和作业控制
二、shell变量
三、环境变量配置文件
一、管道符和作业控制
- cat 1.txt |wc -l cat 1.txt |grep 'aaa'
- 管道符的作用就是把前面的命令输出的结果,交给后面的命令处理
- 作业控制 Ctrl + c、Ctrl +z、bg、fg
- jobs查看当前后台运行的命令
- sleep 1000
- jobs只能在当前终端下查看后台运行,可以使使用ps aux |grep sleep查看进程
操作
[root@centos01 ~]# cat 1.txt |wc -l
168
[root@centos01 ~]#
[root@centos01 ~]# cat 1.txt |grep 'nologin'
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
[root@centos01 ~]# top
top - 21:07:50 up 25 min, 1 user, load average: 0.00, 0.01, 0.01
Tasks: 97 total, 1 running, 96 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.1 us, 0.3 sy, 0.0 ni, 99.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1867292 total, 1588772 free, 115808 used, 162712 buff/cache
KiB Swap: 4194300 total, 4194300 free, 0 used. 1573384 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 128092 6696 3948 S 0.0 0.4 0:01.40 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
[root@centos01 ~]# jobs
[1]+ Stopped top
[root@centos01 ~]#
[root@centos01 ~]# ps aux |grep sleep
root 2216 0.0 0.0 112648 956 pts/0 S+ 21:11 0:00 grep --color=auto sleep
[root@centos01 ~]#
二、shell变量
- 变量
- PATH,HOME,PWD,LOGNAME
- env命令
- set命令多了很多变量,并且包括用户自定义的变量
- 自定义变量a=1
- 变量名规则:字母、数字、下划线,首位不能为数字
- 变量值有特殊符号时需要用单引号括起来
- 变量的累加
- 全局变量export b=2
- unset变量
- env查看系统的环境变量
- set查看所有的环境变量,也可自定义环境变量
- 可以使用set |grep 123查看环境变量,也可以使用set |less 然后/搜索a=1
- 变量名规则:字母、数字、下划线,首位不能为数字
- 变量值有特殊符号时需要用单引号括起来
操作
[root@centos01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos01 ~]# env
XDG_SESSION_ID=1
HOSTNAME=centos01
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=192.168.220.1 62072 22
SELINUX_USE_CURRENT_RANGE=
SSH_TTY=/dev/pts/0
USER=root
[root@centos01 ~]# set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:histappend:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="2" [2]="46" [3]="1" [4]="release" [5]="x86_64-redhat-linux-gnu")
[root@centos01 ~]# a=1
[root@centos01 ~]# echo $a
1
[root@centos01 ~]#
- 全局变量export b=2
- w查看当前系统有哪些用户在登录,使用echo $SSH_TTY查看当前用户属于哪个TTY
- 在当前终端1里面定义变量
- 在终端2里面是不生效的
- yum install -y *pstree
- bash 打开一个新的bash,然后运行pstree(进程树)
- exit回到sshd下的shell,输入echo $abc返回值为linux,说明定义的变量只在当前shell下生效(非全局变量或者本地变量)
- 定义全局变量 export ,向下生效(只对当前终端生效)
- unset abc取消变量
操作
[root@centos01 ~]# export b=2
[root@centos01 ~]# w
23:22:18 up 2:21, 2 users, load average: 0.01, 0.03, 0.03
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.220.1 20:42 2:00m 0.14s 0.14s -bash
root pts/1 192.168.220.1 23:18 2.00s 0.03s 0.01s w
[root@centos01 ~]# echo $SSH_TTY
/dev/pts/1
[root@centos01 ~]#
[root@centos01 ~]# abc=linux
[root@centos01 ~]# echo $abc
linux
[root@centos01 ~]#
[root@centos01 ~]# bash
[root@centos01 ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
├─agetty
├─auditd───{auditd}
├─chronyd
├─crond
├─dbus-daemon───{dbus-daemon}
├─firewalld───{firewalld}
├─irqbalance
├─master─┬─pickup
│ └─qmgr
├─polkitd───5*[{polkitd}]
├─rsyslogd───2*[{rsyslogd}]
├─sshd─┬─sshd───bash
│ ├─sshd───bash───bash───pstree
│ └─sshd───bash───bash
├─systemd-journal
├─systemd-logind
├─systemd-udevd
├─tuned───4*[{tuned}]
└─vmtoolsd───{vmtoolsd}
[root@centos01 ~]#
[root@centos01 ~]# echo $abc
[root@centos01 ~]# exit
exit
[root@centos01 ~]# !e
echo $abc
linux
[root@centos01 ~]#
[root@centos01 ~]# unset abc
[root@centos01 ~]# !e
echo $abc
[root@centos01 ~]#
三、环境变量配置文件
- /etc/profile用户环境变量,交互,登录才能执行(系统层次)
- /etc/bashrc用户不用登录,执行shell就生效(系统层次)
- ~/.bashrc(用户层次)
- ~/.bash_profile(用户层次)
- ~/.bash_history(用户层次)
- ~/.bash_logout(用户层次)
- source==.命令相似,加载更改后的配置文件
- ~/.bash_logout用户退出时需要做的操作
- PS1
- 使用echo $PS1查看定义
- PS2
操作
[root@centos01 ~]# vim .bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc // 此处的.和source相同
fi
~
[root@centos01 ~]# vim /etc/bashrc
# /etc/bashrc
# System wide functions and aliases
# Environment stuff goes in /etc/profile
# 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.
# are we an interactive shell?
if [ "$PS1" ]; then
if [ -z "$PROMPT_COMMAND" ]; then
case $TERM in
xterm*|vte*)
if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
elif [ "${VTE_VERSION:-0}" -ge 3405 ]; then
PROMPT_COMMAND="__vte_prompt_command"
else
PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
i
;;
screen*)
if [ -e /etc/sysconfig/bash-prompt-screen ]; then
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
else
PROMPT_COMMAND='printf "\033k%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
fi
;;
*)
[ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default
;;
esac
fi
# Turn on parallel history
shopt -s histappend
history -a
# Turn on checkwinsize
shopt -s checkwinsize
[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ " // 定义登录的用户名、主机名、当前所在目录
# You might want to have e.g. tty in prompt (e.g. more virtual machines)
# and console windows
# If you want to do so, just add e.g.
# if [ "$PS1" ]; then
# PS1="[\u@\h:\l \W]\\$ "
# fi
# to your custom modification shell script in /etc/profile.d/ directory
fi
[root@centos01 ~]# echo $PS1
[\u@\h \W]\$
[root@centos01 /etc/sysconfig/network-scripts]#PS1='[\u@\h \w]\$'
[root@centos01 /etc/sysconfig/network-scripts]#cd
[root@centos01 ~]#cd /etc/
[root@centos01 /etc]#cd /etc/sysconfig/
[root@centos01 /etc/sysconfig]#echo $PS1
[\u@\h \w]\$
[root@centos01 /etc/sysconfig]#
[root@centos01 /etc/sysconfig]#PS2='#'
[root@centos01 /etc/sysconfig]#\
#
[root@centos01 /etc/sysconfig]#