Is this a typo in my Book?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Is this a typo in my Book?
# 8  
Old 05-05-2012
Quote:
Originally Posted by Scrutinizer
Just the string states
Since the value of $file is states why isn't the output simply:

Visit beautiful states
# 9  
Old 05-05-2012
from
Code:
done < "$file"

This means the the input from the file whose name is in the variable $file gets fed into the loop as stdin, so that read can process it and put the content of a line in variable state at every iteration..
# 10  
Old 05-05-2012
Ok, but the original example does not contain that.

So how is it working?

is the variable name file special or would this work with any named variable?
# 11  
Old 05-05-2012
The variable is just a variable.

In the example it says
Code:
for state in ‘cat $file’

which is not good practice in my opinion (what text book is this?), and again it has the wrong kind of quotes. It should read
Code:
for state in `cat $file`

Here cat extract the contents and "for" will go over them one by one.

This will work for the given sample, but only because the author cleverly left out a state like New Jersey. Watch what happens when you add that to the file...
There are ways to get around that, but fpmurphy's approach is much preferred, and is best practice.
# 12  
Old 05-05-2012
Thank you for the reply. Smilie

The quotation issue is because I copy and pasted, please forgive that issue.Smilie

I have a better way to ask my question!

How does the command for know to extract the file named in the variable and not the string itself? I know its doing that, but just curious WHY and HOW Smilie If you just test `cat $file` you indeed get states Smilie so I'm assuming your reference to the "done" tells the command to extract the file if it exists?

The book is Linux Command Line and Scripting Bible by Richard Blum.

Indeed the next section does cover the issue of New Jersey and introduces the while command and the IFS variable but I have not gotten to that section yet! I hate to move forward not understanding this example in detail...

Last edited by Riker1204; 05-05-2012 at 12:49 PM..
# 13  
Old 05-05-2012
The command for doesn't "know". The shell first expands the variable $file so that it becomes:
Code:
for state in `cat states`

and then `cat states` gets expanded, so that the command becomes:
Code:
for state in Alabama Alaska Arizona Arkansas Colorado Connecticut Delaware Florida Georgia New Jersey
do

So now you know why this construction would not go well for New Jersey
# 14  
Old 05-05-2012
Oh my GOD!

How did I miss that Smilie

I am extremely embarrassed seriously.

Thank you for your patience.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Programming

Which C++ book do you recommend?

Hello, May be my post may look naive to many...but it isn't if you were to believe me.After, trying to learn programming in C for at least 5 years , I concluded that K & R is the best book to learn C, alas I took a lot of time to realize this. So, I'm going to start learning how to program... (8 Replies)
Discussion started by: blackwhite
8 Replies

2. Shell Programming and Scripting

Typo in sample script from book?

Hello, I'm new to this forum, and I apologize in advance if I did something wrong here. I am pretty stumped here as I am still getting the error message, "./comc1.sh: test: argument expected." after executing the script itself. Here's the script file I modified: I tried executing line 4... (1 Reply)
Discussion started by: ConcealedKnight
1 Replies

3. Solaris

Typo in man file??

Hi is it me or is there a typo in the Sun Solaris man page for ps. I thought the option for listing a process by PID was just -p but on the clearcase server at work it reads: - -ps proclist Lists only process data whose process ID numbers are given in proclist.... (5 Replies)
Discussion started by: steadyonabix
5 Replies

4. Shell Programming and Scripting

BASH Command Line Typo -- Can't Get Out

Hi Sorry to post a bit of an "I'm an idiot" post, but I did a typing error and typed "ls 'l" instead of "ls -l". This seems to have made me enter some sort of mode or other that I can't seem to be able to get out of. The new line prompt is > (if that helps). Sorry for posting such a... (1 Reply)
Discussion started by: Viola
1 Replies

5. Solaris

e-book

Hi everybody I a new one And I have just wanted to research on Sun Solaris So can you help me what e-book to read ( and if can you give me the direct address to load ) Thks so much (3 Replies)
Discussion started by: iwbasts
3 Replies
Login or Register to Ask a Question