Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

8.1 shell introduction, 8.2 command history, 8.3 command completion and aliases, 8.4 wildcards, 8.5 input and output redirection

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

8.1 sehll introduces what shellshell is a command interpreter that provides interaction between the user and the machine to support specific syntax, such as logical judgment, loops. Each user can have their own specific shell.centos7 default shell of bash (Bourne Agin shell) and zsh, ksh, etc. 8.2 command history / root/.bash_history; command history placement file [root@jzplinux-01 ~] # ls / root/.bash_history/root/.bash_ commands [root @ jzplinux-01 ~] # cat! $cat / root/.bash_historyhistory To view the commands stored in the system, you can store up to 1000 [root@jzplinux-01 ~] # history | tail 995 make install 996 echo $? 997 ls / usr/local/apache2 998 init 0999 ls / root/.bash_history 1000 cat $! 1001 ls / root/.bash_history 1002 cat / root/.bash_history 1003 history 1004 history | tail [root @ jzplinux-01] #

You can see that there are 1004 entries because they have not yet been written to the file and are temporarily in memory.

Echo $HISTSIZE; check the value of the variable [root@jzplinux-01 ~] # echo $HISTSIZE1000 [root @ jzplinux-01 ~] # history-c Emptying the history command in memory does not empty the command [root@jzplinux-01 ~] # history-c [root @ jzplinux-01 ~] # history 8 history [root@jzplinux-01 ~] # cat .bash _ history | tailcd.. lscd httpd-2.2.32 lsmakeecho $? make installecho $? ls / usr/local/apache2init 0 [root@jzplinux-01 ~] # when exiting the current terminal Commands saved in memory will be saved to / root/.bash_history file to vim / etc/profile Modify the value of the HISTTORY variable in the configuration file [root@jzplinux-01 ~] # vim / etc/profileHOSTNAME= `/ usr/bin/hostname 2 > / dev/ null`HISTSIZE = 2000if ["$HISTCONTROL" = "ignorespace"]; then export HISTCONTROL=ignorebothsource / etc/profile After reloading the file, the value of the HISTSIZE variable changes [root@jzplinux-01 ~] # echo $HISTSIZE1000 [root @ jzplinux-01 ~] # source / etc/ profile [root @ jzplinux-01 ~] # echo $HISTSIZE2000 [root @ jzplinux-01 ~] # HISTTIMEFORMAT= "% Y/%m/%d% H:%M:%S" Set the command history display format [root@jzplinux-01 ~] # HISTTIMEFORMAT= "% Y/%m/%d% H:%M:%S" [root@jzplinux-01 ~] # echo $HISTTIMEFORMAT%Y/%m/%d% history% M:% S [root @ jzplinux-01 ~] # history 8 2017-06-25 12:27:07 history 9 2017-06-25 12:31:05 cat .bash _ history 10 2017-06-25 12:31:14 cat .bash _ history | tail 11 2017-06-25 12:35:08 cat / etc/profile 12 2017-06-25 13:10:00 vim / etc/profile 13 2017-06-25 13:14:38 echo $HISTORY 14 2017-06-25 13:15:34 echo $HISTTORY 15 2017-06-25 13:16:10 echo $HISTSIZE 16 2017 HISTTORY 25 13:16:47 source / etc/profile 17 2017 source 25 13:17:13 echo $HISTSIZE 18 2017 HISTTORY 25 13:17:13 13:19:55 history 19 2017-06-25 13:25:16 HISTTIMEFORMAT= "% Y/%m/%d% H:%M:%S" 20 2017-06-25 13:25:37 echo $HISTTIMEFORMAT 21 2017-06-25 13:25:49 effective [root @ jzplinux-01 ~] # only on the current terminal If you want to take effect in other terminals, you need to add variables to / etc/profile [root@jzplinux-01 ~] # vim / etc/profileHISTSIZE=2000HISTTIMEFORMAT= "% Y/%m/%d% H:%M:%S" [root@jzplinux-01 ~] # source / etc/profilechattr + a / root/.bash_history Set a permission to the file to prevent it from being deleted [root@jzplinux-01 ~] # chattr + a / root/.bash_ permission [root @ jzplinux-01 ~] # lsattr / root/.bash_history-a- / root/.bash_ permission [root @ jzplinux-01 ~] #! Indicates the execution of the previous command That is, the last command in command history [root@jzplinux-01 ~] # ls111.txt 1.txt 1.txt.1 2.txt 3.txt anaconda-ks.cfg httpd-2.2.32.tar.gz [root@jzplinux-01 ~] #! ls111.txt 1.txt 1.txt.1 2.txt 3.txt anaconda-ks.cfg httpd-2.2.32.tar.gz [root@jzplinux-01 ~] # w 13:43:44 up 5:12, 2 users Load average: 0.00,0.01, 0.05USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATroot pts/0 192.168.86.1 08:32 8.00s 0.21s 0.06s wroot pts/1 192.168.86.1 13:33 9:04 0.03s-bash [root @ jzplinux-01 ~] #! W 13:43:46 up 5:12, 2 users, load average: 0.05USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATroot pts/0 192.168.86.1 08:32 2.00s 0.16s 0.01s wroot pts/1 192.168.86.1 13:33 9:06 0.03s 0.03s-bash [root @ jzplinux-01 ~] #! n Indicates the nth order in the history of executing an order. N means several commands [root@jzplinux-01 ~] # history 8 2017-06-25 12:27:07 history 9 2017-06-25 12:31:05 cat .bash _ history 10 2017-06-25 12:31:14 cat .bash _ history | tail 11 2017-06-25 12:35:08 cat / etc/profile 12 2017-06-25 13:10:00 vim / etc/profile 13 2017 etc/profile 25 13:14:38 echo $HISTORY 14 2017 tail 06 / 25 13:15:34 echo $HISTTORY 15 2017-06-25 13:16:10 echo $HISTSIZE 16 2017-06-25 13:16:47 source / etc/profile 17 2017-06-25 13:17:13 echo $HISTSIZE 18 2017-06-25 13:19:55 history 19 2017-06-25 13:25:16 HISTTIMEFORMAT= "% Y/%m/%d% H:%M:%S" 20 2017 HISTTIMEFORMAT 25 13:25:37 echo $HISTTIMEFORMAT 21 2017 HISTTIMEFORMAT 25 13:25:49 history 22 2017-06-25 13:31:12 vim / etc/profile 23 2017-06-25 13:33:21 source / etc/profile 24 2017-06-25 13:39:08 chattr + a / root/.bash_history 25 2017-06-25 13:39:38 lsattr / root/.bash_history 26 2017-06-25 13:43:18 ls 27 2017x06xx25 13:43:40 W 28 2017x06xx25 13:43:40 W 28 2017x06xxx 2513 etc/profile 43w 29 2017-06-25 13:45:46 roomy [jzplinux-01 @ root ~] #! 17echo $HISTSIZE2000 [root @ jzplinux-01 ~] #! + string That starts with a string The most recent command in history [root@jzplinux-01 ~] # history | tail-n 7 29 2017-06-25 13:45:46 history 30 2017-06-25 13:46:00 echo $HISTSIZE 31 2017-06-25 13:50:11 w 32 2017-06-25 13:53:03 history | tail 33 13:53:18 history on 2017-06-25 | tail-n3 34 2017 history 25 13:53:32 history | tail-n3 35 2017 tail 06 / 25 13:53:47 history | tail-n 7 [root@jzplinux-01 ~] # [root@jzplinux-01 ~] #! echoecho $HISTSIZE2000 [root @ jzplinux-01 ~] #! ww 13:50:11 up 5:19 2 users, load average: 0.00,0.01, 0.05USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATroot pts/0 192.168.86.1 08:32 3.00s 0.17s 0.01s wroot pts/1 192.168.86.1 13:33 15:31 0.03s-bash [root @ jzplinux-01] # 8.3Command completion and alias tab key command completion

Knock on the completion command or path

Double-click to list all the files in the current directory

Yum install-y bash-completion; after installing the package, the tab key can complete the parameters of the command

After installation, the system needs to be restarted to take effect.

[root@jzplinux-01 ~] # rpm-Q bash-completionbash-completion-2.1-6.el7.noarchs [root @ jzplinux-01 ~] # systemctl restart networkalias restartnet='systemctl restart network.service'; set alias [root ~] # alias restartnet='systemctl restart network.service' [root@jzplinux-01 ~] # restartnet [root @ jzplinux-01 ~] # alias List all aliases of the system [root@jzplinux-01 ~] # aliasalias 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' [root@jzplinux-01 ~] # / root/.bashrc / etc/profile.d Files that define alias

/ root/.bashrc

[root@jzplinux-01] # cat / root/.bashrc#. Bashrc # User specific aliases and functionsalias rm='rm-i'alias cp='cp-i'alias mv='mv-iTunes # Source global definitionsif [- f / etc/bashrc]; then. / etc/ bashrcfi[ root @ jzplinux-01 ~] #

In the script file under / etc/profile.d

[root@jzplinux-01 ~] # ls / etc/profile.d256term.csh colorgrep.csh colorls.sh less.csh vim.sh356term.sh colorgrep.sh lang.csh less.sh which3.cshbash_completion.sh colorls.csh lang.sh vim.csh which3.shunalias restartnet Cancel the custom alias [root@jzplinux-01 ~] # unalias restartnet [root @ jzplinux-01 ~] # restartnet-bash: restartnet: command [root@jzplinux-01 ~] # 8.4 wildcard * any arbitrary character [root@jzplinux-01 ~] # ls * .txt111.txt 1.txt 2.txt 3.txt [root@jzplinux-01 ~] # ls * txt*111.txt 1.txt 1.txt.1 2.txt 3.txt [ Root@jzplinux-01 ~] # ls 1*111.txt 1.txt 1.txt.1 [root @ jzplinux-01 ~] #? Represents an arbitrary character [root@jzplinux-01 ~] # ls? .txt1.txt 2.txt 3.txt [root @ jzplinux-01 ~] # ls [1-9] .txt List the files containing numbers [0-9], [123], [23], [Amurz], [Amurz], [0-9a-zA-Z] [root@jzplinux-01 ~] # ls [0-9] .txt1.txt 2.txt 3.txt [root @ jzplinux-01 ~] # le [23] .txt-bash: le: the command [root@jzplinux-01 ~] # ls [23] .txt2.txt 3.txt [root @ jzplinux-01 ~] # ls {1r2 was not found 3} .txt List the files containing curly braces [root@jzplinux-01 ~] # txt {1pr 2} .txt1.txt 2.txt [root @ jzplinux-01 ~] # ls {1JI 3} .txt1.txt 2.txt 3.txt [root @ jzplinux-01 ~] # 8.5 input and output redirection cat 1.txt > 2.txt Redirect Redirect the result of the previous command to a later file [root@jzplinux-01 ~] # cat 2.txt1234567890 [root @ jzplinux-01 ~] # cat 1.txtchrony:x:997:995::/var/lib/chrony:/sbin/nologinaming:x:1000:1000::/home/aming:/bin/bashuser1:x:1001:1001::/home/user1:/bin/bashtcpdump:x:72:72::/:/sbin/nologinnginx:x:996:994:Nginx Web server:/var/lib/nginx:/sbin/ science [root @ jzplinux-01 ~] # cat 1.txt > 2.txt [root @ jzplinux-01 ~] # cat 2.txtchrony:x:997:995::/var/lib/chrony:/sbin/nologinaming:x:1000:1000::/home/aming:/bin/bashuser1:x:1001:1001::/home/user1:/bin/bashtcpdump:x:72:72::/:/sbin/nologinnginx:x:996:994:Nginx web Server:/var/lib/nginx:/sbin/ science [root @ jzplinux-01 ~] # cat 1.txt > > 2.txt Pursuit of weight orientation Append the output of the previous command to the following file [root@jzplinux-01 ~] # cat 1.txtchrony:x:997:995::/var/lib/chrony:/sbin/nologinaming:x:1000:1000::/home/aming:/bin/bashuser1:x:1001:1001::/home/user1:/bin/bashtcpdump:x:72:72::/:/sbin/nologinnginx:x:996:994:Nginx web server:/var/lib/nginx:/sbin/nologin [ Root@jzplinux-01 ~] # cat 2.txtchrony:x:997:995::/var/lib/chrony:/sbin/nologinaming:x:1000:1000::/home/aming:/bin/bashuser1:x:1001:1001::/home/user1:/bin/bashtcpdump:x:72:72::/:/sbin/nologinnginx:x:996:994:Nginx web server:/var/lib/nginx:/sbin/ science [root @ jzplinux-01 ~] # cat 1.txt > > 2.txt [root @ jzplinux-01 ~] # cat 2.txtchrony:x:997:995::/var/lib/chrony:/sbin/nologinaming:x:1000:1000::/home/aming:/bin/bashuser1:x:1001:1001::/home/user1:/bin/bashtcpdump:x:72:72::/:/sbin/nologinnginx:x:996:994:Nginx web server:/var/lib/nginx:/sbin/nologinchrony:x:997:995::/var/lib/chrony:/sbin/nologinaming: X:1000:1000::/home/aming:/bin/bashuser1:x:1001:1001::/home/user1:/bin/bashtcpdump:x:72:72::/:/sbin/nologinnginx:x:996:994:Nginx web server:/var/lib/nginx:/sbin/ science [root @ jzplinux-01 ~] # lsaa 2 > a.txt Error redirect [root@jzplinux-01 ~] # touch a.txt [root @ jzplinux-01 ~] # lsaaa-bash: lsaaa: command not found [root@jzplinux-01 ~] # lsaaa 2 > a.txt [root @ jzplinux-01 ~] # cat a.txt-bash: lsaaa: command not found [root@jzplinux-01 ~] # lsaa 2 > > a.txt Append error redirection [root@jzplinux-01 ~] # cat a.txt-bash: lsaaa: command not found [root@jzplinux-01 ~] # lsaaa 2 > > a.txt [root @ jzplinux-01 ~] # lsaaa 2 > > a.txt [root @ jzplinux-01 ~] # cat a.txt-bash: lsaaa: command not found-bash: lsaaa: command not found-bash: lsaaa: command not found [root@jzplinux-01 ~] # ls [23] .txt aaa.txt & > a.txt & > equivalent to 2 > + > [root@jzplinux-01 ~] # ls [23] .txt aaa.txt & > a.txt [root @ jzplinux-01 ~] # cat a.txtls: unable to access aaa.txt: there is no file or directory 2.txt3.txt [root @ jzplinux-01 ~] # ls [23] .txt aaa.txt & > > a.txt & > > equivalent to 2 > > + > > [root@jzplinux-01 ~] # ls [23] .txt aaa.txt & > a.txt [root @ jzplinux-01 ~] # cat a.txtls: unable to access aaa.txt: without that file or directory 2.txt3.txtls: unable to access aaa.txt: without that file or directory 2.txt3.txt [root @ jzplinux-01 ~] # ls [23]. Txt aaa.txt > 1.txt 2 > a.txt Output correct output and error to different files [root@jzplinux-01 ~] # ls [23] .txt aaa.txt > 1.txt2 > a.txt [root @ jzplinux-01 ~] # cat 1.txt2.txt3.txt3.txt [root @ jzplinux-01 ~] # cat a.txtls: unable to access aaa.txt: there is no such file or directory [root ~] # wc-l

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report