3

我正在编写一个脚本,您可以将文件名作为参数传递给它,并且它只会在它是某个文件扩展名时运行。

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

请并感谢您的帮助。

4

1 回答 1

0

试试下面的代码:

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.

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

$ 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”。

于 2012-10-09T12:15:28.377 回答