参考视频:老男孩
shell是一个程序,是用户和Linux内核沟通的桥梁
shell脚本第一行
#!/bin/bash
其中#!不是注释,而是告诉操作系统将执行此脚本所用的解释器的名字。
配置vim更适应shell编写
:syntax on
:set hlsearch
:set tabstop=4
:set autoindent
可以去掉冒号,添加到~/.vimrc中
helloWorld
#! /bin/bash
# helloworld
hello world!
# 文件名为helloworld
运行:.helloworld
运行shell
- .脚本名
- 解释器 脚本名(如 sh 脚本名 或者 bsah 脚本名)
变量和常量
- 变量、常量定义
变量名 = 值
常量的名字一般大写,实际上变量和常量是一样的 - 读取变量
$变量名
特殊字符
`反引号:命令中执行命令
条件判断 if
# commands为某个命令
if commands; then
commands
[elif commands; then
commands...]
[else
commands]
fi
逻辑操作符
- 与 &&
- 或 ||
- 非 !
读取键盘输入
read [-options] 变量名1 变量名2 ……
循环
while commands; do commands; done
支持break和continue