Linux变量使用
Linux变量使用变量的定义与引用,用=进行定义,用$进行引用 1 2 3 4 5 [root@master ~] [root@master ~] zhangsan [root@master ~] name
$在单引号中不能被识别,在双引号中可识别
1 2 3 4 [root@master ~] I am zhangsan [root@master ~] I am $name
环境变量 PATH环境变量:寻找可执行文件的搜索路径
1 2 [root@master ~] /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:当前工作目录
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 ~] [\u@\h \W]\$ [root@master ~] > [root@master ~] [\u@\h \W]\$ [root@master ~] > [root@master ~] Enter password: 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 ~] 总用量 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 ~] [root@master aaa] /root/aaa [root@master aaa] /root
HISTFILE:储存历史命令
1 2 3 4 [root@master aaa] /root/.bash_history [root@master aaa] [root@master aaa]
HOME:当前用户的用户目录
3.PATH环境变量的综合运用
在PATH环境变量之前加入新的路径,/tmp
1 2 3 [root@master ~] [root@master ~] /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 ~] [root@master tmp] 总用量 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] echo "hello hadoop!"
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] -rw-r--r--. 1 root root 21 12月 4 09:00 hello [root@master tmp] [root@master tmp] -rwxrwxrwx. 1 root root 21 12月 4 09:00 hello [root@master tmp] [root@master ~] /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 hadoop! [root@master ~] [root@master tmp] 总用量 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" ?y[root@master tmp] 总用量 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] [root@master ~] -bash: /tmp/hello: 没有那个文件或目录
课堂练习(要求所有的命令均使用全局路径,将结果截图粘贴到桌面Release下面的word文件“模块A提交的结果”):
在根目录下面建立文件夹/bbb 在文件夹/bbb中建立文件hi.sh 在文件/bbb/hi.sh中,用vi编辑器输入10秒倒计时程序如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 vi /bbb/hi.sh 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` )
设置hi.sh文件为可执行文件 把/bbb路径添加在PATH环境变量中,并生效 1 2 echo "PATH=\$PATH:/bbb" >> /etc/profile source /etc/profile
在系统任意位置,执行hi.sh命令,将结果粘贴。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 [root@MySQL01 ~] ----start---- ----10---- ----9---- ----8---- ----7---- ----6---- ----5---- ----4---- ----3---- ----2---- ----1---- ----launch!---- [root@MySQL01 ~]