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
记得快乐
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!