2015-11-03 97 views
0

我試圖讀出名爲directorylist的文件的某一行。將某行文件寫入變量

我試圖調試它,但沒有找到任何幫助。看起來,命令變量不是它應該是的「6p」。

(計數器是6現在)

p=p 
command="$counter$p" 


user= sed -n '$command' directorylist 

chown $user:$user /home/$user 

那些有人知道問題出在哪裏?

在此先感謝

EDIT1:

您好,感謝的快速響應。

這是迄今爲止整個腳本:

#!/bin/bash 

rm -f directorylist 

touch directorylist 


array=($(ls /home)) 

printf "%s\n" "${array[@]}" >> directorylist 

counter= wc -l <directorylist 


recounter=0 


while [[ $recounter != $counter ]] 

do 

((recounter=recounter+1)) 


p=p 
command="$counter$p" 


user= sed -n '$command' directorylist 

chown $user:$user /home/$user 

done 

EDIT2:

出於某種原因,在可變寫作 「ommand」:

chown: invalid user: ‘ommand:ommand’ 

EDIT3:

你說得對,問題與報價。

但仍有兩件事情我不明白第一:

chown: cannot access ‘random’: No such file or directory 

這個目錄肯定是存在的。

其次是事實,它似乎忽略了循環條件。

到目前爲止有六個用戶(以及六個主目錄)。第一個計數器就是這個數字。第二個從零開始,並繼續上升一個。只要不相等,循環應該繼續。但是,這隻需要6個循環。 :/

Edit4:

好像

command="$counter$p" 

排空是出於某種原因$計數器。

所以命令變量只包含「p」。

+0

不要發明自己的語法。閱讀命令替換。 https://www.gnu.org/software/bash/manual/bash.html#Command-Substitution – 4ae1e1

+0

呵呵,引用,這也是錯誤的。 https://www.gnu.org/software/bash/manual/bash.html#Quoting – 4ae1e1

+0

這裏還需要更多的上下文。 $ counter從哪裏來,$ p的用途是什麼 - 我可以向你展示正確的語法,但並非沒有一個有意義的例子。 – asimovwasright

回答

0

現在的工作;-)

#!/bin/bash -x 

rm -f directorylist 

touch directorylist 


array=($(ls /home)) 

printf "%s\n" "${array[@]}" >> directorylist 

counter="$(wc -l < directorylist)" 


recounter=0 


while [ "$recounter" != "$counter" ] 

do 

((recounter=recounter+1)) 


p=p 
command="$counter$p" 


user="$(sed -n $command directorylist)" 

chown $user:$user /home/$user 

done 
+0

感謝您提供修復程序,但似乎循環不再運行。 :/ – Twinhand

+0

啊,我明白了,是我的錯,我用錯誤的變量來指定命令變量。非常感謝。 – Twinhand

+0

很高興能提供幫助:-) – asimovwasright

0

嘗試:

#!/bin/bash 

rm -f directorylist 
touch directorylist 

array=($(ls /home)) 

printf "%s\n" "${array[@]}" >> directorylist 

counter=$(wc -l < directorylist) 

recounter=0 

p=p 

while [[ $recounter != $counter ]] 

do 

((recounter=recounter+1)) 

command="$counter$p" 


user=$(sed -n "$command" directorylist) 

sudo chown $user:$user /home/$user 

done 

EDIT2:

出於某種原因,在可變寫作 「ommand」:

chown: invalid user: ‘ommand:ommand’ 

Becouse的單引號'

+0

雙引號內的單引號是文字字符,即它們不影響任何內容。試試我的答案,看看是否有幫助。 – asimovwasright

+0

@asimovwasright我做了,迴應'「ommand」' - 就像它應該。檢查這個http://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash – Noproblem

+0

哦,好吧,你把評論下錯誤的答案然後。我會建立我的測試環境,看看我能否弄清楚,也許一個小時。 – asimovwasright