次のようなスクリプトがあります。
for html in ./templates/*.html
do
# Here I need only the file name without extension.
done
ループ内で拡張子のないファイル名のみを取得するにはどうすればよいですか?
編集:私はUNIXが初めてです。
使用basename
:
basename "$html" .html
または、bash の正規表現を使用して、パスをディレクトリ プレフィックス、ベース名、およびファイル サフィックスに分割することもできます。
$ [[ "/usr/share/doc/bzip2-devel-1.0.6/manual.html" =~ (.+/)?([^/]+)(\.[^/]+) ]]; echo ${BASH_REMATCH[*]}
/usr/share/doc/bzip2-devel-1.0.6/manual.html /usr/share/doc/bzip2-devel-1.0.6/ manual .html
^ ^ ^ ^
| | | |
entire string directory base suffix
これを試して
./templates/*.html の html の場合 行う echo $html|sed 's/\.[a-zA-Z]*$//g'; 終わり