Liunx操作day-05

一、上堂回顧

安裝git

sudo apt-get install git

創(chuàng)建版本庫【本地版本庫】

普通目錄,git init

創(chuàng)建文件在工作區(qū),git add filename git commit -m "日志描述"

時光穿梭機(jī)

版本回退:git reset --hard HEAD^ 或者git reset --hard commit id

工作區(qū)/版本庫/暫存區(qū)

管理修改:git status / git diff HEAD -- filename :查看版本庫和工作區(qū)之間的不同

撤銷修改:

a.修改了工作區(qū)中的文件,但是還沒有add

git checkout -- filename :丟棄工作區(qū)的修改

b.修改了工作區(qū)中的文件,add到暫存區(qū),但是還沒有commit

git reset HEAD filename:丟棄暫存區(qū)的修改

git checkout -- filename

c.修改了工作區(qū)中的文件,并add,commit

回退版本

刪除文件:

刪除工作區(qū)中的文件:rm filename

刪除版本庫中的文件:git rm filename

遠(yuǎn)程倉庫

添加ssh key【建立當(dāng)前計算機(jī)和遠(yuǎn)程倉庫之間的連接】

添加遠(yuǎn)程倉庫:先有本地倉庫,然后根據(jù)本地倉庫創(chuàng)建遠(yuǎn)程倉庫

git remote add origin git@github.com:username/learngit.git:建立班底和遠(yuǎn)程倉庫之間的連接

git push -u origin master:將當(dāng)前的master分支推送到遠(yuǎn)程倉庫

git push origin master:以后再修改之后,可以再次推送

克隆倉庫:先有遠(yuǎn)程倉庫,然后將遠(yuǎn)程倉庫克隆到本地

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分支

一般情況下,每個bug都需要使用一個分支來進(jìn)行解決,解決后,分支刪除

git stash:可以把當(dāng)前工作現(xiàn)場儲存起來,然后先進(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:~/Desktopcd day5Text/ rock@rockrong:~/Desktop/day5Text git branch

  • master
    rock@rockrong:~/Desktop/day5Textvim README.md #假如有工作未完成 rock@rockrong:~/Desktop/day5Text git stash #封存工作現(xiàn)場
    保存工作目錄和索引狀態(tài) WIP on dev: e742319 Initial commit

創(chuàng)建bug分支

rock@rockrong:~/Desktop/day5Textgit checkout -b bug-01 切換到一個新分支 'bug-01' rock@rockrong:~/Desktop/day5Text vim README.md
rock@rockrong:~/Desktop/day5Textgit add README.md rock@rockrong:~/Desktop/day5Text git commit -m "fixed a bug"
[bug-01 235be14] fixed a bug
1 file changed, 2 insertions(+), 1 deletion(-)
rock@rockrong:~/Desktop/day5Textgit checkout master 切換到分支 'master' #bug修復(fù) 合并 rock@rockrong:~/Desktop/day5Text git 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

解開封存.這時會沖突,解決沖突

rock@rockrong:~/Desktop/day5Text$ git stash pop</pre>

總結(jié):

修復(fù)bug時,創(chuàng)建一個新的分支,進(jìn)行bug的修復(fù),然后合并,最后刪除

當(dāng)手頭的工作沒有完成的時候,使用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/day5Textgit checkout -b feature1 切換到一個新分支 'feature1' rock@rockrong:~/Desktop/day5Text touch a.txt
    rock@rockrong:~/Desktop/day5Textgit add a.txt rock@rockrong:~/Desktop/day5Text git 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/day5Textgit status 位于分支 feature1 無文件要提交,干凈的工作區(qū) rock@rockrong:~/Desktop/day5Text vim a.txt
    rock@rockrong:~/Desktop/day5Textgit add a.txt rock@rockrong:~/Desktop/day5Text git status
    位于分支 feature1
    要提交的變更:
    (使用 "git reset HEAD <文件>..." 以取消暫存)
    ?
    修改: a.txt
    ?
    rock@rockrong:~/Desktop/day5Textgit commit -m "add hello" [feature1 af31c25] add hello 1 file changed, 1 insertion(+) rock@rockrong:~/Desktop/day5Text git checkout dev
    切換到分支 'dev'
    rock@rockrong:~/Desktop/day5Textgit branch -d feature1 #正常刪除 error: 分支 'feature1' 沒有完全合并。 如果您確認(rèn)要刪除它,執(zhí)行 'git branch -D feature1'。 rock@rockrong:~/Desktop/day5Text git branch -D feature1 #強(qiáng)制刪除
    已刪除分支 feature1(曾為 af31c25)。</pre>

總結(jié):

每開發(fā)一個新的功能【版本迭代】,最好新建一個分支來進(jìn)行操作

如果需要丟棄一個還沒有被合并的分支,使用命令 git branch -D branch-name

6.6多人協(xié)作

當(dāng)你從遠(yuǎn)程倉克隆時,實際上git將本地的master和遠(yuǎn)程的master對應(yīng)起來了,并且遠(yuǎn)程倉庫的默認(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 #查看遠(yuǎn)程庫的信息 origin rock@rockrong:~/Desktop/day5Text git remote -v
origin git@github.com:yangyang-git/day5Text.git (fetch) #抓取地址
origin git@github.com:yangyang-git/day5Text.git (push) #推送地址</pre>

1>推送分支

推送分支:把該分支上的所有的本地提交推送到遠(yuǎn)程庫,推送時,要指定本地分支

<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
對象計數(shù)中: 4, 完成.
Delta compression using up to 2 threads.
壓縮對象中: 100% (2/2), 完成.
寫入對象中: 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)程倉庫

a.master分支時主分支,因此要時刻與遠(yuǎn)程保持同步

b.dev是一個開發(fā)分支,團(tuán)隊所有的成員都在上面工作,所以也需要推送到遠(yuǎn)程倉庫

c.bug分支只是修復(fù)一個bug,就沒必要推送到遠(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;">演示命令:

老王和老李,在做同一個項目

rock@rockrong:~/Desktop/day5Textcd .. rock@rockrong:~/Desktop mkdir other
rock@rockrong:~/Desktopcd other/ rock@rockrong:~/Desktop/other git 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
接收對象中: 100% (10/10), 完成.
處理 delta 中: 100% (2/2), 完成.
rock@rockrong:~/Desktop/other$ cd day5Text/

老王抓取下來只有master

rock@rockrong:~/Desktop/other/day5Text$ git branch

  • master

但是老王需要在dev上工作,所以創(chuàng)建dev分支

rock@rockrong:~/Desktop/other/day5Textgit checkout -b dev origin/dev 分支 'dev' 設(shè)置為跟蹤來自 'origin' 的遠(yuǎn)程分支 'dev'。 切換到一個新分支 'dev' rock@rockrong:~/Desktop/other/day5Text git branch

  • dev
    master
    rock@rockrong:~/Desktop/other/day5Texttouch b.txt rock@rockrong:~/Desktop/other/day5Text vim b.txt
    rock@rockrong:~/Desktop/other/day5Textgit add b.txt rock@rockrong:~/Desktop/other/day5Text git commit -m "b"
    [dev b08d6ec] b
    1 file changed, 1 insertion(+)
    create mode 100644 b.txt

老王推送到遠(yuǎn)程倉庫

rock@rockrong:~/Desktop/other/day5Text$ git push origin dev #推送分支
對象計數(shù)中: 3, 完成.
Delta compression using up to 2 threads.
壓縮對象中: 100% (2/2), 完成.
寫入對象中: 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
?

過一陣子,老李也推送遠(yuǎ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)程最新代碼拿來下,在本地合并,在push

抓取失敗,根據(jù)提示操作,原因是沒有指定本地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
展開對象中: 100% (3/3), 完成.
來自 github.com:yangyang-git/day5Text
ae20ec5..b08d6ec dev -> origin/dev
當(dāng)前分支沒有跟蹤信息。
請指定您要合并哪一個分支。
詳見 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/day5Textgit branch --set-upstream-to=origin/dev dev 分支 'dev' 設(shè)置為跟蹤來自 'origin' 的遠(yuǎn)程分支 'dev'。 #設(shè)置跟蹤 rock@rockrong:~/Desktop/day5Text git pull #抓取分支
更新 ae20ec5..b08d6ec
Fast-forward
b.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 b.txt

此時,兩個小伙伴之間就可以各自工作了,然后只需要將各自的修改每次提交到dev分支

rock@rockrong:~/Desktop/day5Textvim b.txt rock@rockrong:~/Desktop/day5Text git add b.txt
rock@rockrong:~/Desktop/day5Textgit commit -m "hello" [dev 61c1d88] hello 1 file changed, 1 insertion(+) rock@rockrong:~/Desktop/day5Text git push origin dev
對象計數(shù)中: 3, 完成.
Delta compression using up to 2 threads.
壓縮對象中: 100% (2/2), 完成.
寫入對象中: 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 .. rock@rockrong:~/Desktop cd other/day5Text/
rock@rockrong:~/Desktop/other/day5Textgit 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 展開對象中: 100% (3/3), 完成. 來自 github.com:yangyang-git/day5Text b08d6ec..61c1d88 dev -> origin/dev 更新 b08d6ec..61c1d88 Fast-forward b.txt | 1 + 1 file changed, 1 insertion(+) rock@rockrong:~/Desktop/other/day5Text cat b.txt
fghajdfja
hello
?

注意:如果合并有沖突,需要手動解決,解決的方法和分支管理中的解決沖突完全一樣。解決后,提交,再push

實際的工作流程是:先pull[抓取],后push[推送]</pre>

總結(jié):

a.查看遠(yuǎn)程庫的信息,使用git remote -v

b.本地新建的分支如果不推送到遠(yuǎ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 #創(chuàng)建標(biāo)簽,默認(rèn)創(chuàng)建的是當(dāng)前最新提交的標(biāo)簽 rock@rockrong:~/Desktop/day5Text git tag
v1.0
rock@rockrong:~/Desktop/day5Textgit tag v0.2 ae20ec5 #為指定commit id創(chuàng)建標(biāo)簽 rock@rockrong:~/Desktop/day5Text git tag
v0.2
v1.0</pre>

總結(jié):

  • 命令git tag <tagname>用于新建一個標(biāo)簽,默認(rèn)為HEAD,也可以指定一個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/day5Textgit tag #查看當(dāng)前分支下的標(biāo)簽 v0.1 v0.2 v1.0 rock@rockrong:~/Desktop/day5Text git tag -d v0.1
已刪除標(biāo)簽 'v0.1'(曾為 97026a8)
rock@rockrong:~/Desktop/day5Text$ git push origin --tags #將本地倉庫中的標(biāo)簽推送到遠(yuǎn)程倉庫
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 tag -d v0.2 #刪除本地倉庫中的標(biāo)簽 已刪除標(biāo)簽 'v0.2'(曾為 ae20ec5) rock@rockrong:~/Desktop/day5Text git push origin :refs/tags/v0.2

刪除遠(yuǎn)程倉庫中的指定標(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>可以推送一個本地標(biāo)簽;
  • 命令git push origin --tags可以推送全部未推送過的本地標(biāo)簽;
  • 命令git tag -d <tagname>可以刪除一個本地標(biāo)簽;
  • 命令git push origin :refs/tags/<tagname>可以刪除一個遠(yuǎn)程標(biāo)簽。

三、shell編程

1.簡介

1.1什么是shell

把在終端運行的命令保存到文件中,這個文件就是shell程序

簡單的說,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命令,則可以將這些命令寫到一個shell腳本中,然后每次只需要運行一下這個腳本即可

2.第一個shell程序

2.1實現(xiàn)

打開文本編輯器(可以使用 tou'ch命令來創(chuàng)建文件),新建一個文件 test.sh,擴(kuò)展名為 sh(sh代表shell),擴(kuò)展名并不影響腳本執(zhí)行,見名知意就好

代碼演示:

<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運行

方式一:作為可執(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" #定義變量沒有,賦值符中間不允許有空格 echoyour_name #使用是必須有num=10 echo{num}
?

注意:變量名外面的花括號是可選的,加不加都行,加花括號是為了幫助解釋器識別變量的邊界

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"
?

運行腳本,報錯:/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 命令不能刪除只讀變量。

以上實例執(zhí)行將沒有任何輸出</pre>

4.字符串和數(shù)組

4.1字符串

雙引號或者單引號

單引號的限制:

a.單引號中的任何字符都會原樣輸出,單引號字符串中的變量是無效的

b.單引號字符串中不能再出現(xiàn)單引號【對單引號進(jìn)行轉(zhuǎn)義后去不起作用】

總結(jié):

雙引號:可以包含除了$、`、\、‘‘之外的任意字符

單引號:其中的任意字符都不會被解析,都會原樣輸出

反引號:會將其中的內(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" !" greeting_1="hello,{your_name} !"
echo greetinggreeting_1
?

獲取字符串長度

string="abcd"
echo ${#string} #輸出 4
?

提取子字符串

string="1000phone is a great site"
echo ${string:1:4} #包頭包尾
?

查找子字符串

string="1000phone is a great company"
echo expr index "$string" is #查找is在string中的位置,從1開始計數(shù)
?

注意: 以上腳本中 "`" 是反引號,而不是單引號 "'",不要看錯了哦</pre>

4.2數(shù)組

bash只支持一維數(shù)組,不支持多維數(shù)組

并沒有限制數(shù)組的大小

數(shù)組元素的下標(biāo)也是從0開始的,獲取數(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)
echo arr1 #打印出第0個元素 arr2=( 10 20 30 40 ) echoarr2
?

數(shù)組的使用

讀取數(shù)組中的元素

echo ${arr1[2]}

如果要讀取數(shù)組中的全部元素

echo ${arr2[@]}
?

取得數(shù)組元素的個數(shù)

length={#arr1[@]} echolength

或者

length={#arr1[*]} echolength

取得數(shù)組單個元素的長度

lengthn={#arr1[3]} echolengthn</pre>

5.shell中的運算符

expr:是一款表達(dá)式計算工具,使用它能夠完成表達(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ù)運算符 運算符兩側(cè)一定要空格

val=expr $a + $b
echo "a + b : val" ? val=`expra * b` echo "a * b :val"
?

2.關(guān)系運算符

[]中,前后都需要空格

if [ a -eqb ]
then
echo "a -eqb : a 等于 b"
else
echo "a -eqb: a 不等于 b"
fi
?

3.邏輯運算符

if [ 1 -lt 3 -a 2 -lt 3 ] 和下面的語句一個意思

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 開啟轉(zhuǎn)義,\n 顯示換行
echo -e "OK! \c" # -e 開啟轉(zhuǎn)義 \c 不換行
?
echo date #顯示命令執(zhí)行結(jié)果

注意: 這里使用的是反引號 `, 而不是單引號 '。

結(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:指的是一個寬度為10的字符(-表示左對齊,沒有則表示右對齊),任何字符都會填充在這10個字符內(nèi),如果不足則使用空格自動填充

-4.2f:指的是格式化為小數(shù),其中.2表示保留小數(shù)點后兩位

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ù)值測試
num1=100
num2=100
if test [num1] -eq[num2]
then
echo '兩個數(shù)相等!'
else
echo '兩個數(shù)不相等!'
fi
?

2.字符串測試

num1="hello"
num2="hello11"
if test num1 =num2 #比較字符不能使用==,==只能比較數(shù)字
then
echo '兩個字符串相等!'
else
echo '兩個字符串不相等!'
fi
?

3.文件測試

cd /bin
if test -e ./bash
then
echo '文件已存在!'
else
echo '文件不存在!'
fi</pre>

test命令用來監(jiān)測某個條件是否成立,他可以進(jìn)行數(shù)值,字符和文件的監(jiān)測

7.shell中的流程控制語句

7.1分支語句

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語句

單分支

if [ 1 -lt 3 ] && [ 2 -lt 3 ]
then
echo "ok"
fi
?

雙分支

num1=[2*3] num2=[1+5]

if else語句經(jīng)常與test命令結(jié)合使用

if test [num1] -eq[num2]
then
echo '兩個數(shù)字相等!'
else
echo '兩個數(shù)字不相等!'
fi
?

多分支

a=10
b=20
if [ a ==b ]
then
echo "a 等于 b"
elif [ a -gtb ]
then
echo "a 大于 b"
elif [ a -ltb ]
then
echo "a 小于 b"
else
echo "沒有符合的條件"
fi
?
?

case語句

echo '輸入 1 到 4 之間的數(shù)字:'
echo '你輸入的數(shù)字為:'
read aNum
case $aNum in

  1. echo '你選擇了 1'
    ;;
  2. echo '你選擇了 2'
    ;;
  3. echo '你選擇了 3'
    ;;
  4. echo '你選擇了 4'
    ;;
    *) echo '你沒有輸入 1 到 4 之間的數(shù)字'
    ;;
    esac</pre>
7.2循環(huán)語句

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語句

需求:順序輸出當(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 in {a[*]} do echox
done

while語句

需求:輸出數(shù)字1到5

int=1
while(( int<=5 )) #(()) 像C語言一樣,去執(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語句

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;">#無參無返回值

定義函數(shù)

demo()
{
echo 'hello world'
}

調(diào)用函數(shù)

demo
?

有返回值

funWithReturn(){
echo "輸入第一個數(shù)字: "
read aNum
echo "輸入第二個數(shù)字: "
read anotherNum
return ((aNum+anotherNum)) } funWithReturn echo? #這個$?表示上面函數(shù)的返回值
?

有參有返回值

arg()
{
echo 1 #第一個參數(shù) echo2 #第二個參數(shù)
echo # #參數(shù)個數(shù) echo* #用字符串的形式,顯示所有參數(shù)
return 123 #只能返回數(shù)字,并且取值是0~255
}
?
arg 1 2 #調(diào)用arg函數(shù),并且傳入1 2作為參數(shù)

$? 表示函數(shù)的返回值

echo $?</pre>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容