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?
# 1  
Old 05-05-2012
Is this a typo in my Book?

I'm currently learning Bash scripting from a book and the following example is given, but I think there is a typo in the variable assignment for the variable file.

Code:
$ cat test5
 #!/bin/bash
 # reading values from a file
 
 file=“states”    ( shouldn't this be file=`cat states` assuming you have a 
                        text file with the list , doesn't this just assign the single 
                        string states? )
 for state in ‘cat $file’
 do
 echo “Visit beautiful $state”
 done
 $ cat states
 Alabama
 Alaska
 Arizona
 Arkansas
 Colorado
 Connecticut
 Delaware
 Florida
 Georgia
 $ ./test5
 Visit beautiful Alabama
 Visit beautiful Alaska
 Visit beautiful Arizona
 Visit beautiful Arkansas
 Visit beautiful Colorado
 Visit beautiful Connecticut
 Visit beautiful Delaware
 Visit beautiful Florida
 Visit beautiful Georgia
 $

Am I missing something here?

Last edited by fpmurphy; 05-05-2012 at 11:04 AM.. Reason: code tags please!
# 2  
Old 05-05-2012
No, the book is right.
Code:
file=“states”

A another way go get the same output is:
Code:
file=“states”    
while read state
do
     echo “Visit beautiful $state”
done < $file

# 3  
Old 05-05-2012
No quotes are needed in this case: file=states would suffice, but they won't hurt if they are the right double quotes, which is not the case here “” vs. ""
# 4  
Old 05-05-2012
Thank you for the quick replies.

Why does
Code:
file="states"

not assign the single string states as the value for the variable like usual?

I tried testing this on the command line and the value assigned to $file is indeed the word states, not the contents of the states file.

Last edited by Riker1204; 05-05-2012 at 11:32 AM..
# 5  
Old 05-05-2012
You are using are the wrong kind of double quotes. Either you copied it from somewhere instead of typing it at the keyboard, or typed it in Microsoft Word or something.
Code:
$ file=“states”      # these are the wrong quotes
$ echo "$file"
“states”
$ file="states"      # these are the right quotes
$ echo "$file"
states

# 6  
Old 05-05-2012
So if you have a file named states and you type the following:
Code:
file=states

The contents of the states file is assigned to $file? or just the string states?

Sorry, very confused Smilie

To me it seems the output should be
Code:
$./test5
Visit beautiful states

# 7  
Old 05-05-2012
Just the string states
 
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