• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Y-wee
博客园    首页    新随笔    联系   管理     

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
记得快乐
posted @ 2022-04-23 12:27  Y-wee  阅读(52)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3