shell编程2

一 语法笔记

二 常用技巧

多行打印

  • 方法1: 使用echo “line 1”; echo “line 2”
  • 方法2:使用EOF
cat <<EOF
line1: hello wolrd

line3: nihao
EOF

去除注释

  1. 去除#开头的注释、去除空行
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值