When writing a script (Shell), you will often encounter the path name, file name, and so on in the filename including the full path.
This includes a full path file named
/usr/local/apach2/etc/httpd.conf
Defined as
File= "/usr/local/apach2/etc/httpd.conf"
(1) Extract filename
filename=${file##*/}
##*/represents the deletion from the front to the last/appearing part, so filename is httpd.conf.
(2) Extract the file name extension
ext=${file##*.}
As with (1), delete from the front to the last. Section, ext for CONF.
(3) Extraction of httpd
withoutext=${file%.*}
%.* deletes from the last start to the 1th time. The part of the without is/usr/local/apach2/etc/httpd.
(4) Extract directory name
dirname=${file%/*}
%/* deletes from the last start to the 1th occurrence/part, dirname for/USR/LOCAL/APACH2/ETC.
# #和 percent represents the longest consistent, while # and% indicate the shortest consistent. #表示从前面,% expressed from behind.
Here is a sample script
code is as follows |
copy code |
#!/bin/ Bash file= "/usr/local/apach2/etc/httpd.conf" filename=${file##*/} ext=${file##*.} withoutext=${file%.*} dirname=${file%/*} Echo $FILENAME Echo $EXT Echo $WITHOUTEXT Echo $ DirName Execution results are httpd.conf conf /usr/local/apach2/etc/httpd /usr/local/apach2/etc | /tr>