學習shell指令碼----mtk
#!/bin/bash
echo “-----------1.指明直譯器,chmod------------”
echo “hello world!”
echo “-----------2.定義變數------------”
MY_NAME=“zhigou” #變數用大寫,引用使用${}是好習慣
echo ${MY_NAME}
readonly MY_NAME_ONLY=“zhigou” #readonly修飾為只讀變數
#MY_NAME_ONLY=“zhigou2”
unset MY_NAMEONLY #unset刪除變數–無法刪除只讀變數
echo “-----------3.字串------------”
MY_NAME=“zhigou”
STR=‘this is ${zhigou}\n’ #單引號–原樣輸出,變數 轉義無效
echo $STR
STR=“this is ${MY_NAME}\n” #雙引號–可以變數 轉義字元
echo
{MY_NAME}" !" #字串可拼接
echo ${STR}
echo ${#STR} #獲取字串長度
#echo KaTeX parse error: Expected 'EOF', got '#' at position 13: {STR:1:4} #̲提取子串(1-4字元) ech…
echo “-----------4.傳遞引數------------”
echo “執行的檔名: $0”;
echo “引數個數為: $#”;
echo “第一個引數: $1”;
echo “第二個引數: $2”;
echo “第三個引數: $3”;
echo “所有引數作為一個字串顯示: $";
echo "----$ 演示----”
for i in “$*”; do
echo $i
done
echo “[email protected] 演示----”
for i in “[email protected]”; do
echo $i
done
echo “-----------5.陣列------------”
array=(1 2 3 4 5) #初始化陣列
array2[3]=123 #指定陣列元素值
array2[4]=234
echo “array的元素為KaTeX parse error: Expected 'EOF', got '#' at position 14: {array[*]}" #̲@全部列印 echo "arr…{array2[@]}” #@全部列印
echo "ayyay[3] = KaTeX parse error: Expected 'EOF', got '#' at position 14: {array[3]}" #̲{a[n]}列印指定元素
echo "array2的元素個數為KaTeX parse error: Expected '}', got '#' at position 2: {#̲array2[*]}" #{#A[*]}–陣列元素個數
echo “-----------6.運算子------------”
VAL=expr 2 + 2
VAL2=4
echo “2 + 2 = ${VAL} —運算子兩邊要有空格”
if [ $VAL == $VAL2 ]
then
echo “VAL == VAL2”
fi
echo “條件表示式要放在[]中,且要有空格:[ ${VAL} == ${VAL2} ]”