Official Programmers Thread

  • Thread starter Thread starter greatkraw
  • Start date Start date
  • Christian Chat is a moderated online Christian community allowing Christians around the world to fellowship with each other in real time chat via webcam, voice, and text, with the Christian Chat app. You can also start or participate in a Bible-based discussion here in the Christian Chat Forums, where members can also share with each other their own videos, pictures, or favorite Christian music.

    If you are a Christian and need encouragement and fellowship, we're here for you! If you are not a Christian but interested in knowing more about Jesus our Lord, you're also welcome! Want to know what the Bible says, and how you can apply it to your life? Join us!

    To make new Christian friends now around the world, click here to join Christian Chat.
thanks, I've rewritten my file seven times now without getting any nearer :p just trying any random thought I get
 
:p

the basics is easy, but this bit I didn't understand

btw cat will print the contents of a file
read is used to use/ change input
 
tried to figure out how on earth I am supposed to use cat or a for loop in a search

8 count=0
9
10
11 while read name
12
13 do
14 count=$((count + 1))
15 echo $navn
16
17 done < navn-1.txt
18
19 echo " "
20 echo "found $count names"
21 echo " "
___________
6 # v.1.0
7
8 teller=0 #count
9 IFS=$'\n'
10
11 for navn in $(cat navn-1.txt)
12
13 do
14 teller=$((teller + 1))
15 echo $navn
16
17 done
18
19 echo " "
20 echo "Det var $teller navn tilsammen"
21 echo " "
=======================
I am supposed to use one of those to change another assignment I did where I search a text file for two inputs, then print the lines matching both inputs

but I can't find a way to use it, because whatever argument i put behind cat, the bash shell expects to be a filename (or so it seems)

cat, an acronym for concatenate, lists a file to stdout. When combined with redirection (> or >>), it is commonly used to concatenate files. # Uses of 'cat'cat filename # Lists the file.cat file.1 file.2 file.3 > file.123 # Combines three files into one.

So it is normal, that cat expects a filename!
 
yea, I just used it in the wrong place :p

It work now :D (It took my teacher the same number of tries that I had already attempted to find the error though..)
I wonder how many ways it can be solved....
(I used cat, not sure how to make it work the other way, but now I at least have something that can be handed in)
 
one question though:

17 done < navn-1.txt

I know this one redirects the contents of navn into the loop
but HOW does it do it? done finishes the loop, so how can the loop use the contents when it is directed into the done command? any ideas?
 
one question though:

17 done < navn-1.txt

I know this one redirects the contents of navn into the loop
but HOW does it do it? done finishes the loop, so how can the loop use the contents when it is directed into the done command? any ideas?

>filename redirects the output of scriptname to file filename. Overwrite filename if it already exists.

done is just the end of the loop.
 
I know that, that's why it makes no sense

when I do that, the loop above WILL use the input of the file, how can it be inputed into the end of the loop?
 
I know that, that's why it makes no sense

when I do that, the loop above WILL use the input of the file, how can it be inputed into the end of the loop?

Either you are or I am confused :D

Can you give the full code of the file, and show me what input it uses? I don't see what input there is tu be used...
 
1 #!/bin/bash
2 #
3 # Per Ulrik Arntsen
4 # Høgskolen i Nesna
5 # Liste_demo
6 # v.2.0
7
8 teller=0
9
10
11 while read navn
12
13 do
14 teller=$((teller + 1))
15 echo $navn
16
17 done < navn-1.txt
18
19 echo " "
20 echo "Det var $teller navn tilsammen"
21 echo " "

============INPUT:
Bjørn Bjørnsen, Mosjøen, Informasjonsvitenskap
Bjørn Pedersen, Mo i Rana, Informatikk
Øyvind Østensen, Mo i Rana, Informatikk
Anders Bjærtnes, Mosjøen, Informatikk
Anne Båtvik, Mo i Rana, Informasjonsvitenskap
Ågot Ærtnes, Sandnessjøen, Informasjonsvitenskap
Per-Arne Pedersen, Mørkved, Læreutdanningen
Anders Østensen, Mosjøen, Informatikk

*It loops over the names and counts them
 
============INPUT:
Bjørn Bjørnsen, Mosjøen, Informasjonsvitenskap
Bjørn Pedersen, Mo i Rana, Informatikk
Øyvind Østensen, Mo i Rana, Informatikk
Anders Bjærtnes, Mosjøen, Informatikk
Anne Båtvik, Mo i Rana, Informasjonsvitenskap
Ågot Ærtnes, Sandnessjøen, Informasjonsvitenskap
Per-Arne Pedersen, Mørkved, Læreutdanningen
Anders Østensen, Mosjøen, Informatikk

Good morning,

This comes from the textfile? (navn-1.txt)
 
< FILENAME
# Accept input from a file.

Soooo that seems to mean that:

while read X
...
done < file

automaticly means that the while conditions lasts for each X (word) in the file. While needs a condition that continues or ends at a certain point, and for "read X", it needs a source to "read" something. So while it has something to read, it continues the loop, untill it hasn't got anything left to read (the last word of the file has been read).
 
yes, I understand all of that, what I don't understand is that the intro to bash says it interprets one and one line

so i don't get how it can know, when the loop starts, that the file used at done is the one it should loop through, as I would then assume it did not yet know about it... but maybe that all changes when it is saved as a sh file or something
 
yes, I understand all of that, what I don't understand is that the intro to bash says it interprets one and one line

so i don't get how it can know, when the loop starts, that the file used at done is the one it should loop through, as I would then assume it did not yet know about it... but maybe that all changes when it is saved as a sh file or something

Don't take the too literally. What they mean is that it isn't object oriënted. Sometimes it has to look to another line when interpreting.

See this as one line: While [condition] [code that has to be done if true] done [options]

The [code that has to be done] can exist in severl lines

The [options] (like <filename) belong to the while line, so that makes sense :)
 
Let me know if I'm right or wrong, remember, I'm not a bash specialist ;)