${strings}other_stuffshell
匆匆搜索後,看到有人說這是shell中的字符擴展。原文中叫${filename} Construct。由於,這裏討論的是對文件進行操做時,文件名之間有一部分相同,有一部分不一樣。這個時候,怎麼處理呢?app
用ssh登陸系統後,寫一個最簡單的程序,取名叫prossh
vi procurl
//在pro裏面寫這樣兩行網站
$filename=tui
mv $filename $filename1this
好比Apache日誌文件,要是置之不理,會變得很是大,達到數十G。固然,這要看網站的訪問量。能夠將這些日誌文件拆成小塊。每週保存爲一個。只保留最近4個星期的日誌文件。星期天晚上12點,就須要將日誌更名。後面那個1變成當天的日期,如suffix=`date -d today +%Y-%m-%d`,與上例相似。url
保存,退出。讓這個程序能夠執行。日誌
chmod +x prorem
在當前目錄建立一個文件名字叫t
touch t
而後運行這個pro
./pro //我在當前路徑下
我但願看到,當前路徑下的t,變成t1。可是運行以後,出錯了。
mv: ‘./t’ and ‘./t’ are the same file
叫你爲文件改個名而已,內容確定是同樣的。咋這麼多廢話呢?不對,它在操做的是,將t更名爲t,而不是我想要的t1。不但內容同樣,並且文件名還同樣。按照下面,將pro修改一下,就行了。
mv $filename ${filename}1
實際應用中,可能會是這樣一種狀況:
filename=access_log
suffix=`date -d today +%Y-%m-%d`
mv $filename ${filename}$suffix
先把原文拷貝在這兒吧。
The ${variable} Construct
Suppose that you have the name of a file stored in the variable filename. If you wanted to
rename that file so that the new name was the same as the old, except with an X added to the
end, your first impulse would be to type
mv $filename $filenameX
When the shell scans this command line, however, it substitutes the value of the variable
filename and also the value of the variable filenameX. The shell thinks filenameX is the full
name of the variable because it’s composed entirely of valid characters.
To avoid this problem, delimit the variable by enclosing the entire name (but not the leading
dollar sign) in curly braces, as in
${filename}X
This removes any ambiguity, and the mv command then works as desired:
mv $filename ${filename}X
Remember that the braces are necessary only if the last character of the variable name is
followed by an alphanumeric character or an underscore.
There are also quite a few functions applicable to variables within this curly brace notation, including extracting subsets, assigning values if the variable is currently unassigned, and more. Stay tuned for those!