bash icon indicating copy to clipboard operation
bash copied to clipboard

bash 编程语言

Linux Shell编程

20200127_164206_47

Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。

Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。

Ken Thompson 的 sh 是第一种 Unix Shell,Windows Explorer 是一个典型的图形界面 Shell。

本仓库内容

  1. shell脚本编程学习笔记
Something I hope you know before go into the coding~
First, please watch or star this repo, I'll be more happy if you follow me.
Bug report, questions and discussion are welcome, you can post an issue or pull a request.

相关站点

参考书目

  • 《Linux命令行与Shell脚本编程大全》
  • 《精通UNIX.shell脚本编程》
  • 《实战LINUX_SHELL编程与服务器管理》
  • 《Shell脚本编程诀窍——适用于Linux、Bash等》

目录

  • shell简介
  • shell原理
    • 命令的结束状态
  • 环境变量
    • 变量类型
    • shell保留关键字
    • 变量赋值
    • 位置参数
    • 删除变量
    • 预定义变量
    • 标准变量
    • 特殊变量
  • 数据类型
    • 字符串
      • 字符串截取与拼接
  • bash内置命令
    • alias
    • buildin
    • compgen
    • declare
    • exec
    • echo
    • exit
    • set
    • let
    • test
    • history
    • pwd
    • cd
    • readonly
    • command
    • shift
    • true
    • false
    • umask
    • ulimit
    • getopts
    • shopt
    • read
    • help
  • 流程控制
    • 判断
      • if
      • case
    • 循环
      • for
      • while
      • until
      • continue
      • break
    • select命令
  • 函数
  • 正则表达式与模式匹配
  • 文本三剑客
    • sed
    • gawk
    • grep
  • 文本编辑器
    • vim
    • nano
    • emacs

总结

  1. 基础永远值得花费90%的精力去学习加强。厚积而薄发~
  2. 实践的重要性

常用套路snip

if判断

  • [[ ... && ... && ... ]] 和 [ ... -a ... -a ...] 不一样,[[ ]] 是逻辑短路操作,而 [ ] 不会进行逻辑短路
  • [[ ... ]]进行算术扩展,而[ ... ]不做
if [ X"${var}" != "X1" ];then
    ...
fi

&& and 判断

if [ X"${var}" != "X1" -a X"${var2}" != "X2" ];then
    ...
fi
if [[ X"${var}" != "X1" and X"${var2}" != "X2" ]];then
    ...
fi
if [[ X"${var}" != "X1"  ]] and [[  X"${var2}" != "X2" ]];then
    ...
fi

|| or 判断

if [ X"${var}" != "X1" -o X"${var2}" != "X2" ];then
    ...
fi
if [[ X"${var}" != "X1" || X"${var2}" != "X2" ]];then
    ...
fi
if [[ X"${var}" != "X1"  ]] or [[  X"${var2}" != "X2" ]];then
    ...
fi

20210708_224801_51

循环

死循环写法

while [ 1 ];do
    ...
done