Linux Shell编程
什么是 shell
shell 是用户与 linux 沟通的一个桥梁
shell 编程是将执行的单个命令按照一定的逻辑和规则,组装到一个文件中,执行的时候直接执行文件
第一个 shell 脚本
vi hello.sh
#! /bin/bash
echo hello world!
#! 约定标记,告诉系统这个脚本使用哪个解释器来执行,即使用哪一种 shell
echo hello world! ,控制台输出 hello world!
bash hello.sh,执行 hello.sh。sh hello.sh 也可以
ll /bin/sh
lrwxrwxrwx.1 root root 4 Mar 22 20:00 /bin/sh -> bash
sh 是一个链接文件,指向的也是 /bin 目录下面的 bash 文件
如果让脚本可以单独执行,需要赋予权限。上面的是将 hello.sh 脚本的内容当做参数传给了 bash 执行
chmod u+x hello.sh
ll
total 3
-rwxr--r--. 1 root root 45 Apr 2 16:11 hello.sh
./hello.sh
hello world!
/root/hello.sh
hello world!
[root@bigdata01 shell]# hello.sh
-bash: hello.sh: command not found
前面没有带任何路径信息,按照 linux 查找规则,会到 PATH 这个环境变量中指定的路径信息中查找
PATH 环境变量中的路径如下,
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
如果想要不带前置路径信息执行 shell,可以配置 PATH,与配置 JAVA 环境变量一样,增加一个 . 即可
vi /etc/profile
export PATH = .:$PATH
source /etc/profile
echo $PATH
bash -x hello.sh
+ echo hello 'world!'
hello world!