Shell简单介绍
Shell简单介绍
Shell是一个命令行解析器,它接收应用程序/用户命令,然后调用操作系统内核
Shell还是一个功能相当强大的编程语言,易编写、易调试、灵活性强
Linux提供的Shell解析器有:
[user01@zookeeper1 shell]$ cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
/bin/tcsh
/bin/csh
Centos默认的解析器是bash
[user01@zookeeper1 shell]$ echo $SHELL
/bin/bash
脚本格式
脚本以#!/bin/bash开头(指定解析器)
第一个shell脚本
helloworld.sh
#!/bin/bash
echo "hello shell"
脚本的三种执行方式
- 采用 bash 或 sh+脚本的相对路径或绝对路径(不用赋予脚本+x 权限)
绝对路径
[root@zookeeper1 shell]# bash /home/user01/java/shell/helloworld.sh
hello shell
[root@zookeeper1 shell]# sh /home/user01/java/shell/helloworld.sh
hello shell
相对路径
[root@zookeeper1 java]# bash shell/helloworld.sh
hello shell
[root@zookeeper1 java]# sh shell/helloworld.sh
hello shell
- 采用输入脚本的绝对路径或相对路径执行脚本(必须具有可执行权限+x)
赋予helloworld.sh脚本的+x权限
# 赋予所有用户helloworld.sh文件可执行权限
chmod +x helloworld.sh
# 赋予当前用户helloworld.sh文件可执行权限
chmod u+x helloworld.sh
绝对路径
[root@zookeeper1 java]# /home/user01/java/shell/helloworld.sh
hello shell
相对路径(./表示当前路径)
[root@zookeeper1 java]# ./shell/helloworld.sh
hello shell
注意:第一种执行方法,本质是 bash 解析器帮你执行脚本,所以脚本本身不需要执行权限;第二种执行方法,本质是脚本需要自己执行,所以需要执行权限
- 在脚本的路径前加上“.”或者 source(了解)
[root@zookeeper1 java]# source /home/user01/java/shell/helloworld.sh
hello shell
[root@zookeeper1 java]# source shell/helloworld.sh
hello shell
[root@zookeeper1 java]# . /home/user01/java/shell/helloworld.sh
hello shell
[root@zookeeper1 java]# . shell/helloworld.sh
hello shell
记得快乐
浙公网安备 33010602011771号