一、上堂回顧
安裝git
sudo apt-get install git
創(chuàng)建版本庫(kù)【本地版本庫(kù)】
普通目錄滴某,git init
創(chuàng)建文件在工作區(qū)狈孔,git add filename git commit -m "日志描述"
時(shí)光穿梭機(jī)
版本回退:git reset --hard HEAD^ 或者git reset --hard commit id
工作區(qū)/版本庫(kù)/暫存區(qū)
管理修改:git status / git diff HEAD -- filename :查看版本庫(kù)和工作區(qū)之間的不同
撤銷修改:
a.修改了工作區(qū)中的文件珠插,但是還沒(méi)有add
git checkout -- filename :丟棄工作區(qū)的修改
b.修改了工作區(qū)中的文件,add到暫存區(qū)跋核,但是還沒(méi)有commit
git reset HEAD filename:丟棄暫存區(qū)的修改
git checkout -- filename
c.修改了工作區(qū)中的文件,并add岖瑰,commit
回退版本
刪除文件:
刪除工作區(qū)中的文件:rm filename
刪除版本庫(kù)中的文件:git rm filename
遠(yuǎn)程倉(cāng)庫(kù)
添加ssh key【建立當(dāng)前計(jì)算機(jī)和遠(yuǎn)程倉(cāng)庫(kù)之間的連接】
添加遠(yuǎn)程倉(cāng)庫(kù):先有本地倉(cāng)庫(kù),然后根據(jù)本地倉(cāng)庫(kù)創(chuàng)建遠(yuǎn)程倉(cāng)庫(kù)
git remote add origin git@github.com:username/learngit.git:建立班底和遠(yuǎn)程倉(cāng)庫(kù)之間的連接
git push -u origin master:將當(dāng)前的master分支推送到遠(yuǎn)程倉(cāng)庫(kù)
git push origin master:以后再修改之后砂代,可以再次推送
克隆倉(cāng)庫(kù):先有遠(yuǎn)程倉(cāng)庫(kù)蹋订,然后將遠(yuǎn)程倉(cāng)庫(kù)克隆到本地
git clone git@github.com:username/clonegit.git
分支管理
創(chuàng)建并切換分支:git checkout -b dev
切換分支:git checkout dev
查看分支:git branch
合并到master:切換到master分支,執(zhí)行命令git merge dev
二刻伊、git
6.分支管理
6.4bug分支
一般情況下露戒,每個(gè)bug都需要使用一個(gè)分支來(lái)進(jìn)行解決椒功,解決后,分支刪除
git stash:可以把當(dāng)前工作現(xiàn)場(chǎng)儲(chǔ)存起來(lái)智什,然后先進(jìn)行其他額的工作动漾,完成工作之后,可以解封繼續(xù)工作
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" contenteditable="true" cid="n42" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">演示命令:
rock@rockrong:~/Desktopgit branch
- master
rock@rockrong:~/Desktop/day5Textgit stash #封存工作現(xiàn)場(chǎng)
保存工作目錄和索引狀態(tài) WIP on dev: e742319 Initial commit創(chuàng)建bug分支
rock@rockrong:~/Desktop/day5Text
vim README.md
rock@rockrong:~/Desktop/day5Textgit commit -m "fixed a bug"
[bug-01 235be14] fixed a bug
1 file changed, 2 insertions(+), 1 deletion(-)
rock@rockrong:~/Desktop/day5Textgit merge --no-ff -m "merge bug-01" bug-01
Merge made by the 'recursive' strategy.
README.md | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)刪除bug分支
rock@rockrong:~/Desktop/day5Text$ git branch -d bug-01
已刪除分支 bug-01(曾為 235be14)荠锭。查看封存列表
rock@rockrong:~/Desktop/day5Text$ git stash list
stash@{0}: WIP on dev: e742319 Initial commit解開(kāi)封存.這時(shí)會(huì)沖突,解決沖突
rock@rockrong:~/Desktop/day5Text$ git stash pop</pre>
總結(jié):
修復(fù)bug時(shí)旱眯,創(chuàng)建一個(gè)新的分支,進(jìn)行bug的修復(fù)证九,然后合并删豺,最后刪除
當(dāng)手頭的工作沒(méi)有完成的時(shí)候,使用git stash 將內(nèi)容封存愧怜,然后取修復(fù)bug呀页,當(dāng)bug修復(fù)完成之后,則使用命令git stash pop解封
6.5feature分支
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" contenteditable="true" cid="n48" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 0px; width: inherit;">演示命令:
rock@rockrong:~/Desktop/day5Text$ git branch
dev
- master
rock@rockrong:~/Desktop/day5Texttouch a.txt
rock@rockrong:~/Desktop/day5Textgit commit -m "create a.txt"
[feature1 120a22f] create a.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a.txt
rock@rockrong:~/Desktop/day5Textvim a.txt
rock@rockrong:~/Desktop/day5Textgit status
位于分支 feature1
要提交的變更:
(使用 "git reset HEAD <文件>..." 以取消暫存)
?
修改: a.txt
?
rock@rockrong:~/Desktop/day5Textgit checkout dev
切換到分支 'dev'
rock@rockrong:~/Desktop/day5Textgit branch -D feature1 #強(qiáng)制刪除
已刪除分支 feature1(曾為 af31c25)丸氛。</pre>總結(jié):
每開(kāi)發(fā)一個(gè)新的功能【版本迭代】,最好新建一個(gè)分支來(lái)進(jìn)行操作
如果需要丟棄一個(gè)還沒(méi)有被合并的分支惨奕,使用命令 git branch -D branch-name
6.6多人協(xié)作
當(dāng)你從遠(yuǎn)程倉(cāng)克隆時(shí)雪位,實(shí)際上git將本地的master和遠(yuǎn)程的master對(duì)應(yīng)起來(lái)了竭钝,并且遠(yuǎn)程倉(cāng)庫(kù)的默認(rèn)的名字為origin
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" contenteditable="true" cid="n55" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 0px; margin-top: 15px; width: inherit;">演示命令:
rock@rockrong:~/Desktop/day5Textgit remote -v
origin git@github.com:yangyang-git/day5Text.git (fetch) #抓取地址
origin git@github.com:yangyang-git/day5Text.git (push) #推送地址</pre>
1>推送分支
推送分支:把該分支上的所有的本地提交推送到遠(yuǎn)程庫(kù)梨撞,推送時(shí),要指定本地分支
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" contenteditable="true" cid="n59" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">演示命令:
rock@rockrong:~/Desktop/day5Text$ git branch
- dev
master推送到主分支
rock@rockrong:~/Desktop/day5Text$ git push origin master
對(duì)象計(jì)數(shù)中: 4, 完成.
Delta compression using up to 2 threads.
壓縮對(duì)象中: 100% (2/2), 完成.
寫入對(duì)象中: 100% (4/4), 340 bytes | 340.00 KiB/s, 完成.
Total 4 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To github.com:yangyang-git/day5Text.git
e742319..cc4bef3 master -> master創(chuàng)建子分支
rock@rockrong:~/Desktop/day5Text$ git checkout -b dev
推送到子分支
rock@rockrong:~/Desktop/day5Text$ git push origin dev
Total 0 (delta 0), reused 0 (delta 0)
To github.com:yangyang-git/day5Text.git
- [new branch] dev -> dev</pre>
總結(jié):
并不是所有的分支都需要推送到遠(yuǎn)程倉(cāng)庫(kù)
a.master分支時(shí)主分支香罐,因此要時(shí)刻與遠(yuǎn)程保持同步
b.dev是一個(gè)開(kāi)發(fā)分支卧波,團(tuán)隊(duì)所有的成員都在上面工作,所以也需要推送到遠(yuǎn)程倉(cāng)庫(kù)
c.bug分支只是修復(fù)一個(gè)bug庇茫,就沒(méi)必要推送到遠(yuǎn)程
d.先本地刪除分支,再刪除遠(yuǎn)程分支git push origin --delete dev
2>抓取分支
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" contenteditable="true" cid="n68" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 0px; width: inherit;">演示命令:
老王和老李,在做同一個(gè)項(xiàng)目
rock@rockrong:~/Desktop/day5Text
mkdir other
rock@rockrong:~/Desktopgit clone git@github.com:yangyang-git/day5Text.git
正克隆到 'day5Text'...
remote: Counting objects: 10, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 10 (delta 2), reused 6 (delta 1), pack-reused 0
接收對(duì)象中: 100% (10/10), 完成.
處理 delta 中: 100% (2/2), 完成.
rock@rockrong:~/Desktop/other$ cd day5Text/老王抓取下來(lái)只有master
rock@rockrong:~/Desktop/other/day5Text$ git branch
- master
但是老王需要在dev上工作,所以創(chuàng)建dev分支
rock@rockrong:~/Desktop/other/day5Text
git branch
- dev
master
rock@rockrong:~/Desktop/other/day5Textvim b.txt
rock@rockrong:~/Desktop/other/day5Textgit commit -m "b"
[dev b08d6ec] b
1 file changed, 1 insertion(+)
create mode 100644 b.txt老王推送到遠(yuǎn)程倉(cāng)庫(kù)
rock@rockrong:~/Desktop/other/day5Text$ git push origin dev #推送分支
對(duì)象計(jì)數(shù)中: 3, 完成.
Delta compression using up to 2 threads.
壓縮對(duì)象中: 100% (2/2), 完成.
寫入對(duì)象中: 100% (3/3), 274 bytes | 274.00 KiB/s, 完成.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:yangyang-git/day5Text.git
ae20ec5..b08d6ec dev -> dev
?過(guò)一陣子,老李也推送遠(yuǎn)程倉(cāng)庫(kù).這個(gè)時(shí)候推送失敗,因?yàn)闆_突了.
$ git push origin dev
To github.com:michaelliao/learngit.git
! [rejected] dev -> dev (non-fast-forward)
error: failed to push some refs to 'git@github.com:michaelliao/learngit.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.解決辦法是,把遠(yuǎn)程最新代碼拿來(lái)下,在本地合并,在push
抓取失敗,根據(jù)提示操作旦签,原因是沒(méi)有指定本地dev分支與遠(yuǎn)程origin/dev分支的鏈接
rock@rockrong:~/Desktop/day5Text$ git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
展開(kāi)對(duì)象中: 100% (3/3), 完成.
來(lái)自 github.com:yangyang-git/day5Text
ae20ec5..b08d6ec dev -> origin/dev
當(dāng)前分支沒(méi)有跟蹤信息查坪。
請(qǐng)指定您要合并哪一個(gè)分支。
詳見(jiàn) git-pull(1)宁炫。
?
git pull <遠(yuǎn)程> <分支>
?
如果您想要為此分支創(chuàng)建跟蹤信息偿曙,您可以執(zhí)行:
?
git branch --set-upstream-to=origin/<分支> dev設(shè)置本地和遠(yuǎn)程都有dev,就可以pull了
rock@rockrong:~/Desktop/day5Text
git pull #抓取分支
更新 ae20ec5..b08d6ec
Fast-forward
b.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 b.txt此時(shí)羔巢,兩個(gè)小伙伴之間就可以各自工作了望忆,然后只需要將各自的修改每次提交到dev分支
rock@rockrong:~/Desktop/day5Text
git add b.txt
rock@rockrong:~/Desktop/day5Textgit push origin dev
對(duì)象計(jì)數(shù)中: 3, 完成.
Delta compression using up to 2 threads.
壓縮對(duì)象中: 100% (2/2), 完成.
寫入對(duì)象中: 100% (3/3), 284 bytes | 284.00 KiB/s, 完成.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:yangyang-git/day5Text.git
b08d6ec..61c1d88 dev -> dev
rock@rockrong:~/Desktop/day5Textcd other/day5Text/
rock@rockrong:~/Desktop/other/day5Textcat b.txt
fghajdfja
hello
?注意:如果合并有沖突罩阵,需要手動(dòng)解決,解決的方法和分支管理中的解決沖突完全一樣启摄。解決后稿壁,提交,再push
實(shí)際的工作流程是:先pull[抓取]歉备,后push[推送]</pre>
總結(jié):
a.查看遠(yuǎn)程庫(kù)的信息傅是,使用git remote -v
b.本地新建的分支如果不推送到遠(yuǎn)程,對(duì)其他人都是不可見(jiàn)的
c.從本地推送分支蕾羊,使用命令git push origin branchname,如果推送失敗落午,則先用git pull抓取
d.在本地創(chuàng)建于遠(yuǎn)程分支的連接,使用命令git checkout -b branchname origin/branchname
e肚豺。從遠(yuǎn)程抓取分支溃斋,使用git pull,如果有沖突吸申,則要先解決沖突
7.標(biāo)簽管理
7.1創(chuàng)建標(biāo)簽
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" contenteditable="true" cid="n78" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 0px; width: inherit;">演示命令:
rock@rockrong:~/Desktop/day5Textgit tag
v1.0
rock@rockrong:~/Desktop/day5Textgit tag
v0.2
v1.0</pre>總結(jié):
- 命令
git tag <tagname>
用于新建一個(gè)標(biāo)簽,默認(rèn)為HEAD
截碴,也可以指定一個(gè)commit id梳侨;
- 命令
git tag -a <tagname> -m "blablabla..."
可以指定標(biāo)簽信息;
- 命令
git tag
可以查看所有標(biāo)簽日丹。
7.2操作標(biāo)簽
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" contenteditable="true" cid="n89" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 0px; width: inherit;">演示命令:
rock@rockrong:~/Desktop/day5Text$ git show v1.0 #查看指定標(biāo)簽的詳細(xì)信息
commit 61c1d8863fd7df3d20c156ace3bfa1d7882b636c (HEAD -> dev, tag: v1.0, origin/dev)
Author: yangyang-git 18501970795@163.com
Date: Mon Jul 2 10:52:50 2018 +0800
?
hello
?
diff --git a/b.txt b/b.txt
index 9022bb8..4bc9d07 100644
--- a/b.txt
+++ b/b.txt
@@ -1 +1,2 @@
fghajdfja
+hello創(chuàng)建標(biāo)簽走哺,攜帶標(biāo)簽的描述信息
rock@rockrong:~/Desktop/day5Text$ git tag -a v0.1 -m "version 0.1" e7423195
rock@rockrong:~/Desktop/day5Text
git tag -d v0.1
已刪除標(biāo)簽 'v0.1'(曾為 97026a8)
rock@rockrong:~/Desktop/day5Text$ git push origin --tags #將本地倉(cāng)庫(kù)中的標(biāo)簽推送到遠(yuǎn)程倉(cāng)庫(kù)
Total 0 (delta 0), reused 0 (delta 0)
To github.com:yangyang-git/day5Text.git
- [new tag] v0.2 -> v0.2
- [new tag] v1.0 -> v1.0
rock@rockrong:~/Desktop/day5Textgit push origin :refs/tags/v0.2
刪除遠(yuǎn)程倉(cāng)庫(kù)中的指定標(biāo)簽
remote: warning: Deleting a non-existent ref.
To github.com:yangyang-git/day5Text.git
- [deleted] v0.2To</pre>
總結(jié):
- 命令
git push origin <tagname>
可以推送一個(gè)本地標(biāo)簽;
- 命令
git push origin --tags
可以推送全部未推送過(guò)的本地標(biāo)簽哲虾;
- 命令
git tag -d <tagname>
可以刪除一個(gè)本地標(biāo)簽丙躏;
- 命令
git push origin :refs/tags/<tagname>
可以刪除一個(gè)遠(yuǎn)程標(biāo)簽。
三束凑、shell編程
1.簡(jiǎn)介
1.1什么是shell
把在終端運(yùn)行的命令保存到文件中晒旅,這個(gè)文件就是shell程序
簡(jiǎn)單的說(shuō),shell編程就是第Linux命令的邏輯化處理
1.2shell解析器的類型
bash汪诉,ash废恋,ksh等,默認(rèn)使用bash
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n109" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 0px; margin-top: 15px; width: inherit;">演示命令:
echo $SHELL
/bin/bash</pre>
1.3shell的作用
如果需要反復(fù)執(zhí)行某些Linux命令扒寄,則可以將這些命令寫到一個(gè)shell腳本中鱼鼓,然后每次只需要運(yùn)行一下這個(gè)腳本即可
2.第一個(gè)shell程序
2.1實(shí)現(xiàn)
打開(kāi)文本編輯器(可以使用 tou'ch命令來(lái)創(chuàng)建文件),新建一個(gè)文件 test.sh该编,擴(kuò)展名為 sh(sh代表shell)迄本,擴(kuò)展名并不影響腳本執(zhí)行,見(jiàn)名知意就好
代碼演示:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" contenteditable="true" cid="n118" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 0px; margin-top: 15px; width: inherit;">#!/bin/bash
打印hello world
echo "Hello World !"</pre>
2.2運(yùn)行
方式一:作為可執(zhí)行程序
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" contenteditable="true" cid="n122" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">touch test.sh
vim test.sh
chmod +x ./test.sh
./test.sh</pre>方式二:作為解釋器參數(shù)
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" contenteditable="true" cid="n124" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 0px; margin-top: 15px; width: inherit;">/bin/bash test.sh </pre>
3.shell中的變量
3.1變量的定義
定義:變量名=值
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" contenteditable="true" cid="n129" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 0px; margin-top: 15px; width: inherit;">演示命令:
your_name="zhangsan" #定義變量沒(méi)有your_name #使用是必須有
{num}
?注意:變量名外面的花括號(hào)是可選的上渴,加不加都行岸梨,加花括號(hào)是為了幫助解釋器識(shí)別變量的邊界
echo "his name is ${your_name}"</pre>
3.2只讀變量
readonly:只讀喜颁,將變量聲明為readonly,只讀變量的值不能發(fā)生改變
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" contenteditable="true" cid="n133" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 0px; margin-top: 15px; width: inherit;">myUrl="http://www.baidu.com"
readonly myUrl
myUrl="http://www.1000phone.com"
?運(yùn)行腳本,報(bào)錯(cuò):/bin/sh: NAME: This variable is read only</pre>
3.3刪除變量
unset:刪除變量
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" contenteditable="true" cid="n137" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 0px; margin-top: 15px; width: inherit;">代碼演示:
myUrl="http://www.baidu.com"
unset myUrl
echo $myUrl
?變量被刪除后不能再次使用曹阔。unset 命令不能刪除只讀變量半开。
以上實(shí)例執(zhí)行將沒(méi)有任何輸出</pre>
4.字符串和數(shù)組
4.1字符串
雙引號(hào)或者單引號(hào)
單引號(hào)的限制:
a.單引號(hào)中的任何字符都會(huì)原樣輸出,單引號(hào)字符串中的變量是無(wú)效的
b.單引號(hào)字符串中不能再出現(xiàn)單引號(hào)【對(duì)單引號(hào)進(jìn)行轉(zhuǎn)義后去不起作用】
總結(jié):
雙引號(hào):可以包含除了$赃份、`寂拆、\、‘‘之外的任意字符
單引號(hào):其中的任意字符都不會(huì)被解析抓韩,都會(huì)原樣輸出
反引號(hào):會(huì)將其中的內(nèi)容作為命令執(zhí)行
反斜線:轉(zhuǎn)義特定的字符纠永,如:&、*谒拴、^尝江、等
代碼演示:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" contenteditable="true" cid="n151" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 0px; margin-top: 15px; width: inherit;">代碼演示:
!/bin/bash
?
定義字符串
your_name='qinjx'
str="Hello, I know you are "$your_name"! \n"
?拼接字符串
your_name="qinjx"
greeting="hello, "{your_name} !"
echogreeting_1
?獲取字符串長(zhǎng)度
string="abcd"
echo ${#string} #輸出 4
?提取子字符串
string="1000phone is a great site"
echo ${string:1:4} #包頭包尾
?查找子字符串
string="1000phone is a great company"
echoexpr index "$string" is
#查找is在string中的位置,從1開(kāi)始計(jì)數(shù)
?注意: 以上腳本中 "`" 是反引號(hào),而不是單引號(hào) "'"英上,不要看錯(cuò)了哦</pre>
4.2數(shù)組
bash只支持一維數(shù)組炭序,不支持多維數(shù)組
并沒(méi)有限制數(shù)組的大小
數(shù)組元素的下標(biāo)也是從0開(kāi)始的,獲取數(shù)組中的元素使用下標(biāo)
定義數(shù)組:數(shù)組名=(值1 值2 值3....)
注意:shell中的數(shù)組元素之間使用空格分隔
代碼演示:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" contenteditable="true" cid="n160" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 0px; margin-top: 15px; width: inherit;">#數(shù)組的定義
arr1=(10 20 30 40)
echoarr2
?數(shù)組的使用
讀取數(shù)組中的元素
echo ${arr1[2]}
如果要讀取數(shù)組中的全部元素
echo ${arr2[@]}
?取得數(shù)組元素的個(gè)數(shù)
length=
length
或者
length=
length
取得數(shù)組單個(gè)元素的長(zhǎng)度
lengthn=
lengthn</pre>
5.shell中的運(yùn)算符
expr:是一款表達(dá)式計(jì)算工具苍日,使用它能夠完成表達(dá)式的求值操作
代碼演示:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" contenteditable="true" cid="n165" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 0px; margin-top: 15px; width: inherit;">val=
expr 1 + 2
echo "兩數(shù)之和為 : $val"
?1.算術(shù)運(yùn)算符 運(yùn)算符兩側(cè)一定要空格
val=
expr $a + $b
echo "a + b :a *
val"
?2.關(guān)系運(yùn)算符
[]中惭聂,前后都需要空格
if [
b ]
then
echo "b : a 等于 b"
else
echo "b: a 不等于 b"
fi
?3.邏輯運(yùn)算符
if [ 1 -lt 3 -a 2 -lt 3 ] 和下面的語(yǔ)句一個(gè)意思
if [ 1 -lt 3 ] && [ 2 -lt 3 ]
then
echo "ok"
fi</pre>
6.echo、printf相恃、test命令
1>echo
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" contenteditable="true" cid="n169" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">echo -e "OK! \n" # -e 開(kāi)啟轉(zhuǎn)義,\n 顯示換行
echo -e "OK! \c" # -e 開(kāi)啟轉(zhuǎn)義 \c 不換行
?
echodate
#顯示命令執(zhí)行結(jié)果注意: 這里使用的是反引號(hào) `, 而不是單引號(hào) '辜纲。
結(jié)果為:Thu Jul 24 10:08:46 CST 2014</pre>
2>printf
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" contenteditable="true" cid="n171" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">printf "%-10s %-8s %-4s\n" 姓名 性別 體重kg
printf "%-10s %-8s %-4.2f\n" 張三 男 66.1234
printf "%-10s %-8s %-4.2f\n" 李四 男 48.6543 </pre>注意:
%s %d %f都是格式替換符
-10s:指的是一個(gè)寬度為10的字符(-表示左對(duì)齊,沒(méi)有則表示右對(duì)齊)拦耐,任何字符都會(huì)填充在這10個(gè)字符內(nèi)耕腾,如果不足則使用空格自動(dòng)填充
-4.2f:指的是格式化為小數(shù),其中.2表示保留小數(shù)點(diǎn)后兩位
3>test
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" contenteditable="true" cid="n177" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">#1.數(shù)值測(cè)試
num1=100
num2=100
if test[num2]
then
echo '兩個(gè)數(shù)相等揩魂!'
else
echo '兩個(gè)數(shù)不相等幽邓!'
fi
?2.字符串測(cè)試
num1="hello"
num2="hello11"
if testnum2 #比較字符不能使用==,==只能比較數(shù)字
then
echo '兩個(gè)字符串相等!'
else
echo '兩個(gè)字符串不相等!'
fi
?3.文件測(cè)試
cd /bin
if test -e ./bash
then
echo '文件已存在!'
else
echo '文件不存在!'
fi</pre>test命令用來(lái)監(jiān)測(cè)某個(gè)條件是否成立,他可以進(jìn)行數(shù)值火脉,字符和文件的監(jiān)測(cè)
7.shell中的流程控制語(yǔ)句
7.1分支語(yǔ)句
if,case
代碼演示:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" contenteditable="true" cid="n184" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 0px; margin-top: 15px; width: inherit;">#if語(yǔ)句
單分支
if [ 1 -lt 3 ] && [ 2 -lt 3 ]
then
echo "ok"
fi
?雙分支
num1=
[1+5]
if else語(yǔ)句經(jīng)常與test命令結(jié)合使用
if test
[num2]
then
echo '兩個(gè)數(shù)字相等!'
else
echo '兩個(gè)數(shù)字不相等!'
fi
?多分支
a=10
b=20
if [b ]
then
echo "a 等于 b"
elif [b ]
then
echo "a 大于 b"
elif [b ]
then
echo "a 小于 b"
else
echo "沒(méi)有符合的條件"
fi
?
?case語(yǔ)句
echo '輸入 1 到 4 之間的數(shù)字:'
echo '你輸入的數(shù)字為:'
read aNum
case $aNum in
- echo '你選擇了 1'
;;- echo '你選擇了 2'
;;- echo '你選擇了 3'
;;- echo '你選擇了 4'
;;
*) echo '你沒(méi)有輸入 1 到 4 之間的數(shù)字'
;;
esac</pre>
7.2循環(huán)語(yǔ)句
for while until
代碼演示:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" contenteditable="true" cid="n189" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 0px; margin-top: 15px; width: inherit;">#for語(yǔ)句
需求:順序輸出當(dāng)前列表中的數(shù)字
for num in 1 2 3 4 5
do
echo "The value is: $num"
done
?
?需求:順序輸出字符串中的字符
for str in 'This is a string' "good" "well"
do
echo $str
done
?需求:遍歷數(shù)組中的所有元素
a=(1 2 3)
for x inx
donewhile語(yǔ)句
需求:輸出數(shù)字1到5
int=1
while(( int<=5 )) #(()) 像C語(yǔ)言一樣,去執(zhí)行代碼
do
echo $int
let "int++" #let 可以直接進(jìn)行加減 ++等操作
done
?
?需求:求1~10之間所有整數(shù)的和
i=1
sum=0
while (( i<10 ))
do
let sum+=i
let i++
done
echo $sum
?
?until語(yǔ)句
i=1
until (( i>10 ))
do
echo $i
((i++))
done
?</pre>
8.函數(shù)
代碼演示:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" contenteditable="true" cid="n193" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 0px; margin-top: 15px; width: inherit;">#無(wú)參無(wú)返回值
定義函數(shù)
demo()
{
echo 'hello world'
}調(diào)用函數(shù)
demo
?有返回值
funWithReturn(){
echo "輸入第一個(gè)數(shù)字: "
read aNum
echo "輸入第二個(gè)數(shù)字: "
read anotherNum
returnaNum+
? #這個(gè)$?表示上面函數(shù)的返回值
?有參有返回值
arg()
{
echo2 #第二個(gè)參數(shù)
echo* #用字符串的形式,顯示所有參數(shù)
return 123 #只能返回?cái)?shù)字,并且取值是0~255
}
?
arg 1 2 #調(diào)用arg函數(shù),并且傳入1 2作為參數(shù)$? 表示函數(shù)的返回值
echo $?</pre>