http://www.linuxeden.com/edu/doctext.php?docid=1337
1.{} 的用法
確認(rèn)你有一個叫file和一個叫file1的變量。能夠使用以下的語句給它們賦值:
$ file=this
$ file1=that
$echo $fileand$file1 尋找變量fileand,file1
sh: fileand: parameter not set
$ echo ${file} and $file1 尋找變量file,file1
thisandthat
花括號被用來區(qū)分變量名和周圍的文本
2.()的用法
命令替代
語法:
$(command)
例子:
$pwd
/home/user2
$ curdir=$(pwd)
$ echo $curdir
/home/user2
$ cd /tmp
$ pwd
$ cd $curdir
$ pwd
/home/user2
命令替代用來替代一個命令和命令行輸出。命令替代的標(biāo)準(zhǔn)語法,也是POSIX鼓勵的一種語法是:$(command).
命令替代讓你捕獲一個命令的輸出,用它作為另一個命令的參數(shù),或是賦值給一個變量。象在變量替代中一樣,命令替代的執(zhí)行是在命令行開始之前完成的。當(dāng)命令行輸出包含回車換行,它們會被空格代替。
同變量替代相似,命令替代使用一個美元符號之后的用括號包圍的一個命令。
所有有效的shell腳本都可以加入命令替代。Shell 掃描每行腳本,執(zhí)行它發(fā)現(xiàn)的開始于一個開括號,結(jié)束與于一個閉括號的命令。
命令替代的另外一種格式是用反引號來環(huán)繞一個命令象:
`command`
它和$(command) 是等價的,并且這是Bourne Shell認(rèn)證的唯一的形式。`command`形式可以用在POSIX的腳本中和Bourne Shell的腳本中。
命令替代通常是在將一個命令的輸出賦給一個變量或以后的處理時使用。通常pwd命令將它的輸出送到你的屏幕。當(dāng)你執(zhí)行以下的賦值語句:
$ curdir=$(pwd) 或 $ curdir=`pwd`
pwd 的輸出被賦給變量 curdir。
原帖由 "config t" 發(fā)表:
以前接觸了一些臺灣/香港軟件和技術(shù)人員
其實(shí)并不是他們技術(shù)首先問題
而是他們很務(wù)實(shí),簡潔,而且最重要是應(yīng)用得很適當(dāng),就是把一個簡單的東西發(fā)揮它最大的用處
我最佩服他們這方面的思想
<&n redirect standard input from file descriptor n
>&n redirect standard output to file descriptor n
n<file redirect file descriptor n from file
n>file redirect file descriptor n to file
n>>file redirect file descriptor n to file. Create file if non-existent, else overwrite.
n>| file redirect file descriptor n to file. Create file even if noclobber is enabled.
n<&m redirect file descriptor n input from file descriptor m
n>&m redirect file descriptor n output to file descriptor m
n<>file open file for reading and writing as file descriptor n
n<<word redirect to file descriptor n until word is read
n<<-word redirect to file descriptor n until word is read; ignore leading tabs
n<&- close file descriptor n for standard input
n>&- close file descriptor n for standard output
print &un args redirect arguments to file descriptor n. If n is greater than 2, it must first be opened with exec. If n is not specified, the default file descriptor argument is 1 (standard output).
read &un args read input line from file descriptor n. If n is greater than 2, it must first be opened with exec. If n is not specified, the default file descriptor argument is 0 (standard input).
原帖由 "黑駿馬" 發(fā)表:
網(wǎng)中人兄:
能不能把:
其中的 IFS 是 shell 預(yù)設(shè)使用的欄位分隔符號,可以由一個及多個如下按鍵組成:
* 空白鍵(White Space)
* 表格鍵(Tab)
* 回車鍵(Enter)
IFS講詳細(xì)一點(diǎn)?
謝謝!
若從技術(shù)細(xì)節(jié)來看,shell 會依據(jù) IFS(Internal Field Seperator) 將 command line 所輸入的文字給拆解為"字段"(word)。
然後再針對特殊字符(meta)先作處理,最後再重組整行 command line 。
若從技術(shù)細(xì)節(jié)來看,shell 會依據(jù) IFS(Internal Field Seperator) 將 command line 所輸入的文字給拆解為"字段"(word)。
然後再針對特殊字符(meta)先作處理,最後再重組整行 command line 。
cd /etc/aa/bb/cc可以執(zhí)行
但是把這條命令寫入shell時shell不執(zhí)行!
這是什么原因呀!
cd /etc/aa/bb/cc可以執(zhí)行
但是把這條命令寫入shell時shell不執(zhí)行!
這是什么原因呀!
因?yàn),一般我們跑?shell script 是用 subshell 去執(zhí)行的。
從 process 的觀念來看,是 parent process 產(chǎn)生一個 child process 去執(zhí)行,
當(dāng) child 結(jié)束後,會返回 parent ,但 parent 的環(huán)境是不會因 child 的改變而改變的。
所謂的環(huán)境元數(shù)很多,凡舉 effective id, variable, workding dir 等等...
其中的 workding dir ($PWD) 正是樓主的疑問所在:
當(dāng)用 subshell 來跑 script 的話,sub shell 的 $PWD 會因?yàn)?cd 而變更,
但當(dāng)返回 primary shell 時,$PWD 是不會變更的。
原帖由 "lzblzb" 發(fā)表:
綱中人版主可不可以說說var=${str=expr}怎么理解啊!
對于
str=
var=${str=expr}
我的理解是先賦給str空值,然后再用str=expr這個語句來把空值用expr替代!所以我認(rèn)為
echo $str和echo $var也都應(yīng)該是expr.
原帖由 "網(wǎng)中人" 發(fā)表:
嗯,這個前面已經(jīng)討論過了,我不再重複了...
原帖由 "網(wǎng)中人" 發(fā)表:
請問你是用 bash 嗎?
若你寫成 script 的話,確定第一行有定義 interpreter :
#!/bin/bash
歡迎光臨 Chinaunix (http://www.72891.cn/) | Powered by Discuz! X3.2 |