Teng's blog Teng's blog
首页
Java
H5前端
GitHub (opens new window)
首页
Java
H5前端
GitHub (opens new window)
  • 介绍

  • 安装

  • 基础

  • shell

    • 介绍
    • 入门
      • 脚本入门
      • 脚本格式
      • helloworld
      • 脚本常用执行方式
    • 系统变量
    • 自定义变量
    • 环境变量
    • 位置参数变量
    • 预定义变量
    • 运算符
    • 条件判断
    • 流程控制
    • 读取控制台输入
    • 系统函数-路径文件名
    • 自定义函数
    • 工具
    • 数据库备份案例
  • 面试与总结
  • System-Linux
  • shell
Shetengteng
2022-03-13

入门

# 脚本入门

使用 shell 编写的脚本程序,作为命令语言互动式的解释和执行用户的输入命令 可以用于程序设计,提供定义变量,参数的手段,丰富的程序控制 类似于Dos系统中的批处理文件

# 脚本格式

脚本以 #!/bin/bash 开头,用于指定解释器 脚本需要有可执行权限

# helloworld

需求说明

  • 创建一个shell脚本,输出hello world

创建一个脚本,输出helloworld

[root@hadoop100 sh-demo]# touch helloworld.sh
[root@hadoop100 sh-demo]# vi helloworld.sh 
[root@hadoop100 sh-demo]# cat helloworld.sh 
#!/bin/bash
echo "helloword"
[root@hadoop100 sh-demo]# sh helloworld.sh 
helloword
1
2
3
4
5
6
7

# 脚本常用执行方式

方式1:输入脚本的绝对路径或相对路径

  • 赋予脚本 +x 权限

  • 执行脚本

[root@hadoop100 sh-demo]# chmod +x helloworld.sh 
[root@hadoop100 sh-demo]# ll
-rwxr-xr-x. 1 root root 29 3月  28 23:44 helloworld.sh
[root@hadoop100 sh-demo]# ./helloworld.sh # 相对路径
helloword
1
2
3
4
5

方式2:sh+脚本,不推荐

  • 使用bash或sh+脚本的相对路径或绝对路径,不用赋予脚本+x权限
  • sh 脚本的相对路径
[root@hadoop100 sh-demo]# sh helloworld.sh 
1
Last Updated: 2022/03/20, 10:04:55
介绍
系统变量

← 介绍 系统变量→

Theme by Vdoing | Copyright © 2021-2022 Shetengteng | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式