Shell編程——弱數據類型的腳本語言快速入門指南

目錄

Linux Shell

數據類型

變量類型

運算符

算術運算符

賦值運算符

拼接運算符

比較運算符

關系運算符

控制結構

順序結構

條件分支結構

if 條件語句

case 分支語句?

循環結構

for 循環

while 循環

until 循環

break 語句

continue語句

函數

函數定義?

函數名

函數體

返回值

參數

函數的局部性

簡單函數示例

函數的遞歸

實例操作

數組遍歷操作

九九乘法表


基本上,每一門編程語言,都能從數據類型、變量、運算符、控制結構、函數五個方面著手,初步掌握這些內容就可以快速入門為一名初級程序員。

Linux Shell

Shell是Linux命令行解釋器,主要用于執行操作系統命令和腳本。Linux Shell編程語言是一種用于與操作系統內核進行交互的命令行腳本語言,屬于解釋型、弱類型的動態語言。

數據類型

bool、數字、字符串、數組

變量類型

環境變量、用戶變量、全局變量、只讀變量

set let export readonly? env

運算符

算術運算符

+ - * / %

賦值運算符

=,沒有+=、-=、*=、/=這類復合賦值

拼接運算符

+= ,只能用于字符串的拼接

比較運算符

==、!=

關系運算符

-eq?? ?檢測兩個數是否相等,相等返回 true。
-ne?? ?檢測兩個數是否不相等,不相等返回 true。
-gt?? ?檢測左邊的數是否大于右邊的,如果是,則返回 true。
-lt?? ?檢測左邊的數是否小于右邊的,如果是,則返回 true。
-ge?? ?檢測左邊的數是否大于等于右邊的,如果是,則返回 true。
-le?? ?檢測左邊的數是否小于等于右邊的,如果是,則返回 true。

控制結構

順序結構

順序結構是最簡單的算法結構,語句與語句之間是按從上到下的順序進行的,它是由若干個依次執行的處理步驟組成的。

注:同一行可以有多個語句,只要用分號“;”來分隔即可。

條件分支結構

if 條件語句

可以細分為?if , if-else , if-elif-else 多種形式

hann@HannYang:~$ help -m if
NAMEif - Execute commands based on conditional.SYNOPSISif COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fiDESCRIPTIONExecute commands based on conditional.The `if COMMANDS' list is executed.  If its exit status is zero, then the`then COMMANDS' list is executed.  Otherwise, each `elif COMMANDS' list isexecuted in turn, and if its exit status is zero, the corresponding`then COMMANDS' list is executed and the if command completes.  Otherwise,the `else COMMANDS' list is executed, if present.  The exit status of theentire construct is the exit status of the last command executed, or zeroif no condition tested true.Exit Status:Returns the status of the last command executed.......

case 分支語句?

hann@HannYang:~$ help -m case
NAMEcase - Execute commands based on pattern matching.SYNOPSIScase WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esacDESCRIPTIONExecute commands based on pattern matching.Selectively execute COMMANDS based upon WORD matching PATTERN.  The`|' is used to separate multiple patterns.Exit Status:Returns the status of the last command executed.......

循環結構

for 循環

hann@HannYang:~$ help -m for
NAMEfor - Execute commands for each member in a list.SYNOPSISfor NAME [in WORDS ... ] ; do COMMANDS; doneDESCRIPTIONExecute commands for each member in a list.The `for' loop executes a sequence of commands for each member in alist of items.  If `in WORDS ...;' is not present, then `in "$@"' isassumed.  For each element in WORDS, NAME is set to that element, andthe COMMANDS are executed.Exit Status:Returns the status of the last command executed.......

while 循環

hann@HannYang:~$ help -m while
NAMEwhile - Execute commands as long as a test succeeds.SYNOPSISwhile COMMANDS; do COMMANDS; doneDESCRIPTIONExecute commands as long as a test succeeds.Expand and execute COMMANDS as long as the final command in the`while' COMMANDS has an exit status of zero.Exit Status:Returns the status of the last command executed.......

until 循環

hann@HannYang:~$ help -m until
NAMEuntil - Execute commands as long as a test does not succeed.SYNOPSISuntil COMMANDS; do COMMANDS; doneDESCRIPTIONExecute commands as long as a test does not succeed.Expand and execute COMMANDS as long as the final command in the`until' COMMANDS has an exit status which is not zero.Exit Status:Returns the status of the last command executed.......

break 語句

hann@HannYang:~$ help -m break
NAMEbreak - Exit for, while, or until loops.SYNOPSISbreak [n]DESCRIPTIONExit for, while, or until loops.Exit a FOR, WHILE or UNTIL loop.  If N is specified, break N enclosingloops.Exit Status:The exit status is 0 unless N is not greater than or equal to 1.......

continue語句

hann@HannYang:~$ help -m continue
NAMEcontinue - Resume for, while, or until loops.SYNOPSIScontinue [n]DESCRIPTIONResume for, while, or until loops.Resumes the next iteration of the enclosing FOR, WHILE or UNTIL loop.If N is specified, resumes the Nth enclosing loop.Exit Status:The exit status is 0 unless N is not greater than or equal to 1.......

兩者的區別?

break語句:

break語句用于退出本層循環,當執行到break會立即跳出當前循環,執行后續代碼。
在多層嵌套循環中,break只會跳出最近的一層循環。

continue語句:

continue語句用于結束本次循環,跳過本次循環中剩余的代碼,直接進入下一次循環。
在多層嵌套循環中,continue只會跳過最近的一層循環。

函數

hann@HannYang:~$ help -m function
NAMEfunction - Define shell function.SYNOPSISfunction name { COMMANDS ; } or name () { COMMANDS ; }DESCRIPTIONDefine shell function.Create a shell function named NAME.  When invoked as a simple command,NAME runs COMMANDs in the calling shell's context.  When NAME is invoked,the arguments are passed to the function as $1...$n, and the function'sname is in $FUNCNAME.Exit Status:Returns success unless NAME is readonly.

函數定義?

函數名

Shell函數用關鍵字 function 聲明,跟在后面的 name 即函數名。聲明后就用"函數名 [參數]"來調用函數。function 非必須,也能用函數名加一對括號?name() { ... } 來聲明定義函數。

函數體

函數名后的 { Commands; } 即函數體,是實現函數功能的主體。

返回值

Shell函數可以有一個返回值,可以使用return語句返回一個值。返回值的范圍是0到255之間,0表示成功,非零值表示錯誤。如果函數中沒有return語句,或者使用exit命令退出函數,則函數的返回值為退出命令的返回值。

參數

Shell函數可以通過參數接收輸入的值。在函數定義時,可以在括號中指定參數列表。參數可以在函數體中使用,也可以通過特殊變量$#獲取函數的參數個數,通過特殊變量$@獲取所有的參數。

函數的局部性

Shell函數的變量是局部的,即在函數內部定義的變量只在該函數內部可見,不會影響到函數外部的變量。如果要使用全局變量,需要在函數外部定義。

簡單函數示例

hann@HannYang:~$ function add {
>     num1=$1
>     num2=$2
>     sum=$((num1 + num2))
>     echo "The sum of $num1 and $num2 is $sum."
> }
hann@HannYang:~$ add 10 20
The sum of 10 and 20 is 30.

?語句比較少的函數可以在一行內完成,函數體中的語句間用分號“;”來分隔即可。

hann@HannYang:~$ function sub { num1=$1; num2=$2; sum=$((num1 - num2)); echo "The difference between $num1 and $num2 is $sum."; }
hann@HannYang:~$ sub 30 20
The difference between 30 and 20 is 10.

函數的遞歸

Shell函數可以遞歸調用自身,這在處理嵌套數據結構或遞歸算法時非常有用。需要注意的是,遞歸調用可能會導致棧溢出或效率低下的問題,因此在使用時需要謹慎。

示例:階乘函數

hann@HannYang:~$ factorial() {
>     if [ $1 -le 1 ]
>     then
>         echo 1
>     else
>         echo $(( $1 * $(factorial $(( $1 - 1 ))) ))
i
}>     fi
> }
hann@HannYang:~$ read -p "請輸入一個整數:" num
請輸入一個整數:6
hann@HannYang:~$ result=$(factorial $num)
hann@HannYang:~$ echo "$num 的階乘為 $result"
6 的階乘為 720

實例操作

數組遍歷操作

hann@HannYang:~$ for i in 1 2 3 4 5; do echo -n $i; done; echo
12345
hann@HannYang:~$ for i in {1..5}; do echo -n $i; done; echo
12345
hann@HannYang:~$ sum=0;for i in {1..100};do let sum+=i;done;echo $sum
5050
hann@HannYang:~$ for i in {1..5}{8..10}; do echo -n $i; done; echo
18191102829210383931048494105859510
hann@HannYang:~$ for i in {1..5}{8..10}; do echo -n $i" "; done; echo
18 19 110 28 29 210 38 39 310 48 49 410 58 59 510
hann@HannYang:~$ for i in {A..C}{a..d}; do echo -n $i" "; done; echo
Aa Ab Ac Ad Ba Bb Bc Bd Ca Cb Cc Cd

九九乘法表

hann@HannYang:~$ cat 99mul.sh

#!/bin/bashfor i in {1..9}
dofor j in {1..9}doif [ $j -le $i ]thenecho -n "$j*$i=$(($i*$j)) "fidoneecho ""
done

hann@HannYang:~$ bash 99mul.sh

1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

hann@HannYang:~$ cat 99mul.sh

#!/bin/bashfor i in {1..9}
dofor j in {1..9}doif [ $j -le $i ]thenprintf "%d*%d=%2d " $j $i $(($i*$j))fidoneecho ""
done

hann@HannYang:~$ bash 99mul.sh

1*1= 1
1*2= 2 2*2= 4
1*3= 3 2*3= 6 3*3= 9
1*4= 4 2*4= 8 3*4=12 4*4=16
1*5= 5 2*5=10 3*5=15 4*5=20 5*5=25
1*6= 6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7= 7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8= 8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9= 9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

hann@HannYang:~$ cat 99mul.sh?

#!/bin/bashfor i in {1..9}
dofor j in {1..9}doif [ $j -le $i ]thenif [ $j -eq 1 ]thenprintf "%d*%d=%d " $j $i $(($i*$j))elseprintf "%d*%d=%2d " $j $i $(($i*$j))fifidoneecho ""
done

hann@HannYang:~$ bash 99mul.sh

1*1=1
1*2=2 2*2= 4
1*3=3 2*3= 6 3*3= 9
1*4=4 2*4= 8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81


本文簡單提一下Linux Shell編程語言的入門要點,之后再對各個環節進行分類詳細舉例說明。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/39420.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/39420.shtml
英文地址,請注明出處:http://en.pswp.cn/news/39420.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Stable Diffusion Webui源碼剖析

1、關鍵python依賴 (1)xformers:優化加速方案。它可以對模型進行適當的優化來加速圖片生成并降低顯存占用。缺點是輸出圖像不穩定,有可能比不開Xformers略差。 (2)GFPGAN:它是騰訊開源的人臉修…

大數據掃盲(1): 數據倉庫與ETL的關系及ETL工具推薦

在數字化時代,數據成為了企業決策的關鍵支持。然而,隨著數據不斷增長,有效地管理和利用這些數據變得至關重要。數據倉庫和ETL工具作為數據管理和分析的核心,將幫助企業從龐雜的數據中提取有價值信息。 一、ETL是什么? …

【不限于聯想Y9000P電腦關蓋再打開時黑屏的解決辦法】

不限于聯想Y9000P電腦關蓋再打開時黑屏的解決辦法 問題的前言問題的出現問題擬解決 問題的前言 事情發生在昨天,更新了Win11系統后: 最惹人注目的三處地方就是: 1.可以查看時間的秒數了; 2.右鍵展示的內容變窄了; 3.按…

Pycharm 雙擊啟動失敗?

事故 雙擊 Pycharm 后,出現加載工程,我不想加載這個工程,就點擊了彈出的 cancle 取消按鈕。然后再到桌面雙擊 Pycharm 卻發現無法啟動了。哪怕以管理員權限運行也沒用,就是不出界面。 原因未知 CtrlshiftESC 打開后臺&#xff…

【騰訊云 Cloud Studio 實戰訓練營】Hexo 框架 Butterfly 主題搭建個人博客

什么是Cloud Studio Cloud Studio 是基于瀏覽器的集成式開發環境(IDE),為開發者提供了一個永不間斷的云端工作站。用戶在使用 Cloud Studio 時無需安裝,隨時隨地打開瀏覽器就能在線編程。 ? Hexo 博客成品展示 本人博客如下&…

leetcode268. 丟失的數字

這題簡單的有點過分了吧。。。 一開始還納悶會不會有重復的元素,后來看到[0,n]范圍,那么肯定有n1個數字,然后要在n 個數字里面找誰沒有,那肯定沒有重復的元素,如果有重復,就不止缺少一個元素了。 思路&am…

【Spring】-Spring項目的創建

作者:學Java的冬瓜 博客主頁:?冬瓜的主頁🌙 專欄:【Framework】 主要內容:創建spring項目的步驟:先創建一個maven項目,再在pom.xml中添加spring框架支持,最后寫一個啟動類。 文章目…

Field injection is not recommended

文章目錄 1. 引言2. 不推薦使用Autowired的原因3. Spring提供了三種主要的依賴注入方式3.1. 構造函數注入(Constructor Injection)3.2. Setter方法注入(Setter Injection)3.3. 字段注入(Field Injection) 4…

03 QT基本控件和功能類

一 進度條 、水平滑動條 垂直滑動條 當在QT中,在已知類名的情況下,要了解類的構造函數 常用屬性 及 信號和槽 常用api 特征:可以獲取當前控件的值和設置它的當值 ---- int ui->progressBar->setValue(value); //給進度條設置一個整型值 ui->progressBar->value…

計算機視覺五大核心研究任務全解:分類識別、檢測分割、人體分析、三維視覺、視頻分析

目錄 一、引言1.1 計算機視覺的定義1.1.1 核心技術1.1.2 應用場景 1.2 歷史背景及發展1.2.1 1960s-1980s: 初期階段1.2.2 1990s-2000s: 機器學習時代1.2.3 2010s-現在: 深度學習的革命 1.3 應用領域概覽1.3.1 工業自動化1.3.2 醫療圖像分析1.3.3 自動駕駛1.3.4 虛擬現實與增強現…

【Linux】進程調度

進程調度 硬件向OS發送時間中斷 --> 系統時鐘硬件會進行時間計數,每隔一段很短的時間會向OS發送時鐘中斷,處理中斷,檢測進程時間片 --> 收到中斷,OS就會不斷定期地執行對應的時鐘中斷處理方法,檢查當前進程的時…

山東布谷科技直播軟件開發WebRTC技術:建立實時通信優質平臺

在數字化的時代,實時通信成為了人們遠程交流的主要方式,目前市場上也出現了很多帶有實時通信交流的軟件,實時通信符合人們現在的需求,所以在直播軟件開發過程中,開發者也運用了實時通信技術為直播軟件加入了實時通信的…

【計算機視覺|生成對抗】生成對抗網絡(GAN)

本系列博文為深度學習/計算機視覺論文筆記,轉載請注明出處 標題:Generative Adversarial Nets 鏈接:Generative Adversarial Nets (nips.cc) 摘要 我們提出了一個通過**對抗(adversarial)**過程估計生成模型的新框架…

mybatisplus學習筆記

1.踩過的坑 1.MybatisPlus 要與其代碼生成器的版本一致; 2.要使用新版代碼(3.5.1及以上)生成器則要使用springboot3,如果用springboot2使用新版代碼生成器會導致builder.parent(“com.sdfsf”) // 設置父包名》重復!&…

2.阿里云對象存儲OSS

1.對象存儲概述 文件上傳,是指將本地圖片、視頻、音頻等文件上傳到服務器上,可以供其他用戶瀏覽或下載的過程。文件上傳在項目中應用非常廣泛,我們經常發抖音、發朋友圈都用到了文件上傳功能。 實現文件上傳服務,需要有存儲的支持…

【概念理解】STM32中的sprintf()函數

sprintf()函數 這個函數在 stdio.h中;可以將格式化的數據寫入到一個字符串緩沖區中。 int sprintf(char *str, const char *format, ...);str:指向字符數組的指針,即用于存儲格式化后字符串的緩沖區。format:格式化字符串&#…

(十六)大數據實戰——安裝使用mysql版的hive服務

前言 hive默認使用的是內嵌據庫derby,Derby 是一個嵌入式數據庫,可以輕松地以庫的形式集成到應用程序中。它不需要獨立的服務器進程,所有的數據存儲在應用程序所在的文件系統中。為了支持hive服務更方便的使用,我們使用mysql數據…

Centos 8和Centos 7中配置阿里云的 yum 源

YUM源簡介 yum是一種在Linux環境下安裝、更新和刪除軟件包的軟件管理器。通過yum,用戶可以輕松地從軟件倉庫中搜索和安裝包含所需軟件的軟件包,并自動處理所需的依賴關系。此外,yum還可以與其他軟件管理工具配合使用,例如rpm。它…

【實戰】十一、看板頁面及任務組頁面開發(一) —— React17+React Hook+TS4 最佳實踐,仿 Jira 企業級項目(二十三)

文章目錄 一、項目起航:項目初始化與配置二、React 與 Hook 應用:實現項目列表三、TS 應用:JS神助攻 - 強類型四、JWT、用戶認證與異步請求五、CSS 其實很簡單 - 用 CSS-in-JS 添加樣式六、用戶體驗優化 - 加載中和錯誤狀態處理七、Hook&…

c語言每日一練(8)

前言:每日一練系列,每一期都包含5道選擇題,2道編程題,博主會盡可能詳細地進行講解,令初學者也能聽的清晰。每日一練系列會持續更新,暑假時三天之內必有一更,到了開學之后,將看學業情…