[2018.04.20 study notes] [linux basics 8.1-8.5]

8.1 Introduction to shell

A shell is a command interpreter that receives user instructions to interact with the operating system;

He has a specific grammar: such as logical judgments, looping statements, etc.

Each system user has its own shell, the default shell of CentOS is -bash (bourne Agin Shell)

The system also has zsh, ksh and so on.

Connecting to 192.168.87.130:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Last login: Fri Apr 20 00:00:10 2018 from 192.168.87.1
[root@lgs-01 ~]# shell
-bash: shell: 未找到命令

8.2 Command History

The Linux system records the historical commands entered by the user.

View command history: history command, which stores 1000 command history by default

[root@lgs-01 ~]# history
 #节省部分显示 
 976  rm -rf /var/cache/yum/x86_64/7/base/packages/zziplib-0.13.62-5.el7.x86_64.rpm 
  977  ls -l /var/cache/yum/x86_64/7/base/packages/
  978  yum reinstall -y zziplib --downloadonly
  979  ls -l /var/cache/yum/x86_64/7/base/packages/
  980  cd /usr/local/src/
  981  ls
  982  wget http://cn2.php.net/distributions/php-7.1.6.tar.bz2
  983  ls -l php-7.1.6.tar.bz2 
  984  tar jxvf php-7.1.6.tar.bz2 
  985  ls
  986  cd php-7.1.6
  987  ls
  988  ./configure --prefix=/usr/local/php7 --with-apxs2=/usr/local/apache2.4/bin/apxs --with-config-file-path=/usr/local/php7/etc --with-pdo-mysql=/usr/local/mysql/ -with-mysqli=/usr/local/mysql/bin/mysql_config --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif
  989  echo $?
  990  make
  991  echo $?
  992  make install
  993  echo $?
  994  /usr/local/apache2.4/bin/apachectl -M
  995  ls -l /usr/local/apache2.4/modules/libphp7.so 
  996  init 0
  997  shell
  998  history

The history of history is recorded in /root/.bash_history:

[root@lgs-01 ~]# ls -l /root/.bash_history 
-rw-------. 1 root root 23650 4月  20 01:11 /root/.bash_history

Clear command history: history -c

By default, 1000 command histories are recorded. You can modify the HISTSIZE parameter in the configuration file /etc/profile:

[root@lgs-01 ~]# echo $HISTSIZE 
1000
[root@lgs-01 ~]# cat /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

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
    umask 002
else
    umask 022
fi

for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then 
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge

Add the variable HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S" to display the command execution time

After modifying the configuration file, the source /etc/profile is reloaded to take effect

[root@lgs-01 ~]# cat /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.
#节省部分内容
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
[root@lgs-01 ~]# source /etc/profile
[root@lgs-01 ~]# history
  995  2018/04/20 13:33:01ls -l /usr/local/apache2.4/modules/libphp7.so 
  996  2018/04/20 13:33:01init 0
  997  2018/04/20 14:20:48shell
  998  2018/04/20 14:23:11history
  999  2018/04/20 14:25:22historyls -l /etc/.bash_history
 1000  2018/04/20 14:25:27ls -l /etc/.bash_history
 1001  2018/04/20 14:25:46ls -l /root/.bash_history 
 1002  2018/04/20 14:27:11cat /root/.bash_history 
 1003  2018/04/20 14:27:41vim /root/.bash_history 
 1004  2018/04/20 14:31:30echo $HISTSIZE 
 1005  2018/04/20 14:32:03cat /etc/profile
 1006  2018/04/20 14:35:16vim /etc/profile
 1007  2018/04/20 14:36:43source /etc/profile
 1008  2018/04/20 14:36:46ls
 1009  2018/04/20 14:36:55history

Execute the previous command: !!

[root@lgs-01 ~]# !!
ls
123.zip                                b                                          mesa-libGL-devel-17.0.1-6.20170307.el7.x86_64.rpm
1.txt.bak                              c                                          mesa-libGLU-9.0.0-4.el7.x86_64.rpm

Execute the first command in the command history: !n, where n is a number

 1010  2018/04/20 14:39:18 cat /etc/profile
 1011  2018/04/20 14:40:04 source /etc/profile
 1012  2018/04/20 14:40:45 ls
 1013  2018/04/20 14:41:54 history
[root@lgs-01 ~]# !1011
source /etc/profile

Execute the most recent command starting with the content of a character: !echo

 1004  2018/04/20 14:31:30 echo $HISTSIZE 
 1005  2018/04/20 14:32:03 cat /etc/profile
 1006  2018/04/20 14:35:16 vim /etc/profile
 1007  2018/04/20 14:36:43 source /etc/profile
 1008  2018/04/20 14:36:46 ls
 1009  2018/04/20 14:36:55 history
 1010  2018/04/20 14:39:18 cat /etc/profile
 1011  2018/04/20 14:40:04 source /etc/profile
 1012  2018/04/20 14:40:45 ls
 1013  2018/04/20 14:41:54 history
[root@lgs-01 ~]# !1011
source /etc/profile
[root@lgs-01 ~]# !echo
echo $HISTSIZE 
1000

8.3 Command completion and aliases

The Linux system provides a command input completion function to improve the efficiency of user input commands.

Requires installation: bash-completion

When entering a string at the beginning of a command, file or directory: press tab completion:

Press tab: when the beginning string matches only one command, file or directory, press tab to complete

# [root@lgs-01 ~]# logo 此处按一下tab,即可补全命令 logout
[root@lgs-01 ~]# logout

Press tab twice: when the beginning string matches more than one command, file or directory, press tab twice to list all the commands, files or directories of the beginning string, and then continue to enter subsequent characters according to the prompts.

[root@lgs-01 ~]# log
logger     login      loginctl   logname    logout     logrotate  logsave

When a command is very long, you can also alias the command to improve input efficiency

Use the alias command: for example, the ls command, which is an alias of the ls command with color display options

[root@lgs-01 ~]# which ls
alias ls='ls --color=auto'
	/usr/bin/l
[root@lgs-01 ~]# alias restartnet='systemctl restart network.service '
[root@lgs-01 ~]# res
reset       resize2fs   resizecons  resizepart  restartnet  restorecon  
[root@lgs-01 ~]# restartnet 
[root@lgs-01 ~]# which restartnet
alias restartnet='systemctl restart network.service '
	/usr/bin/systemctl

View all aliases in the system:

[root@lgs-01 ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias restartnet='systemctl restart network.service '
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

Some system aliases are defined in the /.bashrc configuration file in the user's home directory, and the rest are in

Script files in the /etc/profile.d/ directory

[root@lgs-01 ~]# cat /root/.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
fi
[root@lgs-01 ~]# ls /etc/profile.d/
256term.csh  colorgrep.csh  colorls.csh  gnome-ssh-askpass.csh  lang.csh  less.csh  vim.csh  which2.csh
256term.sh   colorgrep.sh   colorls.sh   gnome-ssh-askpass.sh   lang.sh   less.sh   vim.sh   which2.sh

8.4 Wildcards for Commands

Any number of characters or 0: *

[root@lgs-01 ~]# ls *.bak
1.txt.bak  2_hard.txt.bak  2.txt.bak

[root@lgs-01 ~]# ls 333*.tar
333.tar

single character:

[root@lgs-01 ~]# ls 33?.tar
333.tar

[ ] single character within brackets

[root@lgs-01 ~]# ls *.txt
k.txt  zb.txt
[root@lgs-01 ~]# ls [a-zA-Z].txt
k.txt

[ ] Select a single character in brackets

[root@lgs-01 ~]# ls *.txt
k.txt  w.txt  zb.txt
[root@lgs-01 ~]# ls [lkw].txt
k.txt  w.txt

{ } A field within curly braces, separated by commas

[root@lgs-01 ~]# ls *.txt
abcd.txt  ape.txt  k.txt  los.txt  w.txt  zb.txt
[root@lgs-01 ~]# ls {los,k,sss,ape}.txt
ls: 无法访问sss.txt: 没有那个文件或目录
ape.txt  k.txt  los.txt

8.5 Input and output redirection

Output redirection, overwriting the content of the original file: >

[root@lgs-01 ~]# echo 1234 >w.txt 
[root@lgs-01 ~]# cat w.txt 
1234
[root@lgs-01 ~]# echo 0008 >w.txt 
[root@lgs-01 ~]# cat w.txt 
0008

Append redirect, only append content, not overwrite: >>

[root@lgs-01 ~]# cat w.txt 
0008
[root@lgs-01 ~]# echo 99999 >>w.txt 
[root@lgs-01 ~]# cat w.txt 
0008
99999

Error message output redirection: 2>

[root@lgs-01 ~]# laaaa
-bash: laaaa: 未找到命令
[root@lgs-01 ~]# laaaa 2>k.txt 
[root@lgs-01 ~]# cat k.txt 
-bash: laaaa: 未找到命令

Error message additional redirection: 2>>

[root@lgs-01 ~]# laaaa 2>k.txt 
[root@lgs-01 ~]# cat k.txt 
-bash: laaaa: 未找到命令
[root@lgs-01 ~]# kkkkk 2>>k.txt 
[root@lgs-01 ~]# cat k.txt 
-bash: laaaa: 未找到命令
-bash: kkkkk: 未找到命令

Correct and false output redirection: &>

[root@lgs-01 ~]# ls *.txt 2>k.txt abcccc.txt &>k.txt 
[root@lgs-01 ~]# cat k.txt 
ls: 无法访问abcccc.txt: 没有那个文件或目录
abcd.txt
ape.txt
k.txt
los.txt
w.txt
zb.txt

True and false append redirects are also supported: &>>

Correct and error outputs are redirected separately:

[root@lgs-01 ~]# cat w.txt 
0008
99999
[root@lgs-01 ~]# cat k.txt 
111
[root@lgs-01 ~]# ls *.txt ppp.txt >w.txt 2>k.txt 
[root@lgs-01 ~]# cat w.txt 
abcd.txt
ape.txt
k.txt
los.txt
w.txt
zb.txt
[root@lgs-01 ~]# cat k.txt 
ls: 无法访问ppp.txt: 没有那个文件或目录

Input redirection: < Input the content of the file on the right to the command on the left, and the left can only be a command

[root@lgs-01 ~]# cat w.txt 
abcd.txt
ape.txt
k.txt
los.txt
w.txt
zb.txt
[root@lgs-01 ~]# wc -l <w.txt 
6

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325451236&siteId=291194637