Shell 从入门到精通

  • 相关文章
  • 一.Shell背景
    • 1.1 GNU计划
    • 1.2 Shell
    • 1.3 Bash
  • 二. Shell脚本基本语法
    • 2.1 Getting Start
      • 1. Introduction
      • 2. Setting up Workspace
      • 3. Hello World
      • 4. Motivation Example
    • 2.2 Variable
    • 2.3 Data Type
    • 2.4 String
    • 2.5 Array
    • 2.6 Conditional Statements
      • 1. if else
      • 2. switch case
    • 2.7 Looping
      • 1. while
      • 2. for
      • 3. 无限循环
    • 2.8 基本运算
    • 2.9 Function
    • 2.10 Import
  • 三.Shell脚本实践
    • 3.1 备忘录
    • 3.2 导航
  • 个人简介

相关文章

  • 【Linux】深入理解Awk
  • 【Linux】深入理解Sed
  • 【Linux】深入理解Vim

一.Shell背景

1.1 GNU计划

Tips:
The GNU Project is a free software, mass collaboration project that Richard Stallman announced on September 27, 1983. Its goal is to give computer users freedom and control in their use of their computers and computing devices by collaboratively developing and publishing software that gives everyone the rights to freely run the software, copy and distribute it, study it, and modify it. GNU software grants these rights in its license.

1984年,史托曼开始GNU计划, 这个计划的目的是:创建一个自由、开放的Unix操作系统 (Free Unix)。

Tips:
GNU’s not Unix!
GNU并不是 Unix啊!那么GNU又是什么呢? 就是GNU’s Not Unix嘛!..可是无穷循环啊!忙碌~

本章所说的Bash Shell就是来源于GNU,还有一些比较著名的软件来源于GNU:

  • Emacs
  • GNU C (GCC)
  • GNU C Library (glibc)
  • Bash shell

Tips:
In 1991, the Linux kernel appeared, developed outside the GNU project by Linus Torvalds, and in December 1992 it was made available under version 2 of the GNU General Public License. Combined with the operating system utilities already developed by the GNU project, it allowed for the first operating system that was free software, commonly known as Linux.

1.2 Shell

Important:
我们必须要通过“ Shell ”将我们输入的指令与 Kernel 沟通, 好让 Kernel 可以控制硬件来正确无误的工作。

我们可以发现应用程序其实是在最外层,如同鸡蛋外壳一样,因此就被称呼为壳程序 (shell) 啰!

注意:
只要能够操作应用程序的接口都能够称为壳程序。

  • 狭义:命令行方面的软件,bash 等。
  • 广义:图形接口的软件!因为图形接口其实也能够操作各种应用程序来调用核心工作啊!

Shell有很多版本例如常听到的Bourne SHell (sh) 、在 Sun 里头默认的 C SHell,还有 TCSH,zsh 等等,每一种 Shell 都各有其特点。Bash Shell是Bourne Shell 的增强版本,也是基准于 GNU 的架构下发展出来的!

在你的Mac电脑中有多少可使用的Shells呢,使用如下命令查看:

MacBook-Pro-3:~ sunquan$ cat -n /etc/shells1 # List of acceptable shells for chpass(1).2 # Ftpd will not allow users to connect who are not using3   # one of these shells.4 5   /bin/bash6  /bin/csh7   /bin/dash8  /bin/ksh9   /bin/sh10   /bin/tcsh11 /bin/zsh
MacBook-Pro-3:~ sunquan$

1.3 Bash

而当下日常大多数默认使用的是Bash Shell。我们可以在电脑中man bash一下

MacBook-Pro-3:~ sunquan$ man bash
BASH(1)                                                                BASH(1)NAMEbash - GNU Bourne-Again SHellSYNOPSISbash [options] [file]COPYRIGHTBash is Copyright (C) 1989-2005 by the Free Software Foundation, Inc.DESCRIPTIONBash  is  an  sh-compatible  command language interpreter that executescommands read from the standard input or from a file.  Bash also incor-porates useful features from the Korn and C shells (ksh and csh).Bash  is  intended  to  be a conformant implementation of the Shell andUtilities portion  of  the  IEEE  POSIX  specification  (IEEE  Standard1003.1).  Bash can be configured to be POSIX-conformant by default.

Tips:
Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been used as the default login shell for most Linux distributions.

早在1989年就发布了Bash,被用来操作操作系统的基本接口。那Bash有哪些优点呢:

1.命令历史记录功能
用户可以在命令终端中通过上下箭头键来切换历史使用的命令,在~/.bash_history里记录了使用过的bash命令

MacBook-Pro-3:~ sunquan$ tail -n 10 ~/.bash_history
vim awkDemo2
diff awkDemo awkDemo2
eixt (0) | echo $?
eixt (0) ; echo $?
eixt 0 ; echo $?
eixt 0;echo $?
var = sq ; echo var
var = sq ; echo $var
var=sq;echo $var
exit 0;echo $var

2.命令补全功能
通过Tab键可进行命令补全和文件补全

3.alase 别名设置

MacBook-Pro-3:~ sunquan$ ls -l
total 32
drwx------@  6 sunquan  staff   192  5 27  2020 Applications
...
MacBook-Pro-3:~ sunquan$ alias ll='ls -l'
MacBook-Pro-3:~ sunquan$ ll
total 32
drwx------@  6 sunquan  staff   192  5 27  2020 Applications
MacBook-Pro-3:~ sunquan$

4.程序化脚本: (shell scripts)
可通过写shell脚本,汇集命令构建更多的功能

二. Shell脚本基本语法

2.1 Getting Start

1. Introduction

shell script 是利用shell的功能所写的一个“程序 (program)”,这个程序是使用纯文本文件。也可被看成批处理文件或一个程序语言,且由于都是利用 shell 与相关工具指令,所以不需要编译即可执行。

2. Setting up Workspace

MacBook-Pro-3:~ sunquan$ vim ~/.bash_profile
export HISTFILESIZE=1024 #设置history记录bash命令的大小
export PATH=${PATH}:/Users/sunquan/Library/shell #shell工作空间
export PATH=${PATH}:/usr/local/bin
export PATH=$PATH:/Users/sunquan/Library/Android/android-ndk-r18b/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
source /Library/Frameworks/Python.framework/Versions/3.7/bin/virtualenvwrapper.sh
# Setting PATH for Python 3.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}"
export PATH
MacBook-Pro-3:~ sunquan$ source ~/.bash_profile

我自己的shell脚本存放在/Users/sunquan/Library/shell下,配置到系统的PATH里,当使用时脚本命令时,系统可自动在该目录下找到相关命令并执行。

3. Hello World

#! /bin/bash
# author: scott sun
# #!是一个约定的标记,告诉脚本用什么shell环境来执行
# a program to show "hello world" in your screen
# how to run : switch the dir
# chmod +x helloworld
# ./helloworld; if your add the scripts'dir in your PATH  variable, you can run helloworld directly
echo "Hello World"
exit 0

Important:
1. 第一行#!/bin/bash宣告这个script使用的shell名称,必须要包含。当这个程序运行的以后,就能够载入bash的相关环境配置文件
2.一个指令的执行结果可以使用$?这个变量来观察。因此exit 0 代表离开script后回传一个0给系统。随后如果下达echo $?则可得到0的值

4. Motivation Example

 #!/bin/bash#author : scott sun#function: use to count how much commands in your computer.IFS=":"count=0; nonex=0for directory in $PATH ; doif [ -d "$directory" ]thenfor command in "$directory"/* ; doif [ -x "$command" ]thencount=$[count + 1]elsenonex=$[nonex + 1]fidonefidoneecho "$count commands, and $nonex that weren't executable"exit 0MacBook-Pro-3:shell sunquan$ countShellCommands
2438 commands, and 14 that weren't executable
MacBook-Pro-3:shell sunquan$

2.2 Variable

定义变量:

myname="sq"

Important:
1. 变量名不能有空格
2.英文字母,数字和下划线,不能使用bash关键字

使用变量

myname="sq"
echo $myname
echo ${myname}
echo "my name is ${myname}"

2.3 Data Type

#!/bin/bashvint=1vint2=2vfloat=1.1vString="sq"echo "int add int is $[ vint + vint2 ]"echo "int add float is $[ vint + vfloat ]"echo "int add string $[ vint + vString ]"MacBook-Pro-3:shell sunquan$ variableDemo
int add int is 3
/Users/sunquan/Library/shell/variableDemo: line 8: 1.1: syntax error: invalid arithmetic operator (error token is ".1")
int add string 1
MacBook-Pro-3:shell sunquan$

Important:
1. shell没有明确的关键字定义数据类型
2.相同类型的可相加,否则报错
3.整形和字符串不能相加

2.4 String

#!/bin/bashmyname="scott.sun"echo "hello my name is $myname"echo 'hello, my name is $myname'echo "the length of $myname is ${#myname}"echo "substring(1,3) is ${myname:1:3}"echo "the first name is "; eval "echo $myname | cut -c1-5"

Important:
1. 单引号原样输出,双引号可显示变量
2. ${#string}输出字符串长度
3. ${string:start:end}截取字符串

2.5 Array

#!/bin/basharray=(12345)echo $arrayecho ${array[0]}echo ${array[5]}echo "the length of arr is ${#array[*]}"echo "the content of array is ${array[*]}"echo "the content of array is ${array[@]}"MacBook-Pro-3:shell sunquan$ arrayDemo
1
1the length of arr is 5
the content of array is 1 2 3 4 5
the content of array is 1 2 3 4 5

Important:
1. array定义如上
2. $array输出第一个元素,通过arr[index]获取数值,操作不输出
3. ${array[*]}, ${array[@]}功能雷同

2.6 Conditional Statements

1. if else

if [ -x "/path/filename" ]
thenecho "this file can execute"
elseecho "this file can't execute"
fi

2. switch case

#!/bin/bash
echo "input the num between 1-4"
read num
case $num in1) echo "the num is 1";;2) echo "the num is 2";;3) echo "the num is 3";;4) echo "the num is 4";;*) echo "the num is out of 1-4";;
esac

2.7 Looping

1. while

#!/bin/bash
cnt=1
while(( $cnt<=5 ))
doecho $cntlet "cnt++"
done

Tips:
1. let 用于执行一个或者多个表达式不需要$符号

2. for

#!/bin/bash
for loop in 1 2 3 4 5
do echo "The value is $loop"
done

3. 无限循环

for (( ; ; ))

2.8 基本运算

#    +  -   *   /   %
#   ==    != -eq -ne -gt -lt -ge -le
#   !   -o -a (非或与)
#   -z 字符串长度是否为0
#   -n 字符串长度是否部位0
#   $ 字符串是否为空
echo $[ 1 + 2 ]
# -r -w -f -d -x -e
# 可读, 可写 普通文件    是否目录    是否可执行   是否存在
if [ -r $file ]
thenecho "文件可读"
elseecho "文件不可读"
fi

2.9 Function

 #!/bin/bashfun1(){echo "the name of command is $0"echo "the first params is $1"echo "the num of params is $#"echo "the str of params is $*"}fun1 1 2 3 4 strexit 0MacBook-Pro-3:shell sunquan$ funcDemo
the name of command is /Users/sunquan/Library/shell/funcDemo
the first params is 1
the num of params is 5
the str of params is 1 2 3 4 str

2.10 Import

#!/bin/bash
# this is first script
. secondScriptName

三.Shell脚本实践

3.1 备忘录

#!/bin/bash
# file name: remember
# func : remember the note
rememberfile="$HOME/.remember_work"
if [ $# -eq 0 ] ; thenecho "Enter note, end with ^D"cat - >> $rememberfile
else echo "$@" >> $rememberfile
fi
exit 0
#!/bin/bash
# remind note
rememberfile="$HOME/.remember_work"
if [ ! -f $rememberfile ]
thenecho "$0: .remember not exist." >$2exit 1
fiif [ $# -eq 0 ]
then more $rememberfile
else grep -i "$@" $rememberfile | more
fi
exit 0

3.2 导航

#!/bin/bash
if [ $# -le 0 ] ; then open https://baidu.com
elif [ $1 == 'mail' ] ; thenopen mail.163.com
...
fi

Shell还有很多用法,就如同 4.Motivation Example中,电脑里至少有上千个可执行命令,就如同乐高,用这些命令堆出你想像中的积木。你可以和Git,awk,sed,ffmpeg,adb,gradle等等开发环境和命令结合来打造你的完美开发环境。

个人简介

职业:阿里巴巴 - 无线开发专家
WX:sunquan97
邮件:sunquan9301@163.com
添加请注明CSDN + 原因,

【Linux】深入理解Shell用法,从入门到精通相关推荐

  1. Linux下的shell语言编程入门

    通常情况下,我们从命令行输入命令每输入一次就能够得到系统的一次响应.一旦需要我们一个接着一个的输入命令而最后才得到结果的时候,这样的做法显然就没有效率.要达到这样的目的,通常我们利用shell程序或者 ...

  2. linux shell编程从入门到精通pdf_SHELL脚本编程入门

    一. SHELL入门 1.1 变量 1.1.1 变量名规范 变量是由任何字母.数字.下划线组成的字符串,且不能以数字开头. 区分字母大小写 变量.等号.值中间不能出现任何空格 实例 注:$var1+$ ...

  3. gnu linux额外支持的运算符,《Shell编程从入门到精通》张昊-chap1-8

    缘起 20190314开始复习及学习吧: 张昊编著: 内容 目录 i(9/314) 全书11章 chap1 第1个Shell程序 1(13/334) 1.1.第一道菜 [echo.sh] #!/bin ...

  4. git用法从入门到精通

    文章目录 前言 一 配置user信息 二 往仓库里添加文件 三 通过git log查看版本演变历史 四 其他git常用的一些命令技巧: 五 git踩坑经历 5.1 git add . 后执行了git ...

  5. linux运行luminati,Luminati使用从入门到精通-Luminati中国

    2.8 更换ip 点击如下按钮,就完成了ip更换 也可通过API, 发送一个HTTP POST请求,非GET请求, 更换IP 例子: 用shell执行如下命令更换ip,空的body.当然,你也可以用其 ...

  6. shell编程入门 linux解释器原理,Shell编程入门Linux解释器原理详细介绍 使用Shell进行工作的人们对.doc...

    Shell编程入门Linux解释器原理详细介绍 使用Shell进行工作的人们对 Shell编程入门:Linux解释器原理详细介绍使用Shell进行工作的人们对Unix/Linux下的Shell编程都很 ...

  7. 【Linux命令行与Shell脚本编程】第五章 理解 Shell 父子关系 后台进程 协程

    Linux命令行与Shell脚本编程 第五章 理解 Shell 文章目录 Linux命令行与Shell脚本编程 五,理解 Shell 5.1,shell的类型 5.2,shell的父子关系 5.2.1 ...

  8. 「Linux」Linux Shell 编程基础入门

    Linux Shell 编程基础入门 1. 变量 1.1 变量定义 1.2 使用变量 1.3 引号 1.4 将命令的结果赋值给变量 1.5 位置参数 1.6 特殊变量及其含义 2. 字符串 2.1 字 ...

  9. 京峰linux课程目录,京峰教育 Linux编程Shell从入门到精通视频教程

    本套课程适用于初学者以及各类运维人员,我们可以通过本次系列的编程技术学习,掌握企业里面的运维编程必备脚本,同时掌握企业运维到底要求会什么东西,有哪些是我们必须要掌握的知识,为我们的运维能力保驾护航.增 ...

最新文章

  1. 与应用程序松耦合的报表开发组织
  2. (转)VS2005 SP1发布,解决只能创建WebSite,无法创建Web Application项目的问题
  3. PCB产业对ERP软件提出了哪些挑战?
  4. LeetCode-基础动态规划-70. 爬楼梯
  5. 世界围棋人机大战、顶峰对决第一盘:围棋世界冠军Lee Sedol(李世石,围棋职业九段)对战Google DeepMind AlphaGo围棋程序,Google AlphaGo首战告捷
  6. java收发邮寄_JavaMail收发邮件的一般流程与主要方法
  7. 【kafka】 kafka 0.10报错IOException: Connection to 1 was disconnected before the response was read
  8. windows 代理软件_MacOS好用软件推荐(一)
  9. 如何使用Tuxera NTFS for Mac禁用特定NTFS分区
  10. 机器人庄园作文_赛尔号作文
  11. UE4读取BackBuffer缓冲区贴图(屏幕表面)
  12. 小学计算机二课堂活动总结,康宁路学校“信息技术与课堂教学深度融合”活动总结...
  13. java网络文章博客抓取系统_java 后端博客系统文章系统——No5
  14. vscan Ineligible for use by VSAN
  15. 转载-代码的马斯洛金字塔
  16. 大学生没有项目经验该怎么拿测开岗位的office?
  17. Angular+arcgisjs之平面地图测距、测面积、搜索
  18. go ent框架使用
  19. Linux 之 快捷键,命令总结 --- 三剑客**
  20. SuperRecovery超级硬盘数据恢复软件

热门文章

  1. 来西亚航空公司确认飞机已经失事 我们为遇难者哀悼
  2. FastDFS 分布式文件系统详解
  3. 阿里巴巴最新 SpringCloudAlibaba 实战教程,入门到精通面面俱到
  4. 最新百度 阿里 华为 腾讯 谷歌面试笔试题及解析 (转)
  5. 【​观察】揭露迅雷“内讧”闹剧真相 制度性反腐依旧任重道远
  6. adobe CS5 master collection的序列号
  7. 受政府“关门”影响 纽约市周边两主要机场出现航班延误
  8. 笔记:threejs 版本不同用法
  9. python数据分析北京_Python实现的北京积分落户数据分析示例
  10. UVM Systemverilog EDA IP国外学习网站