2015-11-03 116 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