LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-13-2010, 04:52 PM   #1
giantjavi
LQ Newbie
 
Registered: May 2010
Posts: 4

Rep: Reputation: Disabled
Question 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
 
Old 05-13-2010, 07:33 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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

Last edited by chrism01; 05-13-2010 at 07:34 PM.
 
Old 05-13-2010, 09:35 PM   #3
giantjavi
LQ Newbie
 
Registered: May 2010
Posts: 4

Original Poster
Rep: Reputation: Disabled
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
 
Old 05-13-2010, 10:56 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,253

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
So what seems to be the problem?
Based on what you have so far it seems to already be solved??
 
Old 05-13-2010, 11:54 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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....
 
Old 05-14-2010, 02:06 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by giantjavi View Post
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.
 
Old 05-14-2010, 03:08 AM   #7
giantjavi
LQ Newbie
 
Registered: May 2010
Posts: 4

Original Poster
Rep: Reputation: Disabled
#!/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?
 
Old 05-14-2010, 03:15 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by giantjavi View Post
#!/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?

Last edited by catkin; 05-14-2010 at 03:25 AM.
 
Old 05-15-2010, 08:26 AM   #9
giantjavi
LQ Newbie
 
Registered: May 2010
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by catkin View Post
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
 
Old 05-15-2010, 08:30 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,253

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 07:07 PM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 02:43 PM
Script to read line by line from a file kushalkoolwal Programming 20 01-27-2006 05:17 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 11:24 PM


All times are GMT -5. The time now is 11:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration