Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-05-2012
Registered User
 
Join Date: May 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
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 10:04 AM.. Reason: code tags please!
Sponsored Links
    #2  
Old 05-05-2012
fpmurphy's Avatar
who?
 
Join Date: Dec 2003
Location: /dev/ph
Posts: 4,429
Thanks: 47
Thanked 358 Times in 332 Posts
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

Sponsored Links
    #3  
Old 05-05-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,350
Thanks: 144
Thanked 1,755 Times in 1,592 Posts
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
Registered User
 
Join Date: May 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
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 10:32 AM..
Sponsored Links
    #5  
Old 05-05-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,350
Thanks: 144
Thanked 1,755 Times in 1,592 Posts
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

Sponsored Links
    #6  
Old 05-05-2012
Registered User
 
Join Date: May 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
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

To me it seems the output should be

Code:
$./test5
Visit beautiful states

Sponsored Links
    #7  
Old 05-05-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,350
Thanks: 144
Thanked 1,755 Times in 1,592 Posts
Just the string states
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Typo in man file?? steadyonabix Solaris 5 10-30-2009 06:41 PM
BASH Command Line Typo -- Can't Get Out Viola Shell Programming and Scripting 1 05-23-2008 07:05 PM
HP-UX CSA book yoman HP-UX 1 03-04-2005 06:12 AM



All times are GMT -4. The time now is 11:06 AM.