Читать книгу Linux Bible - Christopher Negus - Страница 203
NOTE
ОглавлениеFor the rest of this section, I show how variables and commands may appear in a shell script. To try out any of those examples, however, you can simply type them into a shell, as shown in the previous example.
In the following example, MYFILENAME
is set to /home/digby/myfile.txt
. Next, the FILE
variable is set to myfile.txt
and DIR
is set to /home/digby
. In the NAME
variable, the filename is cut down simply to myfile
; then, in the EXTENSION
variable, the file extension is set to txt
. (To try these out, you can type them at a shell prompt as in the previous example and echo the value of each variable to see how it is set.) Type the code on the left. The material on the right side describes the action.
MYFILENAME=/home/digby/myfile.txt: Sets the value of MYFILENAME
FILE=${MYFILENAME##*/}: FILE becomes myfile.txt
DIR=${MYFILENAME%/*}: DIR becomes /home/digby
NAME=${FILE%.*}: NAME becomes myfile
EXTENSION=${FILE##*.}: EXTENSION becomes txt