Kevin_306

02Linux入门-命令练习 who echo hostname alias history 20210111

Linux入门 

1.who 、w

Linux中who命令用于显示系统中有哪些使用者正在上面,显示的资料包含了使用者 ID、使用的终端机、从哪边连上来的、上线时间、呆滞时间、CPU 使用量、动作等等。

Linux中w命令用于显示目前登入系统的用户信息。

2 shell 

Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。 

Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。

zsh :Mac OS默认的shell。

2.1显示当前使用的shell (代码分别为centos7、centos8、Ubuntu)

echo $SHELL

[root@centos7 ~]# echo $SHELL
/bin/bash
[root@8 ~]# echo $SHELL
/bin/bash
kevin@kevin:~$ echo $SHELL
/bin/bash
kevin@kevin:~$ 

2.2显示当前系统使用的所有shell (代码分别为centos7、centos8、Ubuntu)

1 [root@centos7 ~]# cat /etc/shells
2 /bin/sh
3 /bin/bash
4 /usr/bin/sh
5 /usr/bin/bash
6 [root@localhost ~]#
1 [root@centos8 ~]# cat /etc/shells
2 /bin/sh
3 /bin/bash
4 /usr/bin/sh
5 /usr/bin/bash
6 [root@centos8 ~]# 
 1 kevin@kevin:~$ cat /etc/shells
 2 # /etc/shells: valid login shells
 3 /bin/sh
 4 /bin/bash
 5 /usr/bin/bash
 6 /bin/rbash
 7 /usr/bin/rbash
 8 /bin/dash
 9 /usr/bin/dash
10 /usr/bin/tmux
11 /usr/bin/screen
12 kevin@kevin:~$ 

3.hostname

使用hostname时候不要使用_下划线来命名

举例:临时修改主机名如下

 如要长久保存使用:

[root@centos8 ~]# hostnamectl set-hostname webserver.magedu.org 

 退出后登陆查看:

 新名字已生效,这个命令写入文件中保存了

 

 4.PS1

临时设置,重启无效 

永久办法:

生效结果

#vim /etc/profile.d/ps1.sh 内容: PS1='\[\e[1;32m\][\[\e[0m\]\t \[\e[1;33m\]\u\[\e[36m\]@\h\[\e[1;31m\] \W\[\e[1;32m\]]\[\e[0m\]\$' ESC-:wq(保存退出) #source /etc/profile.d/ps1.sh

 5.内部命令

别名优先级高    其次内部   最后外部

[root@centos8 ~]#enable |wc -l
61

 

  6.alias命令

     alias命令用来设置指令的别名。我们可以使用该命令可以将一些较长的命令进行简化。使用alias时,用户必须使用单引号''将原来的命令引起来,防止特殊字符导致错误。

alias命令的作用只局限于该次登入的操作。若要每次登入都能够使用这些命令别名,则可将相应的alias命令存放到bash的初始化文件/etc/bashrc中。

语法:alias(选项)(参数)
选项: -p:打印已经设置的命令别名。

 1 [root@centos7 ~]# alias
 2 alias cp='cp -i'
 3 alias egrep='egrep --color=auto'
 4 alias fgrep='fgrep --color=auto'
 5 alias grep='grep --color=auto'
 6 alias l.='ls -d .* --color=auto'
 7 alias ll='ls -l --color=auto'
 8 alias ls='ls --color=auto'
 9 alias mv='mv -i'
10 alias rm='rm -i'
11 alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
12 [root@centos7 ~]# 
 1 [root@centos8 ~]# alias
 2 alias cp='cp -i'
 3 alias egrep='egrep --color=auto'
 4 alias fgrep='fgrep --color=auto'
 5 alias grep='grep --color=auto'
 6 alias l.='ls -d .* --color=auto'
 7 alias ll='ls -l --color=auto'
 8 alias ls='ls --color=auto'
 9 alias mv='mv -i'
10 alias rm='rm -i'
11 alias which='(alias; declare -f) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot'
12 alias xzegrep='xzegrep --color=auto'
13 alias xzfgrep='xzfgrep --color=auto'
14 alias xzgrep='xzgrep --color=auto'
15 alias zegrep='zegrep --color=auto'
16 alias zfgrep='zfgrep --color=auto'
17 alias zgrep='zgrep --color=auto'
18 [root@centos8 ~]# 
1 kevin@kevin:~$ alias
2 alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
3 alias egrep='egrep --color=auto'
4 alias fgrep='fgrep --color=auto'
5 alias grep='grep --color=auto'
6 alias l='ls -CF'
7 alias la='ls -A'
8 alias ll='ls -alF'
9 alias ls='ls --color=auto'

7.命令行扩展: $()的使用 

当你希望一个命令调用另外一个命令显示结果的时候可以使用

 1 [root@centos8 data]# date +%F
 2 2021-01-12
 3 [root@centos8 data]# touch 2021-01-12 (#手滑回车了,这个2021-01-12文件生成了)
 4 [root@centos8 data]# touch 2021-01-12.log
 5 [root@centos8 data]# ls
 6 2021-01-12  2021-01-12.log
 7 [root@centos8 data]# rm -f 2021-01-12.log (#删除多余的.log文件)
 8 [root@centos8 data]# ls
 9 2021-01-12
10 [root@centos8 data]# touch $(date +%F).log
11 [root@centos8 data]# ls
12 2021-01-12  2021-01-12.log
13 [root@centos8 data]# ll
14 total 0
15 -rw-r--r--. 1 root root 0 Jan 12 01:09 2021-01-12
16 -rw-r--r--. 1 root root 0 Jan 12 01:11 2021-01-12.log
17 [root@centos8 data]# 

8.1生成日期的.txt文本 

 1 [root@centos8 data]# rm -f 2021-01-12
 2 [root@centos8 data]# ll
 3 total 0
 4 -rw-r--r--. 1 root root 0 Jan 12 01:11 2021-01-12.log
 5 [root@centos8 data]# touch `(date +%F`.txt
 6 -bash: command substitution: line 2: syntax error: unexpected end of file
 7 [root@centos8 data]# touch `date +%F`.txt
 8 [root@centos8 data]# ll
 9 total 0
10 -rw-r--r--. 1 root root 0 Jan 12 01:11 2021-01-12.log
11 -rw-r--r--. 1 root root 0 Jan 12 01:17 2021-01-12.txt
12 [root@centos8 data]# 

8.2生成主机名+日期的日志 

touch `hostname`-`date +%F`.log
1 [root@centos8 data]# touch `hostname`-`date +%F`.log
2 [root@centos8 data]# ls
3 2021-01-12.log  2021-01-12.txt  centos8.kevin306.cn-2021-01-12.log
4 [root@centos8 data]# ll
5 total 0
6 -rw-r--r--. 1 root root 0 Jan 12 01:11 2021-01-12.log
7 -rw-r--r--. 1 root root 0 Jan 12 01:17 2021-01-12.txt
8 -rw-r--r--. 1 root root 0 Jan 12 01:20 centos8.kevin306.cn-2021-01-12.log

举例:

 1 [root@centos8 data]# echo “This system‘s name is $(hostname)”
 2 “This system‘s name is centos8.kevin306.cn”
 3 [root@centos8 data]# echo This system‘s name is $(hostname)
 4 This system‘s name is centos8.kevin306.cn
 5 [root@centos8 data]# 
 6 
 7 [root@centos8 data]# echo “i am $(who)”
 8 “i am root pts/0 2021-01-12 00:26 (192.168.0.102)”
 9 [root@centos8 data]# echo “i am $(whoami)”
10 “i am root”
11 [root@centos8 data]# echo i am $(whoami)
12 i am root
13 [root@centos8 data]# 

9.括号{}扩展

1 [root@centos8 data]# echo {A-Z}
2 {A-Z}
3 [root@centos8 data]# echo {A..Z}
4 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
5 [root@centos8 data]# echo {A..z}
6 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [  ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z
7 [root@centos8 data]# echo {a..Z}
8 a ` _ ^ ]  [ Z
9 [root@centos8 data]# 

排序是根据ASCII码排序

 1 [root@centos8 data]# echo {1..10..2}
 2 1 3 5 7 9
 3 [root@centos8 data]# echo {1..10..3}
 4 1 4 7 10
 5 [root@centos8 data]# echo {1..10..4}
 6 1 5 9
 7 [root@centos8 data]# echo {1..10..5}
 8 1 6
 9 [root@centos8 data]# echo {1..10..6}
10 1 7
11 [root@centos8 data]# echo {1..10..7}
12 1 8
13 [root@centos8 data]# echo {a..z..3}
14 a d g j m p s v y
15 [root@centos8 data]# echo {a..z..2}
16 a c e g i k m o q s u w y
17 [root@centos8 data]# 
1 [root@centos8 data]# echo {0..100..100}
2 0 100
3 [root@centos8 data]# echo {0..100..10}
4 0 10 20 30 40 50 60 70 80 90 100
5 [root@centos8 data]# echo {0..100}
6 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
7 [root@centos8 data]# echo {00..100}
8 000 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100

 

 

10.Tab键盘功能:

8.1双击Tab键盘举例:

 1 [root@localhost ~]# ke 2 kernel-install kexec 3 [root@localhost ~]#  

 

11.History命令

  

[root@centos8 ~]# cat .bash_history (#可以查看历史记录的命令)

[root@centos8 ~]# echo $HISTSIZE  (#可以查看默认的记录条数)
1000

history 查看历史:

 1 [root@localhost ~]# ba
 2 badblocks   base64      basename    bash        bashbug     bashbug-64  
 3 [root@localhost ~]# k
 4 kbdinfo         kbd_mode        kbdrate         kdumpctl        kernel-install  kexec           kill            killall5        kmod            kpartx
 5 [root@localhost ~]# ken
 6 -bash: ken: command not found
 7 [root@localhost ~]# 
 8 Display all 1219 possibilities? (y or n)
 9 [root@localhost ~]# ke
10 kernel-install  kexec           
11 [root@localhost ~]# ke
12 kernel-install  kexec           
13 [root@localhost ~]# history
14     1  tab
15     2  ba
16     3  ken
17     4  history
18 [root@localhost ~]# 

 

举例:删除内存中的操作记录以及清除最近的几个命令

 1 [root@localhost ~]# ls -a (#先查看一下文件 2 .   1   2  4  6  8  anaconda-ks.cfg  .bash_logout   .bashrc  file1   file2  file4  file6  file8  .tcshrc
 3 ..  10  3  5  7  9  .bash_history    .bash_profile  .cshrc   file10  file3  file5  file7  file9
 4 [root@localhost ~]# rm -f .bash_history (#记录都在这个.bash_history里面,删除他 5 [root@localhost ~]# history -c (#再清除最近的操作记录 6 [root@localhost ~]# exit(#退出)
 7 logout
 8 Connection closing...Socket close.
 9 
10 Connection closed by foreign host.
11 
12 Disconnected from remote host(192.168.0.110CentOS7.0) at 02:08:51.
13 
14 Type `help' to learn how to use Xshell prompt.
15 [C:\~]$ 
16 
17 Connecting to 192.168.0.110:22...
18 Connection established.
19 To escape to local shell, press 'Ctrl+Alt+]'.
20 
21 WARNING! The remote SSH server rejected X11 forwarding request.
22 Last login: Tue Jan 12 08:00:46 2021 from 192.168.0.102
23 [root@localhost ~]# history (#进入之后再次查看,只有最近记录24     1  exit
25     2  history
26 [root@localhost ~]# 

 

12、 计算1+2+3+...+99+100的总和

[root@localhost ~]# seq -s + 1 100 | bc

 20210701Renew~

posted on 2021-01-11 15:50  熊猫小虾  阅读(380)  评论(0编辑  收藏  举报

导航