Chapter 10 10.1 what is a shell

Posted by BinaryBird on Fri, 11 Feb 2022 05:22:45 +0100

10.1 what is a shell

(1) What is Shell

  • Shell is the medium used when the system interacts with computer hardware. It is just a tool of the system
  • In fact, there is another layer between the system and computer hardware - the system kernel
    (1_1) record command history
  • The commands we have executed in Linux will be recorded. By default, 1000 historical commands can be recorded. These commands are saved in the user's home directory bash_ In the history file.
  • However, it should be noted that only when the user exits the current shell normally, the commands running in the current shell will be saved to bash_ In history
    “!” It is a special character related to the command history. The common application of high character consists of the following three characters
  • !!: Indicates the execution of the previous instruction. The example code is as follows:
[root@localhost ~]# pwd
[root@localhost ~]# !!
  • ! n: "N" here refers to a number, which indicates the nth instruction in the execution command history. For example, "! 1002" indicates the 1002nd instruction in the execution command history. As shown in the figure below, it is an example of executing the 100th instruction:
[root@localhost ~]# history |grep 100
  100  ls /mnt/
  694  history |grep 1002
  696  history |grep 100
[root@localhost ~]# !100
ls /mnt/
  • ! String (string greater than or equal to 1): for example "! pw "indicates the last command starting with" pw "in the command execution history
[root@localhost ~]# !pw

(2) Command and file name completion

Previously, we learned that pressing a tab key can help us complete an instruction, a path or a file name. If you press the tab key twice in a row, the system will list all commands or file names
(2_1) alias

  • We seem to have seen alias before. It is one of the unique functions of bash. We can alias a commonly used instruction with a long name as a relatively simple and easy to remember instruction through alias
  • Of course, if we don't use it, we can use the unalias command to remove the alias function
  • Directly execute the alias command to see the preset alias of the system:
[root@localhost ~]# 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 rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
  • We can also use our own command alias. The specific format is: alias named alias = 'specific command'. Let's do a simple operation below
[root@localhost ~]# alias lym='pwd'
[root@localhost ~]# lym
[root@localhost ~]# unalias lym
[root@localhost ~]# lym
bash: lym: Command not found...

(3) Wildcard

In bash, we can use * to match zero or more characters; use? Match one character. The specific operations are as follows:

[root@localhost ~]# ls -d /etc/yum*
/etc/yum  /etc/yum.conf  /etc/yum.repos.d

(4) Input / output redirection

  • Input redirection: used to change the input of a command
  • Output redirection: used to change the output of the command (output redirection is more commonly used). Output redirection is to enter the result of the command into a file rather than on the screen
  • Enter the redirected command:<“
  • Output redirected command: ">"; In addition, the command of error redirection is: "2 >"; Append redirection command: "> >
[root@localhost ~]# echo "123">1.txt
[root@localhost ~]# echo "123">>1.txt
[root@localhost ~]# cat 1.txt

(5) Pipe symbol

We have seen the pipeline symbol before: |, which takes the output of one instruction as the input of the next instruction

[root@localhost ~]# cat /etc/passwd
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
libstoragemgmt:x:998:996:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
colord:x:997:995:User for colord:/var/lib/colord:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
saned:x:996:993:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
saslauth:x:995:76:Saslauthd user:/run/saslauthd:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
unbound:x:992:987:Unbound DNS resolver:/etc/unbound:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
geoclue:x:991:985:User for geoclue:/var/lib/geoclue:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
gluster:x:990:984:GlusterFS daemons:/run/gluster:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
[root@localhost ~]# cat /etc/passwd|wc -l

(6) Job control

When we run a process, we can use Ctrl+Z to pause it, and then use fg command to restore it, or use bg command to make it go to the background, or use Ctrl+C command to terminate it. The example code is as follows

Insert code slice here

Use the vi command to edit test Txt file. After inputting some content casually, we press Esc and then use Ctrl+Z to pause it. The following situation will appear

As shown in the figure above, the editing of test has been stopped at this time Txt file, and then we can use the fg command to restore it. After entering fg, we enter the editing interface just now. We pause it again. The specific command is the same as above, and then we enter jobs to see the suspended or running tasks in the background

If you want the suspended task to run in the background, you can use the bg command

As can be seen from the above figure, vi doesn't seem to support running in the background. Let's try another command

How should we turn off tasks running in the background? If we haven't just exited the shell, we should first use the command fg number to move the task to the foreground, and then use Ctrl+c to end the task. The details are as follows:

We can see that task number 2 has been turned off. Of course, we can also kill the process through their pid. The details are as follows:

First find their pid through the command. In the figure above, they are 18021 and 23878 respectively. Then use the kill command to kill them directly.

Topics: Linux shell