人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動(dòng)態(tài)

入門shell腳本基礎(chǔ)及原理

發(fā)布日期:2022-01-29 08:36 | 文章來(lái)源:腳本之家

1.特殊變量

$#:查看變量參數(shù)的個(gè)數(shù)
$0:查看腳本的名字
$?。翰榭磗hell后臺(tái)的pid
$@:查看傳遞腳本所有參數(shù)的列表
$*:查看所有參數(shù)的列表,單字符串形式顯示
$$:腳本本身進(jìn)程的ID
$?:上一條命令的結(jié)果,顯示0則成功,不是0則失敗

2.內(nèi)部環(huán)境變量

$PATH 
SHELL  當(dāng)前使用的shell
UID 			當(dāng)前的用戶環(huán)境 {0|其它數(shù)字}={root|其它用戶}
HOME當(dāng)前使用的用戶目錄
PWD  		當(dāng)前的目錄	
HISTFILE 	歷史命令路徑
PS1					#[\u@\h \W]\$ 用戶@主機(jī)名\目錄\$

3.整數(shù)以及字符判斷

3.1整數(shù)判斷

-eq 測(cè)試倆個(gè)整數(shù)是否相等(equal)
-ne 測(cè)試倆個(gè)整數(shù)是否不等		(unequal)
-gt 測(cè)試一個(gè)數(shù)是否大于一個(gè)數(shù) (greater than)
-lt		  測(cè)試一個(gè)數(shù)是否小于一個(gè)數(shù) (less than)
-ge	測(cè)試一個(gè)數(shù)大于或等于 
-le 測(cè)試一個(gè)數(shù)小于或等于

3.2字符測(cè)試

=~測(cè)試是否被正則表達(dá)式匹配
-z "string" 檢測(cè)字符是否為空,空則真,不空則假 如: [ -z "" ]為真空則為真
-n "string" 檢測(cè)字符是否不空,不空則真,不空則假
字符相比較大小用[[  ]],比的是第一個(gè)字母(a-zA-Z)都是大寫或者都是小寫比較ascii值 越大則越大
有大寫又有小寫則A>a B>b但是A不大于b的情況
[root@slave02 ~]# [[ "A" < "B" ]]
[root@slave02 ~]# echo $?
0
[root@slave02 ~]# [[ "a" < "b" ]]
[root@slave02 ~]# echo $?
0

4.文件判斷

-e:文件是否存在
-b:測(cè)試是否塊設(shè)備文件
-c:測(cè)試是否字符設(shè)備文件
-f:測(cè)試是否普通文件
-d:測(cè)試是否目錄
-h:測(cè)試是否符號(hào)鏈接文件
-L:測(cè)試是否是符號(hào)鏈接文件
-p:測(cè)試是否是命名管道文件
-S:測(cè)試是否是套接字文件
權(quán)限相關(guān):
-r 讀
-w 寫
-x 執(zhí)行
特殊權(quán)限
-g
-u
-k
等

5.read輸入

選項(xiàng):
-p:指定提示符
-t:指定提示等待的時(shí)間(秒)

6.if判斷

多分支:
if [ 條件 ];then
	statement1
​	.....
elif  [ 條件2 ];then
​	statement2
​	....
else
​	statement3
​	....
fi

7.案例選擇判斷

case $變量名 in
	'value1') 
		 statement 
		 ... 
 ;;
	'value2') 
 		statement 
 		... 
 ;;
*) 
 		statement 
 		.. 
 ;;  
esac
#case支持的通配符:
 *  //任意長(zhǎng)度任意字符
 ?  //任意單個(gè)字符
 [] //指字范圍內(nèi)的任意單個(gè)字符
start|START  //倆種選擇

8.for循環(huán)

第一種:
for  ((expr1;expr2;expr3))# expr1:初始值條件  
		#expr2:循環(huán)的范圍進(jìn)行退出  
		#expr3:變量的值使用
{
​	循環(huán)體
}
for ((expr1;expr2;expr3));do
​	循環(huán)體
done
第二種:
for  變量  in 列表; do
​	循環(huán)體
done

9.while循環(huán)

while循環(huán)用于不知道循環(huán)次數(shù)的場(chǎng)景,注意有退出條件
while [ 條件 ];do
	statement
	.....
done

10.深入練習(xí)

1.寫一個(gè)腳本,輸入三個(gè)數(shù)字進(jìn)行相應(yīng)的加減乘除

[root@slave02 ~]# cat script01.sh 
#!/bin/bash
a=$1
b=$2
c=$3
num1=$[$a+$b+$c]
num2=$[$a-$b-$c]
num3=$[$a*$b*$c]
echo "$a + $b + $c" = $num1
echo "$a - $b - $c" = $num2
echo "$a * $b * $c" = $num3
awk "BEGIN{printf \"$a/$b/$c=%.2f\n\",$a/$b/$c}"
[root@slave02 ~]# source script01.sh 100 10 9
100 + 10 + 9 = 119
100 - 10 - 9 = 81
100 * 10 * 9 = 9000
100/10/9=1.11

2.猜數(shù)字游戲

規(guī)則:指定一個(gè)數(shù)字,只要猜到了這個(gè)數(shù)字則過(guò)關(guān),否則顯示數(shù)字大了或者數(shù)字小了

[root@master ~]# cat test03.sh 
#!/bin/bash
nums=99
read -p "please enter a number: " num
if [ $num -gt $nums ];then
  echo "數(shù)字大了"
elif [ $num -lt $nums ];then
  echo "數(shù)字小了"
else
  echo "猜對(duì)"
fi  
[root@master ~]# . test03.sh 
please enter a number: 10
數(shù)字小了
[root@master ~]# . test03.sh
please enter a number: 100
數(shù)字大了
[root@master ~]# . test03.sh
please enter a number: 99
猜對(duì)

3.寫一個(gè)腳本,讓nginx服務(wù)設(shè)置開(kāi)機(jī)自啟

#$0是nginx本身 $1是變量對(duì)應(yīng)著下面的start|stop|restart|status
[root@192 init.d]# pwd
/etc/init.d
[root@192 init.d]# cat nginx 
#!/bin/bash
case $1 in
  'start')
 /usr/local/nginx/sbin/nginx
 ;;
  'stop')
 /usr/local/nginx/sbin/nginx -s stop
 ;;
  'restart')
  /usr/local/nginx/sbin/nginx -s stop
  /usr/local/nginx/sbin/nginx
 ;;
  'status')
 num=$(ps -ef |grep -v 'grep'|grep -c nginx:)
 if [ $num -eq 0 ];then
  echo "nginx is stoped" 
 else
  echo "nginx is running"
 fi
 ;;
  *)
  echo "Usage: service $0 start|stop|restart|status"
 ;; 
esac
		#當(dāng)判斷有nginx進(jìn)程數(shù)量則認(rèn)為開(kāi)啟服務(wù),否則認(rèn)為服務(wù)開(kāi)啟失敗

4.利用for循環(huán),創(chuàng)建user序號(hào)1-100的用戶

#創(chuàng)建用戶user1-100
[root@master ~]# cat test05.sh 
#!/bin/bash
for (( i=1;i<=100;i++));do
  useradd user$i
  id user$i &>/dev/null
  if [ $? -eq 0 ];then  #只要判斷用戶成功,$?才會(huì)顯示0,顯示0則代表執(zhí)行下一條命令,否則顯示user以及存在
 echo "success"
  else	
  		echo "user is exis" 
  fi
done

5.利用while循環(huán),計(jì)算1+2…100的值

[root@slave02 ~]# cat which.sh 
#!/bin/bash
s=0  #初始值0
i=1					#判斷的數(shù)值,最終到100停止
while [ $i -le 100 ];do
s=$[$s+$i]			 
i=$[$i+1]#自增加數(shù)
done
echo $s
[root@slave02 ~]# source which.sh 
5050
#隨便輸入一個(gè)數(shù)字進(jìn)行計(jì)算的話,把100改為$1即可

6.apache簡(jiǎn)單的一個(gè)編譯部署腳本

1.一般項(xiàng)目或者腳本,文件,放在相應(yīng)的位置里,方便查找
[root@slave02 tmp]# pwd
/tmp
[root@slave02 tmp]# ls
apache
[root@slave02 apache]# ls
install_apache.sh  soft
[root@slave02 soft]# ls
apr-1.7.0.tar.bz2apr-util-1.6.1.tar.bz2 httpd-2.4.48.tar.bz2  httpd.service
[root@slave02 apache]# cat install_apache.sh #!/bin/bash echo "歡迎使用此腳本" apachedir=/usr/local/apache if [ $UID -ne 0 ];then
  echo "伙計(jì),請(qǐng)使用管理員身份運(yùn)行"
fi
echo "正在安裝依賴包..."
yum -y install epel-release  bzip2 "@Development Tools"  &>/dev/null
yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make  &>/dev/null
id apache &>/dev/null
if [ $? -ne 0 ];then
  useradd -r -M -s /sbin/nologin apache
fi
cd /tmp/apache/soft/
tar -xf apr-1.7.0.tar.bz2
tar -xf apr-util-1.6.1.tar.bz2
tar -xf  httpd-2.4.48.tar.bz2
sed -i '/ $RM "$cfgfile"/d' apr-1.7.0/configure
echo "正在編譯安裝apr,請(qǐng)聽(tīng)聽(tīng)歌放松放松......." 
cd apr-1.7.0/
[ ! -d /usr/local/apr ]
if [ $? -eq 0 ];then
  ./configure --prefix=/usr/local/apr && make && make install &>/dev/null
else
  echo "apr已經(jīng)安裝"
fi
cd ../apr-util-1.6.1/
[ ! -d /usr/local/apr-util ]
if [ $? -eq 0 ];then
  ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install &/dev/null
else
  echo "apr-util已經(jīng)安裝"
fi
cd ../httpd-2.4.48/
[ ! -d /usr/local/apache/ ]
if [ $? -eq 0 ];then
./configure --prefix=$apachedir \
  --sysconfdir=/etc/httpd24 \
  --enable-so \
  --enable-ssl \
  --enable-cgi \
  --enable-rewrite \
  --with-zlib \
  --with-pcre \
  --with-apr=/usr/local/apr \
  --with-apr-util=/usr/local/apr-util/ \
  --enable-modules=most \
  --enable-mpms-shared=all \
  --with-mpm=prefork
  make && make install &>/dev/null
else
  echo "httpd已經(jīng)安裝"
fi
cd
#有影響的加判斷,沒(méi)影響的忽略
echo "export PATH=$apachedir/bin:\$PATH"> /etc/profile.d/httpd.sh
ln -s $apachedir/include/ /usr/include/apache &>/dev/null
grep 'apache/man' /etc/man_db.conf&>/dev/null
if [ $? -eq 1 ];then
  sed -i "20aMANDATORY_MANPATH$apachedir/man" /etc/man_db.conf
else
  echo "apache is help exists"
fi
[ ! -f /usr/lib/systemd/system/httpd.service ]
if  [ $? -eq 0 ];then
  cp /clq/apache/soft/httpd.service /usr/lib/systemd/system/
else
  echo "已經(jīng)存在文件跳過(guò)"
fi
systemctl daemon-reload
systemctl enable --now httpd
num02=$(ps -ef |grep -v 'grep'|grep -c httpd)
if [ $num02 -eq 0 ];then
  echo "httpd自啟失敗"
else
  echo "httpd自啟成功"
fi
echo "歡迎下次使用"  
[root@slave02 apache]# chmod +x install_apache.sh 
[root@slave02 apache]# source install_apache.sh 
[root@slave02 apache]# source install_apache.sh 
歡迎使用此腳本
正在安裝依賴包...
正在編譯安裝apr,請(qǐng)聽(tīng)聽(tīng)歌放松放松.......
apr以及安裝
apr-util以及安裝
httpd已經(jīng)安裝
apache is help exists
已經(jīng)存在文件跳過(guò)
httpd自啟成功
歡迎下次使用
[root@slave02 ~]# systemctl status httpd.service 
● httpd.service - Start http
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2021-09-04 17:45:33 CST; 5h 57min ago
 Main PID: 834761 (httpd)
 Tasks: 7 (limit: 5782)
Memory: 6.3M
CGroup: /system.slice/httpd.service
  ├─834761 /usr/local/apache/bin/httpd -k start
  ├─835358 /usr/local/apache/bin/httpd -k start
  ├─835359 /usr/local/apache/bin/httpd -k start
  ├─835360 /usr/local/apache/bin/httpd -k start
  ├─835361 /usr/local/apache/bin/httpd -k start
  ├─835362 /usr/local/apache/bin/httpd -k start
  └─836063 /usr/local/apache/bin/httpd -k start
[root@slave02 ~]# ss -antl
StateRecv-Q  Send-QLocal Address:PortPeer Address:Port  Process  
LISTEN  0 1280.0.0.0:22 0.0.0.0:*  
LISTEN  0 128  *:80*:*  
LISTEN  0 128[::]:22 [::]:*

以上就是入門shell腳本基礎(chǔ)解析的詳細(xì)內(nèi)容,更多關(guān)于shell腳本的資料請(qǐng)關(guān)注本站其它相關(guān)文章!

香港服務(wù)器租用

版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開(kāi)通

自選配置、實(shí)時(shí)開(kāi)通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問(wèn)服務(wù)

1對(duì)1客戶咨詢顧問(wèn)

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部