将某行文件写入变量

write certain line of file into variable

我正在尝试读出名为 directorylist 的文件的特定行。

我尝试调试它,但没有找到任何帮助。看起来,命令变量不是它应该的“6p”。

(此时计数器为6)

p=p
command="$counter$p"


user= sed -n '$command' directorylist

chown $user:$user /home/$user

谁知道问题出在哪里?

提前致谢

编辑 1:

您好,感谢您的快速回复。

这就是到目前为止的整个脚本:

#!/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

编辑2:

出于某种原因,它在变量中写入 "ommand":

chown: invalid user: ‘ommand:ommand’

编辑 3:

你是对的,问题出在引号上。

但是还有两件事我没有首先得到:

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

这个目录肯定存在

其次,它似乎忽略了循环条件。

到目前为止有六个用户(以及六个主目录)。第一个计数器是这个数字。第二个从零开始,然后继续增加一。只要不相等,循环就应该继续。但这应该只需要 6 个循环。 :/

编辑4:

好像是

command="$counter$p"

由于某种原因正在清空 $counter。

所以命令变量只包含"p"。

现在工作 ;-)

#!/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

尝试:

#!/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:

For some reason it is writing "ommand" in the variable:

chown: invalid user: ‘ommand:ommand’

因为单引号 '