Simple read line_ function hangs indefinitely
I would like to use the function read line{line number here} in linux bash programming language. Unfortunately, today that function does not work. I would like to read commented out lines 6-10. My attempt below:
Code:
#!/bin/bash What am I doing wrong? |
Ok, so first off, use / and not \ in the closing code tag to make them work :)
As for your example, you do realise that the read command will wait until you enter something before moving to the next line of the script? So we can ignore all but the first read: Code:
read -r line6 |
questions formated to 'human readable' form important
Quote:
|
Yes, and since this is a homework assignment, get your teacher involved and keep him there. That's what he's there for.
Probably the most important skill in programming is to learn how to see what a program is (not) doing, and to devise a way to figure it out. You can short-change yourself of that if you simply throw your hands up, post out to a forum, and ask someone else to give you the answer. A much better strategy would be to ask Mr. Cooper to give you a nudge in the right direction. Don't have any reluctance to do that! |
something strange going on
Quote:
I gained this false notion from when Example 15-4. What happens when read has no variable uses the code block: Code:
{ # Code block. Looking at this practice exercise I still really think something special exists about "line2", more specifically the line "read line2" above. If you look at this entire program you will see that the variable "line2" was never set. |
Actually I have no teacher
Quote:
Code:
#Student="Andrew Comly" Teacher: Mendell Cooper ___________________________________ Quote:
Code:
{ # Code block. Code:
{ # Code block. |
In the example it is set by reading from the input of $0 and seeing that $0 refers to the script itself it is now reading lines from itself. I can assure you that those variables could have just as easily been
foo and bar and bar would contain the same data as line2. Of course you can try this and also change the digit to be say line10 but you will still only get the second line of the file. |
Solution
I GOT IT!!!!!
If you want the code block to read line "N" of the script $0, than the "read" command must be executed "N-1" times before line "N" is read. E.g. Code:
{ # Code block. Now I understand how to echo the choice line of the script $0. Thanks a-lot grail!! |
Original example program correction
Correction of original program below:
Code:
#!/bin/bash Code:
$./Ch15_IntCmd_04ReadLine.sh |
Alternative:
Code:
#!/usr/bin/env bash |
PEDAGOGICAL SYNOPSIS for grail's alternative method
Quote:
At first I did not understand Code:
read _ word Read a line starting with the Nth word syntax: $ read _(write " _" N times) word(N+1) Examples 1) Read a line skipping the first 5 words. Code:
$ read _ _ _ _ _ word2 Code:
$ read word1 word2 word3 word4 _ phrase Then I did not quite get Code:
(( i < 5 )) && read || read _ word Code:
(( i < 5 )) && read || read _ word Code:
(( condition1 )) && command1 || commandA Finally I also had some problems with Code:
[[ "$out" ]] && out="$out $word" || out=$word Code:
(( condition1 )) && command1 || commandA Code:
out="$out $word" || out=$word Code:
if [[ "${out}" ]]; then |
Your findings are correct and here is the same in more layman terms:
1. Variable names may start with [a-zA-Z_], so our _ could be replaced by a simple 'a', but the generally practised rule is that _ is treated as what you wish to throw away 2. It is also important to remember that read uses the IFS variable to advise the delimiter for information, so when multiple variables are provided, read will separate using the IFS information and if there are less variables than items on the line read, all remaining items are placed in the last variable 3. (()) is specifically for arithmetic in bash, either testing or setting and calculating 4. [[ $var ]] is the same as [[ -n $var ]], which of course is your findings that if the variable is empty / null then the statement is true Here is an additional site that i think is a must have to slowly work through after completing ABS |
All times are GMT -5. The time now is 11:29 PM. |