shell技能大全

1.执行shell脚本提示输入并根据输入内容执行

#!/bin/bash
if [ $# -ne 3 ];then
        echo "usage: $(basename $0) par1 par2 par3"
        exit
fi
myfunc() {
        echo $(($1*$2*$3))
}
result=`myfunc $1 $2 $3`
echo "$result"

2.替换变量中的子字符串(空格)

myStr="hello world"
myWhiteSpaceChar="{_}"
myStr=`echo $myStr |sed "s# #$myWhiteSpaceChar#g"`
echo $myStr
输出:
hello{_}world

3.替换文件中的子字符串(空格)并输出到新的文件

myWhiteSpaceChar="{_}"
sed "s# #${myWhiteSpaceChar}#g" ${tmpDir}/api.records.tmp >${tmpDir}/api.records.deal.tmp

 4.字符串根据指定分隔符转数组并遍历

string="郭靖,黄蓉,神雕侠"
**⽅式⼀:**
array=(`echo$string| tr ','' '` )
**⽅式⼆:**
array=(${string//,/})
**⽅式三:**
array=(`echo$string| sed 's/,/ /g'`)
#遍历数组
for var in ${array[@]}
 do
   echo$var
 done

 

posted @ 2023-11-14 09:50  Franson  阅读(3)  评论(0编辑  收藏  举报