Linux的shell脚本编程详细总结Linux的shell脚本
《大數據和人工智能交流》頭條號向廣大初學者新增C 、Java 、Python 、Scala、javascript 等目前流行的計算機、大數據編程語言,希望大家以后關注本頭條號更多的內容。
Linux的shell腳本編程
一、最簡單的shell
1、Shell的結構:
(1)、#!指定執行腳本的shell
(2)、#注釋行
(3)、控制結構
2、創建shell的步驟:
(1)、使用vi編輯器創建以.sh結尾的文件 :vi demo.sh
(2)、編輯相關shell程序,編寫完畢后加執行權限:
chmod u+x demo.sh
(3)、執行程序 ./demo.sh
也可以使用sh demo.sh來執行
使用 sh -x demo.sh中的x表示腳本的執行過程
使用 sh -n demo.sh不執行腳本,只是檢查語法的錯誤,返回所有錯誤信息
二、shell的變量
Shell變量包括:臨時變量和環境變量,臨時變量時shell程序內部定義的,對其他程序不可見,包括用戶自定義變量和位置變量;環境變量就是全局變量,不隨著shell腳本的結束而消失
在使用變量值的時候,要在變量名前加前綴"#34;
變量賦值用"=",注意兩邊不能有空格
變量賦值舉例:
1、 NUM=1
2、將一個命令的執行結果賦給變量,例如
time=`date`
time=(date +%F) echo $time
3、將一個變量賦給另一個變量:A=$B
查看變量的值:echo $A
字符串用單引號或者雙引號
雙引號有自動解析變量的功能
例如:
sum=100
echo "The total is $sum"
set為顯示所有的環境變量
刪除變量unset
三、位置變量和特殊變量
1、位置變量
Shell解釋執行用戶命令的時候,將命令行的第一部分作為命令名,其他部分作為參數。有出現在命令行上的位置確定的參數成為位置參數
例如:ls -l file1 file2 file3
$0位置被個程序的文件名占用,其它剩余位置$1 $2 $3…$9
例如:在test.sh中有如下內容
mkdir files$1_s00$2_class$3
執行時候./test.sh 001 1 001,表示$0被test.sh占用、$1被001占用、1被$2占用,001被$3占用
2、特殊變量
$*這個程序的所有參數
$#這個程序的參數個數
$這個程序的 PID
$!執行上一個后臺的PID
$?執行上一個命令的返回值
例如有test.sh程序,內容如下:
echo '$# is:' $#
echo '$* is:' $*
echo '$? is:' $?
echo '$ is:' $
echo '$0 is:' $0
運行 ./test.sh p1 p2 p3 后查看結果
四、shell的命令
1、Read命令:從鍵盤讀入數據,賦給變量
例如 read x 100 ; echo $x
如程序test.sh:
read a1 a2 a3
echo "the a1 value is $a1"
echo "the a2 value is $a2"
echo "the a3 value is $a3"
則執行的時候
./test.sh 100 200 300
2、expr指令:對整型變量進行算術運算
例如expr 100 + 200注意加號左右有空格
del@box:/temp$ expr 100 + 200
300
另外注意乘法為" \*",要有轉義符
例如3 \* 5
復雜的運算:expr `expr 6 + 9` / 3
將運算的結果賦予變量 x=`expr 1 + 2`
3、變量測試語句:
測試變量是否相等、為空等情況。語法:test 測試條件;測試范圍:整數、字符串、文件
(1)字符串測試:
test str1=str2 測試字符串是否相等
test str1!=str2 測試字符串是否不相等
test str1 測試字符串是否不為空
test -n str1 測試字符串是否不為空
test -z str1 測試字符串是否為空
(2)測試整數
test int1 -eq int2 測試int1和int2是否相等
test int1 -ge int2 測試int1是否>=int2
test int1 -gt int2 測試int1是否>int2
test int1 -le int2 測試int1是否<=int2
test int1 -lt int2 測試int1是否<int2
</int2
test int1 -ne int2 測試int1和int2是否不相等
(3)文件測試
test -d file 指定文件是否目錄
test -f file 指定文件是否常規文件
test -x file 指定文件是否可執行
test -r file 指定文件是否可讀
test -w file 指定文件是否可寫
test -a file 指定文件是否存在
test -s file 文件大小是否非0
測試語句一般不單獨使用,一般配合if語句來判斷,如:
if test -d $1 then
…….
fi
變量測試語句也可以用[]簡化,例如:
test -d $1等價于 [ -d $1 ] 注意:中括號左右的表達式要有空格
五、流程控制
1、選擇結構if語句
(1) If [ ]; then
fi
(2) If [ ]; then
else
fi
例如:
If [ $# -ne 2 ]; then
echo "not enough parameters"
exit 0 //0表示正常退出
fi
If[ $1 -eq $2 ];then
echo "$1 equals $2"
fi
(3) If [ ] then
elif [ ]
elif [ ]
else
例如:test.sh內容如下
read file_name
if [ -d $file_name ]
then
echo "This is directory!!!"
elif [ -f $file_name ]
then
echo "This is file!!!"
else
echo "This is nothing!!!"
fi
條件組合查詢:
-a :邏輯與,條件都成立的時候結果為真
-o :邏輯或,只要有一個條件成立,結果為真
例如:test.sh內容如下
read file_name
if [ -d $file_name ]
then
echo "This is directory!!!"
elif [ -f $file_name ]
then
echo "This is file!!!"
elif [ -c $file_name -o -b $file_name ]
echo "This is device file!!!"
else
echo "This is nothing!!!"
fi
2、for循環
語法:
for 變量 in 名字表
do
…..
done
例如:
for x in 11 22 33 44 55 66
do
echo "x value is : $x"
done
3、case語句
語法:
case "$variable" in
"$condition1" )
command...
;;
"$condition2" )
command...
;;
esac
對變量使用""并不是強制的, 因為不會發生單詞分割
每句測試行, 都以右小括號)來結尾
每個條件判斷語句塊都以一對分號結尾 ;;
case塊以esac (case的反向拼寫)結尾
例如,有下列程序test.sh內容如下:
read c
case $c in
a)
echo "this is a"
;;
b)
echo "this is b"
;;
c)
echo "this is c"
;;
*)
echo "none"
esac
4、while循環
基本語法:
while [ condition ]
do
command1
command2
command3
done
例如,test.sh中的寫法如下:
num=1
while [ $num -le 10 ]
do
sum=`expr $num \* $num`
echo $sum
num=`expr $num+1`
done
5、break和continue語句
break:結束循環
continue:結束本次循環,開始新的一輪循環
例如:
while true
do
echo "============================"
echo " 1 add "
echo "2 delete "
echo "3 update"
echo "4 Quit"
read num_op
case $num_op in
1)
echo "this is add operation"
;;
2)
echo "this is delete operation"
;;
3)
echo "this is update operation"
;;
4)
echo "exit ….."
break
;;
esac
done
6、shift指令
參數左移:每執行一次,參數序列順次左移一個位置,即$#的值減去1。其用于處理每個參數,移動出去的參數不能再用
例如,test.sh文件內容如下:
If [ $# -le 0 ]
then
echo "not enough parameters"
exit 0
Fi
Sum=0
While [ $# -gt 0 ]
sum=`expr $sum + $1`
shift
Done
Echo $sum
《大數據和人工智能交流》的宗旨
1、將大數據和人工智能的專業數學:概率數理統計、線性代數、決策論、優化論、博弈論等數學模型變得通俗易懂。
2、將大數據和人工智能的專業涉及到的數據結構和算法:分類、聚類 、回歸算法、概率等算法變得通俗易懂。
3、最新的高科技動態:數據采集方面的智能傳感器技術;醫療大數據智能決策分析;物聯網智慧城市等等。
根據初學者需要會有C語言、Java語言、Python語言、Scala函數式等目前主流計算機語言。
根據讀者的需要有和人工智能相關的計算機科學與技術、電子技術、芯片技術等基礎學科通俗易懂的文章。
總結
以上是生活随笔為你收集整理的Linux的shell脚本编程详细总结Linux的shell脚本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 三招破解任何电脑的开机密码如何破解电脑开
- 下一篇: 怎么创建具有真实纹理的CG场景岩石?