shell技能大全

发布时间 2023-11-14 09:55:16作者: Franson

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