发布于:,更新于:

Linux变量使用

Linux变量使用

  1. 变量的定义与引用,用=进行定义,用$进行引用
1
2
3
4
5
[root@master ~]# name=zhangsan
[root@master ~]# echo $name
zhangsan
[root@master ~]# echo name
name

$在单引号中不能被识别,在双引号中可识别

1
2
3
4
[root@master ~]# echo "I am $name"
I am zhangsan
[root@master ~]# echo 'I am $name'
I am $name
  1. 环境变量

PATH环境变量:寻找可执行文件的搜索路径

1
2
[root@master ~]# echo $PATH
/root/perl5/bin:/root/perl5/bin:/opt/kafka_2.11-2.0.0/bin:/opt/zookeeper-3.4.12/bin:/opt/flink-1.10.2/bin:/opt/spark-2.1.1-bin-hadoop2.7//bin:/opt/apache-hive-2.3.4-bin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/hadoop-2.7.7/bin:/opt/hadoop-2.7.7/sbin:/usr/java/jdk1.8.0_162/bin:/usr/java/jdk1.8.0_162/jre/bin:/opt/scala-2.11.0/bin:/opt/apache-flume-1.7.0-bin/bin:/root/bin

PWD:当前工作目录

1
2
[root@master ~]# echo $PWD
/root

PS1和PS2:命令行的一级提示符和二级提示符

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
[root@master ~]# echo $PS1
[\u@\h \W]\$
[root@master ~]# echo $PS2
>

[root@master ~]# echo $PS1
[\u@\h \W]\$
[root@master ~]# echo $PS2
>
[root@master ~]# mysql -u root -p
Enter password: # (输入Passwd123!)
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.18 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| bak |
| hive |
| mysql |
| performance_schema |
| shtd_result |
| shtd_store |
| shtd_store01 |
| sys |
+--------------------+
9 rows in set (0.02 sec)

mysql> exit
Bye
[root@master ~]#

OLDPWD:前一个工作目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@master ~]# ll
总用量 68
drwxr-xr-x. 4 root root 36 11月 16 2022 aaa
-rw-------. 1 root root 1257 11月 18 2021 anaconda-ks.cfg
drwxr-xr-x. 5 root root 39 11月 18 2021 localperl
-rw-------. 1 root root 1623 7月 11 21:08 nohup.out
drwxr-xr-x. 5 root root 39 11月 18 2021 perl5
-rw-r--r--. 1 root root 50112 7月 11 21:31 ShiSai2022-1.0-SNAPSHOT.jar
-rw-r--r--. 1 root root 0 11月 15 2022 spark-yarn-logs
-rw-r--r--. 1 root root 4175 11月 16 2022 zookeeper.out
[root@master ~]# cd aaa
[root@master aaa]# echo $PWD
/root/aaa
[root@master aaa]# echo $OLDPWD
/root

HISTFILE:储存历史命令

1
2
3
4
[root@master aaa]# echo $HISTFILE
/root/.bash_history
[root@master aaa]# vi /root/.bash_history (退出----按:,按q,按回车键)
[root@master aaa]#

HOME: 当前用户的用户目录

1
2
[root@master ~]# echo $HOME
/root

3.PATH环境变量的综合运用

在PATH环境变量之前加入新的路径,/tmp

1
2
3
[root@master ~]# export PATH=/tmp:$PATH
[root@master ~]# echo $PATH
/tmp:/root/perl5/bin:/root/perl5/bin:/opt/kafka_2.11-2.0.0/bin:/opt/zookeeper-3.4.12/bin:/opt/flink-1.10.2/bin:/opt/spark-2.1.1-bin-hadoop2.7//bin:/opt/apache-hive-2.3.4-bin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/hadoop-2.7.7/bin:/opt/hadoop-2.7.7/sbin:/usr/java/jdk1.8.0_162/bin:/usr/java/jdk1.8.0_162/jre/bin:/opt/scala-2.11.0/bin:/opt/apache-flume-1.7.0-bin/bin:/root/bin

转到新的路径/tmp下面制造了一个可执行文件hello

1
2
3
4
5
6
7
8
9
[root@master ~]# cd /tmp
[root@master tmp]# ll
总用量 0
drwx------. 3 root root 17 12月 4 08:08 systemd-private-29aad2a4b89b4c6b8901f17655036ac6-chronyd.service-IX3I1a
drwx------. 2 root root 6 12月 4 08:08 vmware-root_8736-2857568991
[root@master tmp]# vi hello
# (按i键,看到屏幕底下有insert字样,此时输入)
echo "hello hadoop!"
# (按Esc键,屏幕底下的insert字样消失,再按:,按wq,按回车键,此时就保存退出了vi编辑器)
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
[root@master tmp]# ll hello
-rw-r--r--. 1 root root 21 12月 4 09:00 hello
[root@master tmp]# chmod 777 hello
[root@master tmp]# ll hello
-rwxrwxrwx. 1 root root 21 12月 4 09:00 hello
[root@master tmp]# cd /root
[root@master ~]# echo $PATH
/tmp:/root/perl5/bin:/root/perl5/bin:/opt/kafka_2.11-2.0.0/bin:/opt/zookeeper-3.4.12/bin:/opt/flink-1.10.2/bin:/opt/spark-2.1.1-bin-hadoop2.7//bin:/opt/apache-hive-2.3.4-bin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/hadoop-2.7.7/bin:/opt/hadoop-2.7.7/sbin:/usr/java/jdk1.8.0_162/bin:/usr/java/jdk1.8.0_162/jre/bin:/opt/scala-2.11.0/bin:/opt/apache-flume-1.7.0-bin/bin:/root/bin
[root@master ~]# hello
hello hadoop!
[root@master ~]# cd /tmp
[root@master tmp]# ll
总用量 4
-rwxrwxrwx. 1 root root 21 12月 4 09:00 hello
drwx------. 3 root root 17 12月 4 08:08 systemd-private-29aad2a4b89b4c6b8901f17655036ac6-chronyd.service-IX3I1a
drwx------. 2 root root 6 12月 4 08:08 vmware-root_8736-2857568991
[root@master tmp]# rm hello
rm:是否删除普通文件 "hello"?y
[root@master tmp]# ll
总用量 0
drwx------. 3 root root 17 12月 4 08:08 systemd-private-29aad2a4b89b4c6b8901f17655036ac6-chronyd.service-IX3I1a
drwx------. 2 root root 6 12月 4 08:08 vmware-root_8736-2857568991
[root@master tmp]# cd /root
[root@master ~]# hello
-bash: /tmp/hello: 没有那个文件或目录

课堂练习

(要求所有的命令均使用全局路径,将结果截图粘贴到桌面Release下面的word文件“模块A提交的结果”):

  1. 在根目录下面建立文件夹/bbb
1
mkdir /bbb
  1. 在文件夹/bbb中建立文件hi.sh
1
touch /bbb/hi.sh
  1. 在文件/bbb/hi.sh中,用vi编辑器输入10秒倒计时程序如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
vi /bbb/hi.sh
# 点击 i 键 进入 insert 编辑模式
echo "----start----"
echo "----10----";sleep 1;
echo "----9----";sleep 1;
echo "----8----";sleep 1;
echo "----7----";sleep 1;
echo "----6----";sleep 1;
echo "----5----";sleep 1;
echo "----4----";sleep 1;
echo "----3----";sleep 1;
echo "----2----";sleep 1;
echo "----1----";sleep 1;
echo "----launch!----"
保存并退出。(点击Esc退出insert编辑模式后输入 `:wq` )
  1. 设置hi.sh文件为可执行文件
1
chmod +x /bbb/hi.sh
  1. 把/bbb路径添加在PATH环境变量中,并生效
1
2
echo "PATH=\$PATH:/bbb" >> /etc/profile
source /etc/profile
  1. 在系统任意位置,执行hi.sh命令,将结果粘贴。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@MySQL01 ~]# sh hi.sh
----start----
----10----
----9----
----8----
----7----
----6----
----5----
----4----
----3----
----2----
----1----
----launch!----
[root@MySQL01 ~]#