shell 指令碼入門

2021-08-15 10:28:03 字數 1509 閱讀 2520

shell 是乙個用 c 語言編寫的程式,它是使用者使用 linux 的橋梁。shell 既是一種命令語言,又是一種程式語言。shell 是指一種應用程式,這個應用程式提供了乙個介面,使用者通過這個介面訪問作業系統核心的服務。ken thompson 的 sh 是第一種 unix shell,windows explorer 是乙個典型的圖形介面 shell。shell 指令碼(shell script),是一種為 shell 編寫的指令碼程式。業界所說的 shell 通常都是指 shell 指令碼,但讀者朋友要知道,shell 和 shell script 是兩個不同的概念。

下面在unbuntu的環境下建立乙個簡單的shell,寫乙個整數求和小應用:

1、ctrl+alt+t進入unbuntu終端,進入指定的目錄:

$ cd home/wuchengfeng/downloads  

$ vi countfun.sh

#!/bin/bash

sum=0

for((i=0;i<101;i++))

dosum=$((sum+i))

done

echo "1+2+3+...100="$sum

4、儲存指令碼**:

$ source countfun.sh

5、新增操作許可權:

$ chmod +x countfun.sh

6、執行shell檔案,列印結果:

$   ./countfun.sh

控制台輸出結果:1+2+3+...100=5050;

例項2:求100以內的偶數、奇數和

shell 指令碼的運算操作符號之間必須有空格`expr $count % 2`

#!/bin/bash

sum1=0

sum2=0

count=0

while(($count<100))

do val=`expr $count % 2`

echo "計數器: $val"

if [ $val -eq 0 ]

then

sum1=$(($sum1+$count))

else

sum2=`expr $sum2 + $count`

ficount=$(($count+1))

done

echo "100以內的偶數和: $sum1"

echo "100以內的奇數和: $sum2"

Shell指令碼(三) Shell指令碼入門

1 指令碼格式 指令碼以 bin bash開頭 指定解析器 2 第乙個shell指令碼 helloworld 1 需求 建立乙個shell指令碼,輸出helloworld 2 案例實操 atguigu hadoop101 datas touch helloworld.sh atguigu hadoo...

shell指令碼入門

str wxz echo 輸出 3 提取子字串 以下例項從字串第 2 個字元開始擷取 4 個字元 string the shell script is great echo 輸出 he s查詢子字串 查詢字元 i 或 s 的位置 string runoob is a great company ec...

Shell指令碼入門

一 shell簡介 shell指令碼,就是利用shell的命令解釋的功能,對乙個純文字的檔案進行解析,然後執行這些功能,也可以說shell指令碼就是一系列命令的集合。shell可以直接使用在win unix linux上面,並且可以呼叫大量系統內部的功能來解釋執行程式,如果熟練掌握shell指令碼,...