2012-10-09 20 views
3

我正在寫一個腳本,你可以傳入一個文件名作爲參數,它只會運行,如果它是一個特定的文件擴展名。通過文件擴展名驗證腳本的參數?

flac2mp3 "01 Song.flac" 

flac2mp3 "01 Song.FLAC" 

我知道有很多劇本在那裏向你展示如何FLAC轉換成MP3,但是這是我的劇本,我想學習如何使用編寫腳本這種方法。

這是我可以學習的參數,當我想轉換隻有1個人的文件。 (對於多個文件,我只是寫了一個for循環,內部帶有* .flac)

我只想學習如何檢查$ 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 

請和謝謝您的幫助。

回答

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. 

如果您在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」灰。

+0

謝謝! 因此,內置shopt只是爲了不區分大小寫?我只需要: 如果[[$ 1!= * flac]] 並將其放在雙方括號內? –

+0

看到我編輯的帖子,是的,'如果[[$ 1!= * .flac]]' –

+0

謝謝。我剛剛意識到,shopt的意思是「外殼選項」。我會查找shopt和bash設置。我希望在我正在閱讀的書中提到它。 「Linux命令,編輯器和Shell編程實用指南」 –