shell基礎(chǔ)學(xué)習(xí)中的字符串操作、for循環(huán)語(yǔ)句示例
#!/bin/bash
my_name="jxq"
echo $my_name
echo ${my_name}
# ------------------------------------
# 字符串操作
# ------------------------------------
# 單引號(hào)字符串的限制,雙引號(hào)沒(méi)有這些限制:
# 單引號(hào)里的任何字符都會(huì)原樣輸出,單引號(hào)字符串中的變量是無(wú)效的
# 單引號(hào)字串中不能出現(xiàn)單引號(hào)(對(duì)單引號(hào)使用轉(zhuǎn)義符后也不行)
name="will"
age=24
my_full_name='${name}${age}'
echo ${my_full_name}
my_full_name="${name}${age}"
echo ${my_full_name}
# 字符串拼接
echo ${name}${age}
# 字符串長(zhǎng)度
echo ${#name} # 4
# substring
message="I want to be healthy"
echo ${message:2} # want to be health, 2是position
echo ${message:2:4} # want,2是position,4是len
# delete shortest match from front: ${string#substring}
echo ${message#*want}
# delete shortest match from back: ${string%substring}
echo ${message%healthy}
# delete longest match from front: ${string##substring}
echo ${message##*h}
# delete longest match from back: ${string%%substring}
echo ${message%%t*}
# find and replace: ${string/pattern/replacement}
book_name="Catch Eye Eye"
echo ${book_name/Eye/Cat}
# find and replace all match: ${string//pattern/replacement}
echo ${book_name//Eye/Cat}
file_path="/usr/local/bin"
# only replace when pattern match the beginning: ${string/#pattern/replacement}
echo ${file_path/#\/usr/tmp}
# only replace when pattern match the end: ${string/%pattern/replacement}
echo ${file_path/%bin/tmp}
# string index
stringZ=abcABC123ABCabc
echo `expr index "$stringZ" C12` # Mac OSX不支持expr
# ------------------------------------
# 語(yǔ)句
# ------------------------------------
# if
if true
then
echo "ok, true"
fi
# 寫(xiě)成一行
if true; then echo "ok"; fi
var='12'
if [ $var -eq 12 ]; then
echo "This is a numeric comparison if example"
fi
if [ "$var" = "12" ]; then
echo "This is a string if comparison example"
fi
if [[ "$var" = *12* ]]; then
echo "This is a string regular expression if comparison example"
fi
name="jxq"
if [ "$name" = "jxq" ]; then
echo "hello" $name
fi
# 循環(huán)語(yǔ)句
for item in `ls *.sh`
do
echo $item
echo "completed"
done
# 寫(xiě)成一行
for item in `ls *.sh`; do echo $item; echo "completed"; done;
counter=1
while :
do
echo "bee"
let "counter=$counter+1"
if [ $counter -eq 3 ]; then
break # break/continue與Java類(lèi)似
fi
done
# Case語(yǔ)句
opt="install"
case "${opt}" in
"install" )
echo "install..."
exit
"update" )
echo "update..."
exit
*) echo "bad opt"
esac
- Shell循環(huán)語(yǔ)句及中斷語(yǔ)句的使用
- shell編程中for循環(huán)語(yǔ)句的實(shí)現(xiàn)過(guò)程及案例
- shell腳本實(shí)戰(zhàn)-while循環(huán)語(yǔ)句
- Shell腳本的條件控制和循環(huán)語(yǔ)句
- shell腳本編程之循環(huán)語(yǔ)句
- shell腳本編程之循環(huán)語(yǔ)句學(xué)習(xí)筆記
- shell中的循環(huán)語(yǔ)句、判斷語(yǔ)句實(shí)例
- Shell腳本while、until循環(huán)語(yǔ)句簡(jiǎn)明教程
- Shell腳本for循環(huán)語(yǔ)句簡(jiǎn)明教程
- Shell中的循環(huán)語(yǔ)句for、while、until實(shí)例講解
- Shell循環(huán)語(yǔ)句的使用(for循環(huán)、while循環(huán)、until循環(huán))
相關(guān)文章
查看某時(shí)間段到現(xiàn)在的系統(tǒng)日志的sed命令
查看某時(shí)間段到現(xiàn)在的系統(tǒng)日志的sed命令,需要的朋友可以參考下2013-02-02
shell腳本實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)雙機(jī)定時(shí)備份的方法
最近有個(gè)需求,要求實(shí)現(xiàn)對(duì)某個(gè)數(shù)據(jù)庫(kù)進(jìn)行雙機(jī)備份,每天凌晨備份一次,要求主機(jī)器只保留最近十五天的記錄,我們決定用shell腳本加定時(shí)任務(wù)的方式來(lái)實(shí)現(xiàn)這個(gè)需求,接下來(lái)通過(guò)本文給大家介紹shell腳本實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)雙機(jī)定時(shí)備份的方法,感興趣的朋友一起看看吧2022-07-07
sed或awk處理文件最后一行的實(shí)現(xiàn)方法
sed或awk處理文件最后一行,供大家學(xué)習(xí)參考2013-02-02
Linux下模擬實(shí)現(xiàn)進(jìn)度條實(shí)例詳解
這篇文章主要介紹了Linux下模擬實(shí)現(xiàn)進(jìn)度條實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
寫(xiě)一個(gè)shell腳本實(shí)現(xiàn)視頻處理
Linux和Unix都擁有很多能夠處理圖像和視頻文件的應(yīng)用程序和工具,下面這篇文章主要給大家介紹了關(guān)于如何寫(xiě)一個(gè)shell腳本來(lái)實(shí)現(xiàn)視頻處理的相關(guān)資料,需要的朋友可以參考下2022-07-07
Bash技巧:把變量賦值為換行符(判斷文件是否以換行符結(jié)尾)
這篇文章主要介紹了Bash技巧:把變量賦值為換行符,判斷文件是否以換行符結(jié)尾,需要的朋友可以參考下2020-08-08
獲取同一網(wǎng)段下所有機(jī)器MAC地址的shell腳本
有時(shí)候需要在當(dāng)前同一網(wǎng)段下所有機(jī)器MAC地址,需要的朋友可以參考下2013-01-01
shell結(jié)合expect寫(xiě)的批量scp腳本工具
expect用于自動(dòng)化地執(zhí)行l(wèi)inux環(huán)境下的命令行交互任務(wù),例如scp、ssh之類(lèi)需要用戶(hù)手動(dòng)輸入密碼然后確認(rèn)的任務(wù)。有了這個(gè)工具,定義在scp過(guò)程中可能遇到的情況,然后編寫(xiě)相應(yīng)的處理語(yǔ)句,就可以自動(dòng)地完成scp操作了2013-02-02
Linux 中C語(yǔ)言getcwd()函數(shù)的用法
這篇文章主要介紹了Linux 中C語(yǔ)言getcwd()函數(shù)的用法的相關(guān)資料,需要的朋友可以參考下2017-04-04

