【问题标题】:Validate script's argument by file extension?通过文件扩展名验证脚本的参数?
【发布时间】:2012-09-29 18:25:28
【问题描述】:

我正在编写一个脚本,您可以将文件名作为参数传递给它,并且它只会在具有特定文件扩展名的情况下运行。

flac2mp3 "01 Song.flac"

flac2mp3 "01 Song.FLAC"

我知道那里有很多脚本向您展示如何将 flac 转换为 mp3,但这是我的脚本,我想学习如何使用这种方法编写脚本。

这样我就可以学习参数以及当我想只转换 1 个单独的文件时。 (对于多个文件,我只是在脚本中使用 *.flac 编写了一个 for 循环)

我只是想学习如何检查 $1 参数是否包含 *.[Ff][Ll][Aa][Cc]

这是我到目前为止从互联网上拼凑出来的(我知道这是令人尴尬的错误,但我想展示我的目标):

#!/bin/bash
#flac2mp3

if [ -z $1 ] && [[$1 !=~ *.[Ff][Ll][Aa][Cc]]];then echo "Give FLAC File Name"; exit 0;fi

OUTF=${1%.flac}.mp3

ARTIST=$(metaflac "$1" --show-tag=ARTIST | sed s/.*=//g)
TITLE=$(metaflac "$1" --show-tag=TITLE | sed s/.*=//g)
ALBUM=$(metaflac "$1" --show-tag=ALBUM | sed s/.*=//g)
GENRE=$(metaflac "$1" --show-tag=GENRE | sed s/.*=//g)
TRACKNUMBER=$(metaflac "$1" --show-tag=TRACKNUMBER | sed s/.*=//g)
DATE=$(metaflac "$1" --show-tag=DATE | sed s/.*=//g)

flac -c -d "$1" | lame -m j -q 0 --vbr-new -V 0 -s 44.1 - "$OUTF"
id3 -t "$TITLE" -T "${TRACKNUMBER:-0}" -a "$ARTIST" -A "$ALBUM" -y "$DATE" -g "${GENRE:-12}" "$OUTF"

done

请并感谢您的帮助。

【问题讨论】:

    标签: linux validation shell arguments


    【解决方案1】:

    试试下面的代码:

    shopt -s nocasematch
    
    if [[ $1 == *flac ]]; then
        echo "ok"
    fi
    

    这是不区分大小写的。

    编辑

    $ LANG=C help shopt
    shopt: shopt [-pqsu] [-o] [optname ...]
        Set and unset shell options.
    
        Change the setting of each shell option OPTNAME.  Without any option
        arguments, list all shell options with an indication of whether or not each
        is set.
    
        Options:
          -o        restrict OPTNAMEs to those defined for use with `set -o'
          -p        print each shell option with an indication of its status
          -q        suppress output
          -s        enable (set) each OPTNAME
          -u        disable (unset) each OPTNAME
    
        Exit Status:
        Returns success if OPTNAME is enabled; fails if an invalid option is
        given or OPTNAME is disabled.
    

    如果您在 shell 中单独运行 shopt,您将看到所有可用选项:

    $ shopt
    autocd          on
    cdable_vars     on
    cdspell         off
    checkhash       off
    checkjobs       off
    checkwinsize    off
    cmdhist         on
    compat31        off
    compat32        off
    compat40        off
    compat41        off
    direxpand       off
    dirspell        off
    dotglob         on
    execfail        off
    expand_aliases  on
    extdebug        off
    extglob         on
    extquote        on
    failglob        off
    force_fignore   on
    globstar        on
    gnu_errfmt      off
    histappend      on
    histreedit      off
    histverify      off
    hostcomplete    off
    huponexit       off
    interactive_comments    on
    lastpipe        off
    lithist         off
    login_shell     off
    mailwarn        off
    no_empty_cmd_completion off
    nocaseglob      off
    nocasematch     off
    nullglob        off
    progcomp        on
    promptvars      on
    restricted_shell        off
    shift_verbose   off
    sourcepath      on
    xpg_echo        off
    

    要知道所有这些选项的作用:

    man bash | less +/'^SHELL BUILTIN COMMANDS'
    

    然后在此部分中搜索“shopt”。

    【讨论】:

    • 谢谢!所以内置 shopt 只是为了不区分大小写?我所要做的就是:如果 [[ $1 != *flac ]] 并将其放在双方括号内?
    • 谢谢。我刚刚意识到 shopt 的意思是“外壳选项”。我会查找 shopt 和 bash 设置。我希望在我正在阅读的书中提到它。 《Linux 命令、编辑器和 Shell 编程实用指南》
    猜你喜欢
    • 2012-12-11
    • 2016-10-26
    • 2018-11-26
    • 1970-01-01
    • 2021-04-29
    • 2011-04-07
    • 2014-11-06
    • 1970-01-01
    • 2017-07-02
    相关资源
    最近更新 更多