#!/bin/bash
#1 輸出
function sayHello(){
echo 'hello shell'
}
#sayHello
#2 定義變量
function defVar(){
my_name='neo'
echo ${my_name}
}
#defVar
#3 循環(huán)輸出
function testFor(){
for script in java php mysql js; do
echo "i am good at ${script}-script"
done
}
#testFor
function testFor02(){
arr=(java php mysql js)
for skill in ${arr[*]}; do
echo "i can use ${skill}-script"
done
}
#testFor02
#4 只讀變量
function testReadonly(){
country_name='china'
readonly country_name
country_name='japan'
}
#testReadonly
#5 刪除變量
function testDelVar(){
test_del='test del'
unset test_del
echo ${test_del}
}
#testDelVar
#6 單引號(hào)
function test01(){
my_name='neo'
echo 'hello, i am ${my_name}'
}
#test01
#7 雙引號(hào)
function test02(){
my_name='neo'
echo "hello, i am ${my_name}"
}
#test02
#8 拼接字符串 https://blog.csdn.net/lixiaohuiok111/article/details/18313039
function testStrConcat(){
my_name='neo'
hello="hello, i am "${my_name}" !\n"
hello_2="hello, i am ${my_name} !"
echo -e ${hello}${hello_2}
}
#testStrConcat
#9 獲取字符串長度
function getStrLen(){
str="abdc"
echo ${#str}
}
#getStrLen
#10 提取子字符串
function subStr(){
str="i am study shell"
substr=${str:5:5} #從第6個(gè)字符開始截取5個(gè)字符【如果#接到}后面揪胃,變成字符串拼接】
echo ${substr}
}
#subStr
#函數(shù)傳參
function subStr02(){
str="i am study shell"
substr=${str:${1}:${2}}
echo ${substr}
}
#subStr02 11 5
#11 查找子字符串 https://blog.csdn.net/iamlaosong/article/details/54728393
#【 以下腳本中 "`" 是反引號(hào),而不是單引號(hào) "'"】
function testExpr(){
str="runoob is a great site"
echo `expr index "${str}" is` #查找字符"i或s"的位置
echo $(expr index "${str}" is) #查找字符"i或s"的位置【推薦】
}
#testExpr
#12 Shell數(shù)組
function testArr(){
arr=('西游' '水滸' '三國' '紅樓夢(mèng)')
echo ${arr[0]}
echo ${arr[@]} #遍歷數(shù)組
echo ${arr[*]} #遍歷數(shù)組
echo ${#arr[@]} #獲取數(shù)組長度
echo ${#arr[*]} #獲取數(shù)組長度
echo ${#arr[3]} #獲取某個(gè)元素的長度
}
#testArr
#13 Shell傳遞參數(shù) 沽翔。蛛倦。察蹲。
#14 if判斷 注意判斷條件[]中的空格
# https://www.jb51.net/article/34332.htm
# https://www.cnblogs.com/avivahe/p/5635911.html
function testIf(){
if [ ${1} -eq 1 ]; then
echo 'num -eq 1'
elif [ ${1} -eq 2 ]; then
echo 'num -eq 2'
else
echo 'num -eq ?'
fi
}
#testIf 2
function testIf02(){
if [ ${1} == 1 ]; then
echo 'num == 1'
elif [ ${1} == 2 ]; then
echo 'num == 2'
else
echo 'num == ?'
fi
}
#testIf02 3
function test04(){
if [ 1 == 1 ]; then
echo? '1 == 1'
else
echo '1 != 1'
fi
if [ 2 -eq 2 ]; then
echo 2 -eq 2
else
echo 2 -ne 2
fi
}
#test04
#15 算數(shù)運(yùn)算符 【2 + 2之間必須有空格】
#``好處是各個(gè)版本linux都可用锰瘸, $()好處是直觀
#定義和使用函數(shù) https://blog.csdn.net/zbw18297786698/article/details/77802037
function arithmetic(){
a=${1}
b=${2}
add_val=`expr ${a} + $粹淋`
echo "a + b -eq ${add_val}"
sub_val=`expr ${a} - $监透`
echo "a - b -eq ${sub_val}"
chu_val=`expr ${a} / $蛋辈`
echo "a / b -eq ${chu_val}"
yu_val=`expr ${a} % $属拾`
echo "a % b -eq ${yu_val}"
a=$ #變量之間賦值
echo "a -eq ${a}"
}
#arithmetic 20 10
#16 字符串運(yùn)算符function strExpr(){a=${1}b=${2}if [ ${a} = $梯浪 ]; thenecho "a 等于 b"elseecho "a 不等于 b"fiif [ ${a} != $捌年 ]; thenecho "a 不等于 b"else echo "a 等于 b"fiif [ -z ${a} ]; thenecho "a 長度為 0"elseecho "a長度不為 0"fiif [ ${a} ]; thenecho "a不為空"else echo "a為空"fi}#strExpr "abc" "efg"#17 文件運(yùn)算符function file(){file=${1}if [ -e ${file} ]; thenecho "文件存在"elseecho "文件不存在"fiif [ -s ${file} ]; thenecho "文件不為空"else echo "文件為空"fiif [ -f ${file} ]; thenecho "普通文件"elseecho "不是普通文件"fiif [[ ! -d ${file} ]]; thenecho "不是文件夾"fiif [ -r ${file} ]; thenecho "可讀"elseecho "不可讀"fiif [ -w ${file} ]; thenecho "可寫"else echo "不可寫"fiif [ -x ${file} ]; then echo "可執(zhí)行"elseecho "不可執(zhí)行"fi}#touch test.exe#echo "i am a file" > test.exe#chmod +x ./test.exe#file "test.exe"#18 echo命令function readInput(){read nameecho "${name} has been read"}#readInput# 換行輸出function output(){echo -e "echo實(shí)現(xiàn)換行:\n 加上-e"}#output# 不換行輸出function output02(){echo -e "echo實(shí)現(xiàn)不換行 \c"echo "test"}#output02# 輸出至文件function output03(){echo "give you sth" > myfile}#output03#cat myfile# 顯示命令執(zhí)行結(jié)果,使用``[反引號(hào)]#echo `date`#echo $(date +%F%n%T)#echo date("Y-m-d H:i:s");#%n: 空格#%F:年月日#%T:時(shí)分秒function testPrintf(){printf "%-10s %-8s %-4s\n" 姓名 性別 體重printf "%-10s %-8s %-4.2f\n" 郭靖 男 66.1234printf "%-10s %-8s %-4.2f\n" 楊過 男 48.8803printf "%-10s %-8s %-4.2f\n" 郭芙 女 45.55623}function comment(){%s %c %d %f都是格式替代符%-10s 指一個(gè)寬度為10個(gè)字符(-表示左對(duì)齊挂洛,沒有則表示右對(duì)齊)礼预,任何字符都會(huì)被顯示在10個(gè)字符寬的字符內(nèi),如果不足則自動(dòng)以空格填充虏劲,超過也會(huì)將內(nèi)容全部顯示出來托酸。%-4.2f 指格式化為小數(shù),其中.2指保留2位小數(shù)柒巫。}#testPrintf#19 流程控制# let命令:http://www.runoob.com/linux/linux-comm-let.html#let 不需要空格隔開表達(dá)式的各個(gè)字符励堡。#而 expr 后面的字符需要空格隔開各個(gè)字符。function testWhile(){i=1while (( ${i} <= 5 )) doecho ${i}let "i++"#i=`expr ${i} + 1`done}#testWhilefunction testWhile02(){echo "按下推出"
echo -n "輸入你最喜歡的網(wǎng)站:"
while read site
do
echo "${site} is good"
done
}
#testWhile02
function testCase(){
num=${1}
case ${num} in
1) echo '選1'
;;
2) echo '選2'
;;
3) echo '選3'
;;
*) echo '沒選'
;;
esac #case反寫作為結(jié)束
}
#testCase 5
function testBreak(){
num=${1}
while :
do
case ${num} in
1|2|3) echo "您的選擇是:${num}"
;;
*) echo "您沒有在1-3之間選擇堡掏,游戲結(jié)束"
? break
;;
esac
done
}
#testBreak
#20 函數(shù)
function add(){
return $((${1}+${2}))
}
#add 1 2
#echo "1 + 2 = ${?}" #使用${?}獲取返回值
function param(){
echo "第一個(gè)參數(shù):${1}"
echo "參數(shù)總數(shù):${#}"
echo "所有參數(shù):${*}"
}
#param 1 2 3 4 5 6 7 8
#21 輸入輸出
function testOutput(){
touch users
echo -e "hello file \c" > users
echo "append txt" >> users
cat users
}
#testOutput