Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-17-2005, 01:20 PM
|
#1
|
|
Member
Registered: Sep 2005
Location: Vienna, Austria
Distribution: Mint 13
Posts: 518
Rep:
|
shell script that read each line separatly
Hi,
I would like to write a shell script that reads a text-file, reading out each line separatly in order to use each line as argument.
The Problem is that I do not know how to read each line separatly.
My file has user's names:
bob
john
chris
.....
and I would like to write a script that reads the 1st line and uses bob as argument, then the 2nd line and uses it as argument for the same command, and so on.
I would really appreciate any help.
Christophe
|
|
|
|
09-17-2005, 01:43 PM
|
#2
|
|
Senior Member
Registered: May 2005
Location: boston, usa
Distribution: fc-12/ fc-11-live-usb/ aix
Posts: 2,673
|
what have you tried, what are you specifically stuck on...
Code:
post code between code tags
|
|
|
|
09-17-2005, 01:45 PM
|
#3
|
|
Moderator
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047
Rep:
|
Try this:
Code:
#!/bin/bash
# Set the field seperator to a newline
IFS="
"
# Loop through the file
for line in `cat file.txt`;do
# Echo the line (echo could be changed to whatever command you want)
echo $line
done
|
|
|
|
09-17-2005, 01:50 PM
|
#4
|
|
Member
Registered: Sep 2005
Location: Vienna, Austria
Distribution: Mint 13
Posts: 518
Original Poster
Rep:
|
the code that I wrote is:
<code>
#!/bin/bash
#file = $1
count= cat $1 | wc -l
myuser=cat $1 | ?????? What should I write here to read each single line separatly and use as argument?
for i in count
do
./another_script mysuser
done
</code>
I don't know how to get just a line at a time (a line beeing a word) to save it into the variable myuser.
|
|
|
|
09-17-2005, 02:06 PM
|
#5
|
|
Member
Registered: Sep 2005
Location: Vienna, Austria
Distribution: Mint 13
Posts: 518
Original Poster
Rep:
|
Great! It works fine!
I wrote the following test-script.
Code:
#!/bin/bash
# Set the field seperator to a newline
IFS="
"
# Loop through the file
for line in `cat file.txt`;do
# Echo the line (echo could be changed to whatever command you want)
echo "$line wird gelöscht" >> text2
echo "$line wurde gelöcht"
done
Last edited by xpucto; 09-17-2005 at 02:09 PM.
|
|
|
|
09-18-2005, 06:08 AM
|
#6
|
|
Member
Registered: May 2005
Posts: 378
Rep:
|
If you're using bash (or ksh) the construct
Code:
for line in $(< file.txt);do
is better as the shell doesn't have to run a separate command to get the file contents.
|
|
|
|
09-20-2005, 08:06 AM
|
#7
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
this doesn't need IFS
and reads the file as needed - doesn't slurp it in as one big line.
Code:
while read line ;do
echo $line
done < infile
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:38 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|