1.shell初步了解
1.1 什么是Shell
Shell是一個(gè)命令解釋器蜗侈,它的作用是解釋執(zhí)行用戶輸入的命令及程序等虏缸,用戶輸入一條命令挚赊,Shell就解釋執(zhí)行一條。
這種從鍵盤一輸入命令雹食,就可以立即得到回應(yīng)的 對(duì)話方式郑原,被稱之為交互的方式。
Shell存在于操作系統(tǒng)的最外層陶珠,負(fù)責(zé)直接與用戶對(duì)話, 把用戶的輸入解釋給操作系統(tǒng)享钞,并處理各種各樣的操作系統(tǒng)的輸出結(jié)果揍诽,輸出到屏幕返回給用戶,當(dāng)我們輸入 系統(tǒng)用戶名和密碼,登錄到Linux后的所有操作都是由 Shell解釋并執(zhí)行的寝姿。
1.2.什么是Shell腳本
當(dāng)命令或程 序語句不在命令行下執(zhí)行交排,而是通過一個(gè)程序文件執(zhí)行 時(shí),該程序就被稱為Shell腳本饵筑。
如果在Shell腳本里內(nèi)置了很多條命令、語句及循環(huán)控制处坪,然后一次性把這些命令執(zhí)行完根资,這種通過文件執(zhí)行腳本的方式,稱為非交互的方式同窘。
Shell腳本類似于DOS系統(tǒng)下的批處理程序玄帕。
用戶可以在Shell腳本中敲入一系列的命令及命令語句組合。
這些命令想邦、變量和流程控制語句等有機(jī)地結(jié)合起來就形成了一個(gè)功能強(qiáng)大的Shell腳本
1.3. 腳本語言的種類
1. Bourne shell
Bourne shell又包括Bourne shell (sh)裤纹、Korn shell (ksh)、Bourne Again Shell(bash)三種類型丧没,重點(diǎn)是Bourne Again Shell (bash) 鹰椒。
2. C shell
C shell又包括csh、tcsh兩種類型呕童。csh由Berkeley大學(xué)開發(fā)漆际,隨BSDUNIX發(fā)布,它的流程控制語句很像C語言夺饲,支持很多Bourne Shell所不支持的功能奸汇,例如:作業(yè)控制、別名往声、系統(tǒng)算術(shù)擂找、命令歷史、命令行編輯等浩销。tcsh是csh的增 強(qiáng)版贯涎,加入了命令補(bǔ)全等功能,在FreeBSD撼嗓、MacOSX等系統(tǒng)上替代了csh
Bourne Again Shell (bash)
Bourne Again Shell即bash由GNU項(xiàng)目組開發(fā)柬采,主要目標(biāo)是與POSIX標(biāo)準(zhǔn)保持一致,同時(shí)兼顧對(duì)sh的兼容且警,bash從csh 和ksh借鑒了很多功能粉捻,是各種Linux發(fā)行版默認(rèn)配置的Shell,在Linux系統(tǒng)上的/bin/sh往往是指向/bin/bash的 符號(hào)鏈接斑芜。
雖然如此肩刃,bash和sh還是有很多的不同,一方面,bash擴(kuò)展了一些命令和參數(shù)盈包,另一方面沸呐,bash并不完 全和sh兼容,它們有些行為并不一致呢燥,但大多數(shù)企業(yè)運(yùn)維 的情況下區(qū)別不大崭添,特殊場(chǎng)景可以使用bash替代sh。
因?yàn)楫?dāng)時(shí)有很多廠商基于unix開發(fā)很多版本的系統(tǒng) 所有就產(chǎn)生了很多的不同的shell解釋器
Shell腳本與php/perl/python語言的區(qū)別和優(yōu)勢(shì)叛氨?
shell腳本的優(yōu)勢(shì)在于處理操作系統(tǒng)底層的業(yè)務(wù) (linux系統(tǒng)內(nèi)部的應(yīng)用都是shell腳本完成)因?yàn)橛写罅康膌inux系統(tǒng)命令為它做支撐呼渣。
2000多個(gè)命令都是shell腳本編程的有力支撐,特別是grep寞埠、awk屁置、sed等。例如:一鍵軟件安裝仁连、優(yōu)化蓝角、監(jiān)控報(bào)警腳本,常規(guī)的業(yè)務(wù)應(yīng)用饭冬,shell開發(fā)更簡(jiǎn)單快速使鹅,符合運(yùn)維的簡(jiǎn)單、易用伍伤、高效原則.
PHP并徘、Python優(yōu)勢(shì)在于開發(fā)運(yùn)維工具以及web界面的管理工具,web業(yè)務(wù)的開發(fā)等扰魂。處理一鍵軟件安裝麦乞、優(yōu)化,報(bào)警腳本劝评。常規(guī)業(yè)務(wù)的應(yīng)用等php/python也是能夠做到的姐直。但是開發(fā)效率和復(fù)雜比用shell就差很多了。
PHP語言
PHP是網(wǎng)頁程序語言蒋畜,也是腳本語言声畏。是一款更注 于Web頁面開發(fā)(前端展示)的語言,例如: wordpress姻成、dedecms插龄、discuz等著名開源產(chǎn)品都是 用PHP語言開發(fā)的
Perl語言
Perl腳本語言,語法靈活科展、復(fù)雜均牢,缺點(diǎn)是不易讀, 團(tuán)隊(duì)協(xié)作困難才睹,存世的大量相關(guān)程序軟件(比如徘跪, xtrabackup熱備工具甘邀、MySQL MHA集群高可用軟件等 都有Perl語言的身影)。
Python語言
Python是當(dāng)下流行的語言垮庐,不但可以用于腳本程序開發(fā)松邪,也可實(shí)現(xiàn)Web程序開發(fā)(例如:CMDB管理系統(tǒng)),還可以做軟件開發(fā)( OpenStack)哨查、游戲開 發(fā)逗抑、大數(shù)據(jù)開發(fā)、移動(dòng)端開發(fā)寒亥。
1.4.為什么學(xué)習(xí)shell
Linux底層核心
Linux運(yùn)維工作常用的工具
自動(dòng)化運(yùn)維必備基礎(chǔ)工具
提升工作效率
減少重復(fù)工作
shell不可替代
1.5 如何學(xué)好Shell編程
變量基礎(chǔ)锋八、條件表達(dá)式、if判斷护盈,for循環(huán),while循環(huán)羞酗、case語句
循環(huán)控制命令 continue,break,exit 要能看懂別人的代碼腐宋,掌握常見的各種語法,經(jīng)常練習(xí)檀轨、經(jīng)常寫
從簡(jiǎn)單的判斷循環(huán)開始做起
閱讀胸竞、模仿,閱讀参萄,模仿卫枝、仔細(xì)分析、總結(jié)讹挎、文檔
要有編程思維
找一本合適的教材校赤、或者自己認(rèn)真做的較為全面的筆記
核心
多練習(xí)-多思考-再練-在思考,堅(jiān)持如此循環(huán)即可
大忌:不可拿來主義筒溃,可以模仿马篮,但是要自己嚼爛了在吃下
學(xué)完shell 可解決企業(yè)中大部分腳本問題
2. shell腳本的規(guī)范
1、腳本存放固定目錄 /server/scripts
2怜奖、開頭加腳本解釋器
3浑测、附帶作者及版權(quán)信息
4、腳本擴(kuò)展名為*.sh
5歪玲、腳本中不用中文
6迁央、成對(duì)的符號(hào)一次書寫完成
7、循環(huán)格式一次性輸入完成
8滥崩、循環(huán)格式要縮進(jìn)
2.1 shell腳本的執(zhí)行
1. bash script-name或sh script-name
這是當(dāng)腳本文件本身沒有可執(zhí)行權(quán)限(即文件權(quán)限屬性x 位為-號(hào))時(shí)常使用的方法岖圈,或者腳本文件開頭沒有指定解釋器時(shí)需要使用的方法
2. path/script-name或./script-name
指在當(dāng)前路徑下執(zhí)行腳本(腳本要有執(zhí)行權(quán)限),需要先將腳本文件的權(quán)限改為可執(zhí)行(即文件權(quán)限屬性加x 位)夭委,具體方法為chmod +x script-name幅狮。然后通過腳本絕對(duì)路徑或相對(duì)路徑就可以直接執(zhí)行腳本了募强。
3. source script-name或. script-name
4. sh<script-name或cat scripts-name|sh
講解執(zhí)行方法區(qū)別
1 2 4 執(zhí)行腳本時(shí)在命令行無法取到腳本中的內(nèi)容
用3 執(zhí)行時(shí)就可以取到腳本中的內(nèi)容
[root@web01 scripts]# cat ceshi.sh
#!/bin/bash
user=`whoami`
[root@web01 scripts]# sh ceshi.sh
[root@web01 scripts]# echo $user
[root@web01 scripts]# source ceshi.sh
[root@web01 scripts]# echo $user
root
2.2 shell腳本執(zhí)行過程
A腳本中放B腳本 那A腳本里的那個(gè)B腳本就是子shell腳本 A就是父shell腳本
還可以把命令行當(dāng)成父shell,在命令行執(zhí)行的腳本是子shell腳本,所以source ceshi.sh就是把子腳本的定義放到命令行中操作
3.shell變量
3.1 什么是變量崇摄?
在小學(xué)或初中時(shí)擎值,我們開始接觸數(shù)學(xué)方程式,例如:已知x=1逐抑,y=x+1鸠儿,那么y等于多少? 在上述問題中厕氨,等號(hào)左邊的x和y當(dāng)時(shí)被稱做未知數(shù)进每,但在Shell編程里就叫做變量名,等號(hào)右邊的1和x+1則是變 量的內(nèi)容(變量的值)命斧。
注意田晚,這里的等號(hào)符號(hào)被稱為賦值,而不是等號(hào)国葬。
3.2 變量的分類
變量可分為兩類:環(huán)境變量(全局變量)和普通變量(局部變量)贤徒。
環(huán)境變量也可稱為全局變量,可以在創(chuàng)建他們的Shell及其派生出來的任意子進(jìn)程Shell中使用汇四,環(huán)境變量又可分為自定義環(huán)境變量和bash內(nèi)置的環(huán)境變量接奈。
普通變量也可稱為局部變量,只能在創(chuàng)建他們 的Shell函數(shù)或Shell腳本中使用通孽。
普通變量一般由開發(fā)者在開發(fā)腳本程序時(shí)創(chuàng)建序宦。
全局變量:環(huán)境變量 在父腳本中定義在子腳本中就可以使用
普通變量:局部變量 在命令行定義就在命令行使用 在腳本中定義就在腳本中使用
3.3 Shell環(huán)境變量介紹
環(huán)境變量一般是用export內(nèi)置命令導(dǎo)出的變量,用于定義Shell的運(yùn)行環(huán)境背苦,保證Shell命令的正確執(zhí)行互捌。Shell通過環(huán)境變量來確定登錄用戶名、命令路徑糠惫、終端類型疫剃、登錄目錄等,所有的環(huán)境變量都是系統(tǒng)全局變量硼讽,可用于所有子進(jìn)程中巢价,這包括編輯器、Shell腳本和各類應(yīng)用
3.3.1 操作Shell環(huán)境變量
1.顯示環(huán)境變量
2.設(shè)置環(huán)境變量
3.取消環(huán)境變量
顯示環(huán)境變量
顯示:echo env
[root@bogon scripts]# echo $HOME
/root
[root@bogon scripts]# echo $SHELL
/bin/bash
[root@bogon scripts]# echo $UID
0
[root@bogon scripts]# cd -
/root
[root@bogon ~]# env|grep -i oldpwd
OLDPWD=/server/scripts
[root@bogon ~]# cd -
/server/scripts
[root@bogon scripts]# cd $OLDPWD
[root@bogon ~]# cd $OLDPWD
定義:export
export BOY=1
GIRL=2
export GIRL
declare -x 作用相當(dāng)于export
[root@bogon scripts]# export BOY=1
[root@bogon scripts]# echo BOY
BOY
[root@bogon scripts]# echo $BOY
1
[root@bogon scripts]# GIRL=2
[root@bogon scripts]# export OLDGIRL
[root@bogon scripts]# echo $GIRL
2
刪除:unset 取消變量
[root@bogon scripts]# export BOY=1
[root@bogon scripts]# echo $BOY
1
[root@bogon scripts]# unset BOY
[root@bogon scripts]# echo $BOY
3.3.2 shell環(huán)境變量如何永久生效
全局生效:
/etc/profile
/etc/bashrc
當(dāng)前用戶生效:
~/.bashrc
~/.bash_profile
Su – 切換用戶的話會(huì)帶著環(huán)境變量
[root@bogon scripts]# useradd haoge
[root@bogon scripts]# su – haoge
3.3.3 環(huán)境變量初始化與對(duì)應(yīng)文件生效順序
㈠ 登入shell(直接從終端登錄)
/etc/profile 是登入shell啟動(dòng)后運(yùn)行的第一個(gè)啟動(dòng)腳本
它只為登入shell運(yùn)行固阁;非登入shell不會(huì)引發(fā)這個(gè)腳本
登入shell加載啟動(dòng)腳本的順序?yàn)椋? /etc/profile → /etc/profile.d → ~/.bash_profile → ~/.bashrc → /etc/bashrc
每個(gè)調(diào)用的腳本會(huì)依次撤銷前一個(gè)調(diào)用腳本中的改變壤躲,會(huì)覆蓋前一個(gè)定義的變量
在退出登入shell時(shí),我們還想執(zhí)行某些任務(wù)备燃,如清理歷史命令碉克、清除臨時(shí)文件把這些任務(wù)放在.bash_logout文件
㈡ 非登入shell(用su切換過去)
非登入shell加載啟動(dòng)腳本的順序:
.bashrc → /etc/bashrc → /etc/profile.d
3.4 shell普通變量介紹
普通變量也稱局部變量,局部變量在用戶當(dāng)前Shell生存期的腳本中使用并齐。例如漏麦,本地變量haoge的取值為bomc客税,這個(gè)值只在用戶當(dāng)前Shell生存期中有意義。如果在Shell中啟動(dòng)另一個(gè)進(jìn)程或退出撕贞,變量hoage的值將無效更耻。
3.4.1 操作shell普通變量
普通變量規(guī)范:
變量名=值
變量名:字母、下劃線捏膨、數(shù)字秧均,并且不能數(shù)字開頭。
變量名不要用沒有意義的字符串:
haoge_age 下劃線連接單詞
haogeAge 駝峰語法
HaogeAge 每個(gè)單詞首字母都大寫
=號(hào):賦值符号涯,兩端沒有空格目胡,等號(hào)常用==
值:
不加引號(hào) haoge_age=35 一般純數(shù)字,簡(jiǎn)單連續(xù)的字符串也可以链快,但不推薦誉己。
加單引號(hào) haoge_age='35' 所見即所得,眼睛看到什么域蜗,輸出或者使用就是什么巫延。
加雙引號(hào) haoge_age="35" 除了純數(shù)字以外,無特殊需求默認(rèn)雙引號(hào)地消,解析變量等。
加反引號(hào) haoge_age=`date +%Y` 還可以用$()畏妖,將命令解析出結(jié)果
使用:
$變量名 通用
${變量名} 保護(hù)變量
舉例:
db=1
echo ${db}_name
1_name
echo $db_name
空行
3.5 特殊變量
3.5.1 特殊位置變量
$0 獲取當(dāng)前執(zhí)行shell腳本的文件名脉执、如果執(zhí)行腳本帶路徑,就包括腳本路徑
$n 獲取當(dāng)前執(zhí)行shell腳本的第n個(gè)參數(shù)值戒劫,當(dāng)n為0時(shí)表示腳本的文件名半夷,如果n大于9 則用大括號(hào)括起來 例如:{10} 接的參數(shù)以空格隔開
$# 獲取當(dāng)前執(zhí)行Shell腳本后面接的參數(shù)的總個(gè)數(shù)
$* 獲取當(dāng)前shell腳本所有傳參的參數(shù),如果不加引號(hào)作用同$@相同迅细,如果加上雙引號(hào)"$*" 則表示將所有的參數(shù)視為一個(gè)字符串 相當(dāng)于"$1$2$3"
$@ 獲取當(dāng)前shell腳本所有傳參的參數(shù)巫橄,不加引號(hào)同$* 相同,加上雙引號(hào)則表示把所有的參數(shù)視為不同的獨(dú)立的字符串 相當(dāng)于“$1” “$2” “$3”
$0
[root@shell scripts]# cat lianxi.sh
#!/bin/bash
echo $0
[root@shell scripts]# sh lianxi.sh
lianxi.sh
[root@shell scripts]# sh /server/scripts/lianxi.sh
/server/scripts/lianxi.sh
類似命令:
[root@shell scripts]# dirname /server/scripts/lianxi.sh
/server/scripts
[root@shell scripts]# basename /server/scripts/lianxi.sh
lianxi.sh
$n(n=任意數(shù)字)
[root@shell scripts]# tail -2 test2.sh
echo $0
echo $1
[root@shell scripts]# sh /server/scripts/test2.sh Haoge
/server/scripts/test2.sh
Haoge
[root@shell scripts]# tail -3 test2.sh
echo $0
echo $1
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15}
[root@shell scripts]# sh /server/scripts/test2.sh {a..z}
/server/scripts/test2.sh
a
a b c d e f g h i j k l m n o
$#
[root@shell scripts]# cat test2.sh
echo $#
[root@shell scripts]# sh test2.sh abc cba
2
應(yīng)用:
if [ $# -ne 2 ];then
echo "Usage: $0 需要傳遞兩個(gè)參數(shù)"
exit 1
fi
$ *
$@
[root@shell scripts]# set -- "I am" hao gege.
[root@shell scripts]# echo $1
I am
[root@shell scripts]# echo $2
hao
[root@shell scripts]# echo $3
gege.
[root@shell scripts]# echo $#
3
[root@shell scripts]# echo $*
I am hao gege.
[root@shell scripts]# echo $@
I am hao gege.
[root@shell scripts]# for n in "$*";do echo $n;done
I am hao gege.
[root@shell scripts]# for n in "$@";do echo $n;done
I am
hao
gege.
3.5.2 特殊進(jìn)程狀態(tài)變量
$? 獲取執(zhí)行上一個(gè)指令執(zhí)行狀態(tài)返回值 0為成功 非零失敗
$$ 獲取當(dāng)前shell腳本的進(jìn)程號(hào)PID
$! 獲取上一個(gè)在后臺(tái)工作腳本進(jìn)程的進(jìn)程號(hào)PID
$_ 獲取在此之前執(zhí)行命令或腳本的最后一個(gè)參數(shù)茵典,相當(dāng)于ESC .
$?
企業(yè)場(chǎng)景:
執(zhí)行啟動(dòng)服務(wù)的程序
判斷$?是否為0 湘换,為0就給出啟動(dòng)成功提示,非0可以給出啟動(dòng)失敗的提示
$$
[root@shell scripts]# cat ceshi.sh
echo $$ > /tmp/ceshi.pid
sleep 10
企業(yè)應(yīng)用:
獲取進(jìn)程號(hào)從而管理進(jìn)程统阿。
kill `cat /tmp/oldboy.pid`
$_
[root@shell scripts]# sh ceshi.sh fff ddd
[root@shell scripts]# echo $_
ddd
$!
[root@shell scripts]# sh ceshi.sh &
[1] 1338
[root@shell scripts]# echo $!
1338
[root@shell scripts]# cat /tmp/ceshi.pid
1338
3.6 變量子串
${#parameter } 返回變量長度
[root@shell scripts]# haoge="I am a Linux enthusiast"
[root@shell scripts]# echo ${haoge}
I am a Linux enthusiast
[root@shell scripts]# echo ${#haoge}
23
其他方式:獲取字符串或變量?jī)?nèi)容長度的四種方法
[root@shell scripts]# echo ${#haoge}
23
[root@shell scripts]# echo ${haoge}|wc -L
23
[root@shell scripts]# expr length "$haoge"
23
[root@shell scripts]# echo ${haoge}|awk '{print length}'
23
${#parameter:offset}從parameter的offset位置到結(jié)尾
[root@shell scripts]# echo ${haoge:2}
am a Linux enthusiast
[root@shell scripts]# echo ${haoge:4}
a Linux enthusiast
${#parameter:offset:length}從parameter的offset位置開始截取長度為length的子串
[root@shell scripts]# echo ${haoge:2:4}
am a
[root@shell scripts]# echo ${haoge:2:2}
am
[root@shell scripts]# echo ${haoge:2:1}
a
${parameter#world}刪除變量中的字符串:#開頭彩倚; %結(jié)尾; ## %%最長匹配扶平; # % 最短匹配
[root@shell scripts]# echo $haoge
abcABC123ABCabc
[root@shell scripts]# echo ${haoge#a*c}
ABC123ABCabc
[root@shell scripts]# echo ${haoge##a*c}
[root@shell scripts]# echo ${haoge%a*c}
abcABC123ABC
[root@shell scripts]# echo ${haoge%%a*c}
${parameter/pattern/string}替換變量中的字符:/ 替換匹配到的第一個(gè)pattern
[root@shell scripts]# echo ${haoge}
abcABC123ABCabc
[root@shell scripts]# echo ${haoge/abc/haoge}
haogeABC123ABCabc
${parameter//pattern/string}替換變量中的字符:/ / 替換匹配所有的pattern
[root@shell scripts]# echo ${haoge}
abcABC123ABCabc
[root@shell scripts]# echo ${haoge//abc/haoge}
haogeABC123ABChaoge
3.7 特殊變量擴(kuò)展
${parameter:-word} 冒號(hào)可以省略
[root@shell scripts]# result=${test:-haoge}
[root@shell scripts]# echo $result
haoge
[root@shell scripts]# echo $test
如果test有賦值
[root@shell scripts]# test=yuyu
[root@shell scripts]# result=${test:-haoge}
[root@shell scripts]# echo $result
yuyu
[root@shell scripts]# echo $test
yuyu
${parameter:=word}
用法比${parameter:-word}用法多一個(gè)給變量?jī)?nèi)容里的變量賦值帆离。
[root@shell scripts]# unset test
[root@shell scripts]# result=${test:=haoge}
[root@shell scripts]# echo $result
haoge
[root@shell scripts]# echo $test
haoge
${parameter:?word}用法。當(dāng)未賦值時(shí)word當(dāng)作標(biāo)準(zhǔn)錯(cuò)誤輸出
[root@shell scripts]# unset test
[root@shell scripts]# result=${test:?變量為負(fù)值}
-bash: test: 變量為負(fù)值
${parameter:+word}用法结澄。
和${parameter:-word}用法相反
[root@shell scripts]# unset test
[root@shell scripts]# result=${test:+haoge}
[root@shell scripts]# echo $result
[root@shell scripts]# test=1
[root@shell scripts]# result=${test:+haoge}
[root@shell scripts]# echo $result
haoge
[root@shell scripts]# echo $test
1
4. 變量的數(shù)值運(yùn)算
4.1 整數(shù)運(yùn)算符
(())
[root@shell ~]# a=11
[root@shell ~]# echo $((a+1))
12
[root@shell ~]# echo $(($a+1 ))
12
let
[root@shell ~]# i=1
[root@shell ~]# i=i+8
[root@shell ~]# echo $i
i+8
[root@shell ~]# i=1
[root@shell ~]# let i=i+8
[root@shell ~]# echo $i
9
案例:
sum=0
while read line
do
size=`echo $line|awk '{print $10}'|grep -v -`
let sum+=size
done<$1
echo $sum
let sum+=size 就是把sum與size兩變量的值相加賦予新的sum
[root@shell scripts]# sum=0
[root@shell scripts]# a=10
[root@shell scripts]# let sum+=a
[root@shell scripts]# echo $sum
10
expr
數(shù)字跟加號(hào)中間要有空格
[root@oldboy ~]# expr 1 + 1
2
[root@oldboy ~]# expr 1 + 3
4
[root@oldboy ~]# expr 1+3
1+3
[root@oldboy ~]# expr 1 + 3
4
[root@oldboy ~]# expr 1 + 3
4
$[]
[root@shell scripts]# echo $[1+1]
2
[root@shell scripts]# echo $[1 + 1 ]
2
[root@shell scripts]# echo $[1-1]
0
declare
[root@shell scripts]# a=10
[root@shell scripts]# declare -i b=$a+10
[root@shell scripts]# echo $b
20
[root@shell scripts]# a=1
[root@shell scripts]# declare b=$a+10
[root@shell scripts]# echo $b
11
declare -i 聲明是整數(shù)型變量 +i取消整數(shù)型屬性
[root@shell scripts]# declare -i wa
[root@shell scripts]# wa="haoge"
[root@shell scripts]# echo $wa
0
[root@shell scripts]# wa=123456
[root@shell scripts]# echo $wa
123456
[root@shell scripts]# declare +i wa
[root@shell scripts]# wa="haoge"
[root@shell scripts]# echo $wa
haoge
4.2 整數(shù)及小數(shù)的運(yùn)算符
bc
yum install bc -y
[root@shell ~]# echo 1+2|bc
3
[root@shell ~]# echo 11-2|bc
9
小數(shù)
echo "scale=2;6/4" |bc
scale是值小數(shù)點(diǎn)后的長度
awk
[root@shell ~]# echo 11.3 2.8|awk '{print $1-$2}'
8.5
python
echo "print 5.0/2" |python
4.3 expr特殊功效
判斷變量是否是整數(shù)(返回0是整數(shù) 返回其他不是整數(shù))
[root@shell scripts]# a=1
[root@shell scripts]# expr $a + 1 &>/dev/null
[root@shell scripts]# echo $?
0
[root@shell scripts]# a=1.1
[root@shell scripts]# expr $a + 1 &>/dev/null
[root@shell scripts]# echo $?
2
計(jì)算字符串長度
[root@shell scripts]# a=abc
[root@shell scripts]# expr length "$a"
3
[root@shell scripts]# a=abc4444
[root@shell scripts]# expr length "$a"
7
判斷文件擴(kuò)展名
if expr "$1" : ".*\.txt$" >/dev/null ; then
echo "你輸入的是$1"
else
echo "對(duì)不起哥谷,格式錯(cuò)誤"
fi
[root@shell scripts]# sh judge.sh haoge.txt
你輸入的是haoge.txt
[root@shell scripts]# sh judge.sh haoge.log
對(duì)不起岸夯,格式錯(cuò)誤
5. read 和用戶交互,接收用戶的輸入
-p 提示
-t 超時(shí)時(shí)間
-s 不顯示終端的任何輸入
[root@shell scripts]# read -p "請(qǐng)輸入一個(gè)數(shù)字:" num
請(qǐng)輸入一個(gè)數(shù)字:300
[root@shell scripts]# echo $num
300
相當(dāng)于num=300
練習(xí)題
實(shí)現(xiàn)一個(gè)加減乘除等功能的計(jì)算器
要求
1)必須輸入兩個(gè)東西
2)必須為數(shù)字
3)運(yùn)算