0

I am trying to read out a certain line of a file called directorylist.

I tried to debug it but I found nothing that helped. It just seemed, that the command variable is not the "6p" it should be.

(The counter is 6 by now)

p=p
command="$counter$p"


user= sed -n '$command' directorylist

chown $user:$user /home/$user

Those someone know where the problem is?

thanks in advance

Edit1:

Hi , thanks for the fast respond.

That is the whole script so far:

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

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

chown: invalid user: ‘ommand:ommand’

Edit3:

You were right, the problem were with the quotes.

But there are still two things I do not get first:

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

This directory is certainly existing.

Secondly is the fact, that it seems to ignore the loop conditions.

There are six users so far (as well as six home directories). The first counter is this number. The second one start at zero and continues to go up by one. The loop should continue as long as there are not equal. But that should only take 6 loops. :/

Edit4:

It seems like

command="$counter$p"

Is emptying $counter for some reason.

So the command variable just contains "p".

Twinhand
  • 15
  • 1
  • 5
  • Don't invent your own syntax. Read up on command substitution. https://www.gnu.org/software/bash/manual/bash.html#Command-Substitution – 4ae1e1 Nov 03 '15 at 08:18
  • Oh, and quoting, which is also wrong. https://www.gnu.org/software/bash/manual/bash.html#Quoting – 4ae1e1 Nov 03 '15 at 08:19
  • You also need more context here. Where is $counter coming from, and what is the purpose of $p - I can show you correct syntax, but not without a meaningful example. – asimovwasright Nov 03 '15 at 08:32
  • ok I get it, this must be in a loop? Can you provide the loop structure in the example, so we can see that it is working. – asimovwasright Nov 03 '15 at 08:35

2 Answers2

0

Working now ;-)

#!/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
asimovwasright
  • 838
  • 1
  • 11
  • 28
0

Try:

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

Becouse of single quote '

Noproblem
  • 745
  • 1
  • 6
  • 15
  • Single quotes within double quotes are literal characters i.e. they do not affect anything. Try My answer and see if that helps. – asimovwasright Nov 03 '15 at 08:58
  • @asimovwasright i did, response `"ommand"` - like it should by. Check this http://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash – Noproblem Nov 03 '15 at 09:05
  • Oh, ok, you put the comment under the wrong answer then. I will set up my test environment and see if I can figure it out, maybe one hour. – asimovwasright Nov 03 '15 at 10:41
  • @asimovwasright just try this example: `a=5 echo "$a" echo '$a'`. – Noproblem Nov 03 '15 at 10:46
  • Echo "$a" will show expanded value of 'a' variable. '$a' will show literal '$a' but when there are single quotes within double quotes, the shell expands the values of all special characters within the entire double quoted string, and the single quotes are expanded as single quotes- literally. – asimovwasright Nov 03 '15 at 10:49