|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Just the string states
|
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|