linuxshell腳本pdf-ag真人国际官网
編寫shell腳本 首先你要有linux命令的基礎,怎麼進入文件,怎麼執行文件,有什麼命令等等。
我們的shell 類型有很多,常見的shell環境有sh,bash,csh,zsh等等。在linux的腳本中可以最常見的就是 sh或者shell。在shell腳本中最開始 要指定shell環境。於是乎我們有了shell的沙邦:
/bin/sh 或者/bin/bash
shell腳本的格式:shell腳本一般是以*.sh 為名字,在許可權上面是有可執行許可權x的也就是chmod u x *.sh
命令的執行:3種:
sh 腳本路徑/腳本名
cd 腳本路徑 && ./腳本名
soure 腳本路徑/腳本
寫一個最簡單的腳本吧:
[root@linuxprobe ~]#vim 1.sh
/bin/sh
echo "this is my frist scripts,more and more linux ,you can read 《linux就該這樣學》"
[root@linuxprobe ~]#chmod u x 1.sh
[root@linuxprobe ~]#./1.sh
this is my frist scripts,more and more linux ,you can read 《linux就該這樣學》
學習linux需要多學多練
⑵ linux shell腳本,怎樣變數傳遞執行結果
1、shell一般都是放在/bin或者/user/bin目錄下,我們可以使用命令cat /etc/shells命令,查看當前linux系統可用的shell是什麼。
⑶ 誰有《linux shell腳本攻略第2版》全本電子書下載百度網盤資源
鏈接:
《linux shell腳本攻略第2版》作品簡介:
《linux高級程序設計》,《嵌入式linux基礎教程(第2版)》,《linux內核編程》,《unix內核源碼剖析》等。
⑷ linux的shell腳本編程,求兩數字間所有偶數的和,我是小白,不太懂。
這部分主要討論數學相關的shell腳本編程。
加法運算
新建一個文件「addition.sh」,輸入下面的內容並賦予其可執行的許可權。
復制代碼代碼如下:
#!/bin/bash
echo 「enter the first number: 」
read a
echo 「enter the second number: 」
read b
x=$(expr "$a" "$b")
echo $a $b = $x
輸出結果:
復制代碼代碼如下:
[root@tecmint ~]# vi additions.sh
[root@tecmint ~]# chmod 755 additions.sh
[root@tecmint ~]# ./additions.sh
「enter the first number: 」
12
「enter the second number: 」
13
12 13 = 25
減法運算
復制代碼代碼如下:
#!/bin/bash
echo 「enter the first number: 」
read a
echo 「enter the second number: 」
read b
x=$(($a - $b))
echo $a - $b = $x
注意:這里我們沒有像上面的例子中使用「expr」來執行數學運算。
輸出結果:
復制代碼代碼如下:
[root@tecmint ~]# vi substraction.sh
[root@tecmint ~]# chmod 755 substraction.sh
[root@tecmint ~]# ./substraction.sh
「enter the first number: 」
13
「enter the second number: 」
20
13 - 20 = -7
乘法運算
復制代碼代碼如下:
#!/bin/bash
echo 「enter the first number: 」
read a
echo 「enter the second number: 」
read b
echo "$a * $b = $(expr $a \* $b)"
輸出結果:
復制代碼代碼如下:
[root@tecmint ~]# vi multiplication.sh
[root@tecmint ~]# chmod 755 multiplication.sh
[root@tecmint ~]# ./multiplication.sh
「enter the first number: 」
11
「enter the second number: 」
11
11 * 11 = 12
除法運算
復制代碼代碼如下:
#!/bin/bash
echo 「enter the first number: 」
read a
echo 「enter the second number: 」
read b
echo "$a / $b = $(expr $a / $b)"
輸出結果:
復制代碼代碼如下:
[root@tecmint ~]# vi division.sh
[root@tecmint ~]# chmod 755 division.sh
[root@tecmint ~]# ./division.sh
「enter the first number: 」
12
「enter the second number: 」
3
12 / 3 = 4
數組
下面的這個腳本可以列印一組數字。
復制代碼代碼如下:
#!/bin/bash
echo 「enter the number upto which you want to print table: 」
read n
i=1
while [ $i -ne 10 ]
do
i=$(expr $i 1)
table=$(expr $i \* $n)
echo $table
done
輸出結果:
復制代碼代碼如下:
[root@tecmint ~]# vi table.sh
[root@tecmint ~]# chmod 755 table.sh
[root@tecmint ~]# ./table.sh
「enter the number upto which you want to print table: 」
29
58
87
116
145
174
203
232
261
290
你可以從這里下載這個例子的代碼
判斷奇偶數
復制代碼代碼如下:
#!/bin/bash
echo "enter the number"
read n
num=$(expr $n % 2)
if [ $num -eq 0 ]
then
echo "is a even number"
else
echo "is a odd number"
fi
輸出結果:
復制代碼代碼如下:
[root@tecmint ~]# vi evenodd.sh
[root@tecmint ~]# chmod 755 evenodd.sh
[root@tecmint ~]# ./evenodd.sh
enter the number
12
is a even number
1
2
3
4
5
[root@tecmint ~]# ./evenodd.sh
enter the number
11
is a odd number
factorial數
復制代碼代碼如下:
#!/bin/bash
echo "enter the number"
read a
fact=1
while [ $a -ne 0 ]
do
fact=$(expr $fact \* $a)
a=$(expr $a - 1)
done
echo $fact
輸出結果:
復制代碼代碼如下:
[root@tecmint ~]# vi factorial.sh
[root@tecmint ~]# chmod 755 factorial.sh
[root@tecmint ~]# ./factorial.sh
enter the number
12
479001600
你可以從這里下載這個例子的代碼
判斷armstrong數
armstrong數:在三位的正整數中,例如abc,有一些可能滿足(a^3) (b^3) (c^3)=abc,即各個位數的立方和正好是該數的本身。這些數即稱為armstrong數。
復制代碼代碼如下:
#!/bin/bash
echo "enter a number"
read n
arm=0
temp=$n
while [ $n -ne 0 ]
do
r=$(expr $n % 10)
arm=$(expr $arm $r \* $r \* $r)
n=$(expr $n / 10)
done
echo $arm
if [ $arm -eq $temp ]
then
echo "armstrong"
else
echo "not armstrong"
fi
輸出結果:
復制代碼代碼如下:
[root@tecmint ~]# vi armstrong.sh
[root@tecmint ~]# chmod 755 armstrong.sh
[root@tecmint ~]# ./armstrong.sh
enter a number
371
371
armstrong
1
2
3
4
5
6
[root@tecmint ~]# ./armstrong.sh
enter a number
123
36
not armstrong
判斷質數
復制代碼代碼如下:
#!/bin/bash
echo 「enter any number」
read n
i=1
c=1
while [ $i -le $n ]
do
i=$(expr $i 1)
r=$(expr $n % $i)
if [ $r -eq 0 ]
then
c=$(expr $c 1)
fi
done
if [ $c -eq 2 ]
then
echo 「prime」
else
echo 「not prime」
fi
輸出結果:
復制代碼代碼如下:
[root@tecmint ~]# vi prime.sh
[root@tecmint ~]# chmod 755 prime.sh
[root@tecmint ~]# ./prime.sh
「enter any number」
12
「not prime」
⑸ 如何在windows下運行linux shell腳本
1、首先電腦中需要安裝的是git的應用程序,安裝完成之後,可以在開始菜單進行查看。