一 语法笔记
无
二 常用技巧
多行打印
- 方法1: 使用echo “line 1”; echo “line 2”
- 方法2:使用EOF
cat <<EOF
line1: hello wolrd
line3: nihao
EOF
去除注释
- 去除#开头的注释、去除空行
grep -v '^#\|^$' conf/local.conf.sample > conf/local.conf
最长匹配和最短匹配
也叫贪婪模式和非贪婪模式。比较下面两条命令
echo "<abc>Hello<def>World"|sed 's/<[^>]*>//g'
# 我们不输入“'<' 字符后面跟有一些字符并以 '>' 字符结束”的规则表达式,
# 而只需输入一个“'<' 字符后面跟有任意数量非 '>' 字符并以 '>' 字符结束”的规则表达式。
# 这将是最短、而不是最长的可能性匹配。
echo '<abc>Hello<def>World'|sed 's/<.*>//g'
执行脚本时传入可选参数
参考:https://cloud.tencent.com/developer/article/1529851
需求:
- 不带参数就输出help信息
- 只让确定的参数过如-b
usage()
{
echo -e "\nUsage: source fsl-setup-release.sh
Optional parameters: [-b build-dir] [-h]"
echo "
* [-b build-dir]: Build directory, if unspecified script uses 'build' as output directory
* [-h]: help
"
}
# get command line options
OLD_OPTIND=$OPTIND
unset FSLDISTRO
while getopts "k:r:t:b:e:gh" fsl_setup_flag
do
case $fsl_setup_flag in
b) BUILD_DIR="$OPTARG";
echo -e "\n Build directory is " $BUILD_DIR
;;
h) fsl_setup_help='true';
;;
?) fsl_setup_error='true';
;;
esac
done
if [ -z "$DISTRO" ]; then
if [ -z "$FSLDISTRO" ]; then
FSLDISTRO='fsl-imx-xwayland'
fi
else
FSLDISTRO="$DISTRO"
fi
OPTIND=$OLD_OPTIND
# check the "-h" and other not supported options
if test $fsl_setup_error || test $fsl_setup_help; then
usage && clean_up && return 1
fi
脚本执行过程中输入数据
REPLY=
while [ -z "$REPLY" ]; do
echo -n "Do you accept the EULA you just read? (y/n) "
read REPLY
case "$REPLY" in
y|Y)
echo "EULA has been accepted."
echo "ACCEPT_FSL_EULA = \"1\"" >> conf/local.conf
;;
n|N)
echo "EULA has not been accepted."
;;
*)
REPLY=
;;
esac
done