網(wǎng)站首頁 編程語言 正文
一、if 判斷
1)基本語法
(1)單分支
if [ 條件判斷式 ];then
程序
fi
或者
if [ 條件判斷式 ]
then
程序
fi
?大于18,命令行輸出OK
[root@hadoop ~]# a=25
[root@hadoop ~]# if [ $a -gt 18 ];then echo OK; fi
OK
[root@hadoop ~]# a=15
[root@hadoop ~]# if [ $a -gt 18 ]; then echo OK; fi
或者
[root@hadoop scripts]# a=25
[root@hadoop scripts]# if [ $a -gt 18 ] && [ $a -lt 35 ];then echo OK;fi
OK
?判斷是否張三,是輸出張三
[root@hadoop scripts]# vim if_test.sh
#!/bin/bash
if [ "$1"x = "zhangsan"x ] #等號(hào)左右加x,避免輸入空值報(bào)錯(cuò)
then
echo "zhangsan"
fi
[root@hadoop scripts]# . if_test.sh zhangsan
zhangsan
[root@hadoop scripts]# . if_test.sh
[root@hadoop scripts]# . if_test.sh lisi
邏輯與-a,邏輯或-o
[root@hadoop scripts]# a=36
[root@hadoop scripts]# if [ $a -gt 18 ] && [ $a -lt 35 ]; then echo OK; fi
[root@hadoop scripts]# if [ $a -gt 18 && $a -lt 35 ]; then echo OK; fi
-bash: [: missing `]' #不能直接用
[root@hadoop scripts]# if [ $a -gt 18 -a $a -lt 35 ]; then echo OK; fi
[root@hadoop scripts]# a=20
[root@hadoop scripts]# if [ $a -gt 18 -a $a -lt 35 ]; then echo OK; fi
OK
(2)多分支
if [ 條件判斷式 ]
then
程序
elif [ 條件判斷式 ]
then
程序
else
程序
fi
?判斷屬于哪個(gè)年齡段,雙分支
[root@hadoop scripts]# vim if_test.sh
#!/bin/bash
if [ "$1"x = "zhangsan"x ]
then
echo "zhangsan"
fi
#輸入第二個(gè)參數(shù),表示年齡,判斷屬于哪個(gè)年齡段
if [ $2 -lt 18 ]
then
echo "未成年人"
else
echo "成年人"
fi
[root@hadoop scripts]# . if_test.sh zhangsan 15
zhangsan
未成年人
[root@hadoop scripts]# . if_test.sh zhangsan 25
zhangsan
成年人
判斷屬于哪個(gè)年齡段,多分支
注意事項(xiàng):
①[ 條件判斷式 ],中括號(hào)和條件判斷式之間必須有空格
②if 后要有空格?
二、case 語句
1)基本語法
case $變量名 in
"值 1")
如果變量的值等于值 1,則執(zhí)行程序 1
;;
"值 2")
如果變量的值等于值 2,則執(zhí)行程序 2
;;
…省略其他分支…
*) 如果變量的值都不是以上的值,則執(zhí)行此程序
;;
esac
輸入數(shù)字,輸出對(duì)應(yīng)語句
[root@hadoop ~]# vim case_test.sh
#!/bin/bash
case $1 in
1)
?? ?echo "one"
;;
2)
?? ?echo "two"
;;
3)
?? ?echo "three"
;;
*)
?? ?echo "number else"
;;
esac
[root@hadoop ~]# chmod +x case_test.sh
[root@hadoop ~]# . case_test.sh
number else
[root@hadoop ~]# . case_test.sh 2
two
[root@hadoop ~]# . case_test.sh 1
one
[root@hadoop ~]# . case_test.sh 6
number else
注意事項(xiàng):
(1)case 行尾必須為單詞“in”,每一個(gè)模式匹配必須以右括號(hào)“)”結(jié)束。
(2)雙分號(hào)“;;”表示命令序列結(jié)束,相當(dāng)于 java 中的 break。
(3)最后的“*)”表示默認(rèn)模式,相當(dāng)于 java 中的 default。?
三、for 循環(huán)
1)基本語法1
for (( 初始值;循環(huán)控制條件;變量變化 ))
do
? ? ? ? 程序
done
?2)案例實(shí)操
從 1 加到 100,注意在for循環(huán)里面可以直接用數(shù)學(xué)運(yùn)算符
[root@hadoop ~]# vim sum_to.sh
[root@hadoop ~]# cat sum_to.sh
#!/bin/bash
sum=0
for (( i=1; i <= $1; i++ ))
do
sum=$[ $sum + $i ]
done
echo $sum
[root@hadoop ~]# chmod +x sum_to.sh
[root@hadoop ~]# . sum_to.sh 100
5050
?3)基本語法 2
for 變量 in 值 1 值 2 值 3…
do
? ? ? ? 程序
done
[root@hadoop ~]# for i in {1..100}; do sum=$[$sum+$i]; done; echo $sum
5050
(4)比較$*和$@區(qū)別
$*和$@都表示傳遞給函數(shù)或腳本的所有參數(shù),不被雙引號(hào)“”包含時(shí),都以$1 $2 …$n 的形式輸出所有參數(shù)。?
沒加雙引號(hào),兩種輸出結(jié)果沒有區(qū)別
[root@hadoop scripts]# vim parameter_for_test.sh
#!/bin/bash
echo '============$*================'
for para in $*
do
echo $para
done
echo '============$@================'
for para in $@
do
echo $para
done
[root@hadoop scripts]# chmod +x parameter_for_test.sh
[root@hadoop scripts]# . parameter_for_test.sh a b c d e
============$*================
a
b
c
d
e
============$@================
a
b
c
d
e
加雙引號(hào),兩種輸出結(jié)區(qū)別,一種有換行,一種沒有換行。
當(dāng)它們被雙引號(hào)“”包含時(shí),$*會(huì)將所有的參數(shù)作為一個(gè)整體,以“$1 $2 …$n”的形式輸 出所有參數(shù);$@會(huì)將各個(gè)參數(shù)分開,以“$1” “$2”…“$n”的形式輸出所有參數(shù)。
[root@hadoop scripts]# vim parameter_for_test.sh
#!/bin/bash
echo '============$*================'
for para in "$*"
do
echo $para
done
echo '============$@================'
for para in "$@"
do
echo $para
done
[root@hadoop scripts]# . parameter_for_test.sh a b c d e
============$*================
a b c d e
============$@================
a
b
c
d
e
四、while循環(huán)
1)基本語法
while [ 條件判斷式 ]
do
? ? ? ? 程序
done
2)案例實(shí)操
從1加到100
[root@hadoop scripts]# vim sum_to.sh
#!/bin/bash
#用for進(jìn)行實(shí)現(xiàn)
sum=0
for (( i=1; i <= $1; i++ ))
do
sum=$[ $sum + $i ]
done
echo $sum
#用while做一個(gè)實(shí)現(xiàn)
a=1
while [ $a -le $1 ]
do
sum2=$[ $sum2 + $a ]
a=$[ $a + 1 ]
done
echo $sum2
[root@hadoop scripts]# . sum_to.sh 100
5050
5050
更簡(jiǎn)單語法用shell內(nèi)嵌命令let
[root@hadoop scripts]# vim sum_to.sh
#用while做一個(gè)實(shí)現(xiàn)
sum2=0
a=1
while [ $a -le $1 ]
do
# sum2=$[ $sum2 + $a ]
# a=$[ $a + 1 ]
let sum2+=a
let a++
done
echo $sum2
[root@hadoop scripts]# . sum_to.sh 100
5050
5050
原文鏈接:https://blog.csdn.net/m0_55834564/article/details/126435699
相關(guān)推薦
- 2022-06-09 Nginx流量拷貝ngx_http_mirror_module模塊使用方法詳解_nginx
- 2022-11-10 Android中PopupWindow彈出式窗口使用方法詳解_Android
- 2022-05-22 關(guān)于VS2022不能使用<bits/stdc++.h>的解決方案(萬能頭文件)_C 語言
- 2023-03-25 React錯(cuò)誤邊界Error?Boundaries_React
- 2022-10-10 C++?STL標(biāo)準(zhǔn)庫std::vector擴(kuò)容時(shí)進(jìn)行深復(fù)制原因詳解_C 語言
- 2022-04-17 WPF框架Prism中導(dǎo)航Navigation用法介紹_基礎(chǔ)應(yīng)用
- 2023-12-08 IDEA中, Maven不顯示插件 mybatis-generator
- 2022-08-01 Python+pyecharts繪制雙動(dòng)態(tài)曲線教程詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支