2014-07-18 42 views
0

代碼:猛砸如果塊不運行時,它顯然應該

first=true 
mountpoint=" " 
partlist=`df -h | grep "^/dev"` # get partition info 
for i in $partlist    # loop through info 
do 
    if [[ ${i:0:1} = "/" ]]  # items starting with/are what I'm interested in 
    then 
    if [[ $first ]]    # The first instance in each pair is the device name 
    then 
     mountdev=$i 
     mountpoint=" " 
     first=false 
    else      # The second instance is the device mount point 
     mountpoint=$i 
     first=true 
    fi 
    if [[ $mountpoint != " " ]] # If the mountpoint was just set (last to be set 
           # before printing config) 
    then 
     # Print config 
     echo "${mountpoint} \${fs_size ${mountpoint}" 
     echo "USED \${fs_used ${mountpoint}}\${alignr}\${fs_free ${mountpoint}} FREE" 
     echo "\${fs_bar 3,300 ${mountpoint}}" 
     echo "READ \${diskio_read ${mountdev}}\${alignr}\${diskio_write ${mountdev}} WRITE" 
     echo "" 
    fi 
    fi 
done 

問題:

的目標是創建一個腳本,將生成每個我的電腦我的首選Conky的配置而無需編輯每臺計算機的文件。以上是生成報告我的磁盤使用信息的部分的代碼片段。不幸的是,我無法輸出任何內容。我向其中拋出各種調試回聲,除了實際輸出配置的if語句外,它似乎都能正常工作。但我無法弄清楚它有什麼問題。任何建議或協助?

提前謝謝:)

+0

的可能重複【如何聲明和在shell腳本中使用布爾變量?](http://stackoverflow.com/questions/ 2953646/how-to-declare-and-use-boolean-variables-in-shell-script) – BroSlow

回答

6

這是入錯行:

if [[ $first ]]    # The first instance in each pair is the device name 

這將始終爲true。 [[ true ]][[ false ]][[ -n true ]][[ -n false ]]同義,這總是正確的。

什麼你大概的意思是

if "$first" 

或者

if [[ $first == true ]] 
+0

Thank you,that worked :)我現在覺得這很簡單。 – FatalKeystroke

+0

那麼,使用''「」true「''和''」false「''字符串是醜陋的。你可以使用''1''和''0''然後用'((first))'檢查它,或者你可以使用''1''和空字符串''[[$ first]]' ' –

+2

@ Aleks-DanielJakimenko爲什麼它很醜?爲了減少效率,減少練習?我當然不會同意這是因爲它的可讀性不好。 – konsolebox