LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to do a script which read line by line /passwd using while (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-do-a-script-which-read-line-by-line-passwd-using-while-807664/)

giantjavi 05-13-2010 03:52 PM

How to do a script which read line by line /passwd using while
 
Hi everyone, I have to do several scripts and I have no idea of how to do this one: Make a script that read line by line the passwd file and prints in console.
Hope you understand couse my english is so bad as you can see.
Our teacher told us something like this:

#!/bin/bash

while read line do

echo $linea

done < dispositive

exit

chrism01 05-13-2010 06:33 PM

As this is a homework, I'll just say this:

1. give it a try based on what you've been taught; you wouldn't be asked to do this if you hadn't covered the relevant info in class/your books
Show us what you've done
2. read/bookmark these
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
3. see the LQ Rules re homework http://www.linuxquestions.org/linux/rules.html

giantjavi 05-13-2010 08:35 PM

see.. i read a lot from the internet but i canīt do this script. Iīve done 17 scripts already, but canīt make thisone work.. im not trying someone to do my homework. Itīs just an exercise

grail 05-13-2010 09:56 PM

So what seems to be the problem?
Based on what you have so far it seems to already be solved??

pixellany 05-13-2010 10:54 PM

Per the LQ Rules, please do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and Google searches) and we'll do our best to help. Also, keep in mind that your instructor might also be an LQ member.

Your example from your teacher is almost there....

catkin 05-14-2010 01:06 AM

Quote:

Originally Posted by giantjavi (Post 3967718)
but canīt make thisone work..

If you posted your script and explained how it doesn't work (does not do what is wanted, gives error messages ...) then we would help.

giantjavi 05-14-2010 02:08 AM

#!/bin/bash

AR=/home/practicas/tp2/nuevo/home/practicas/tp1/passwd

exec 3<&0
exec 0<$AR
while read line
do
echo $line

done
exec 0<&3


I did this, what do you think now? how can i do that it prints line by line when the operator press the enter button?

catkin 05-14-2010 02:15 AM

Quote:

Originally Posted by giantjavi (Post 3967973)
#!/bin/bash

AR=/home/practicas/tp2/nuevo/home/practicas/tp1/passwd

exec 3<&0
exec 0<$AR
while read line
do
echo $line

done
exec 0<&3


I did this, what do you think now? how can i do that it prints line by line when the operator press the enter button?

The fact that the script is waiting for user input shows that the exec 0<$AR command has not worked to redirect input from /home/practicas/tp2/nuevo/home/practicas/tp1/passwd.

There's no need for the redirects. Most simply you could change the script to
Code:

#!/bin/bash

while read line
do
    echo $line
done < /home/practicas/tp2/nuevo/home/practicas/tp1/passwd

I'll post again about why the execs are not working as intended.

EDIT: Maybe I will not post about why the execs are not working as intended because they are OK (for tidiness you could close 3 after the last line of the script). Are you sure that /home/practicas/tp2/nuevo/home/practicas/tp1/passwd exists? What happens if you change AR=/home/practicas/tp2/nuevo/home/practicas/tp1/passwd to AR=/etc/passwd?

giantjavi 05-15-2010 07:26 AM

Quote:

Originally Posted by catkin (Post 3967979)
The fact that the script is waiting for user input shows that the exec 0<$AR command has not worked to redirect input from /home/practicas/tp2/nuevo/home/practicas/tp1/passwd.

There's no need for the redirects. Most simply you could change the script to
Code:

#!/bin/bash

while read line
do
    echo $line
done < /home/practicas/tp2/nuevo/home/practicas/tp1/passwd

I'll post again about why the execs are not working as intended.

EDIT: Maybe I will not post about why the execs are not working as intended because they are OK (for tidiness you could close 3 after the last line of the script). Are you sure that /home/practicas/tp2/nuevo/home/practicas/tp1/passwd exists? What happens if you change AR=/home/practicas/tp2/nuevo/home/practicas/tp1/passwd to AR=/etc/passwd?

Yes, it does exist because i created it.. copying and thinks like redirecting from other files.. those were the firsts excercises

grail 05-15-2010 07:30 AM

ok ... so what doe it not do or error message does it give when you run it?


All times are GMT -5. The time now is 01:34 PM.