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?
# 15  
Old 05-05-2012
You're welcome and don't be embarrassed, this is what these forums are for, plus by asking these questions, and really probing until you understand, you'll end up gaining more insight, than those who are satisfied with merely knowing "how" something is done. Have fun...
# 16  
Old 05-05-2012
you maybe missed this part:
Code:
for state in ‘cat $file’

edit: little late with the answer... note to myself: don't watch DVD while helping in the forums Smilie
# 17  
Old 05-05-2012
I saw it honestly, but I didn't follow the logic through properly.

I feel this was a good lesson to learn early on. I should have written out the expansion on paper like Scrutinizer did before I asked the question!

Something like `cat $file` ---> `cat states` ----> Alabama Alaska ......

As soon as I saw him write that it hit me.
# 18  
Old 05-05-2012
Much more readable code is produced if you use $(whatever)

Of course, that's just my opinion.

Regards.
# 19  
Old 05-05-2012
The for syntax is weak:
Code:
$ cat states
Alabama
Alaska
New Mexico
$ cat test5
file="states"
for state in `cat $file`
do
  echo "Visit beautiful $state"
done
$ ./test5
Visit beautiful Alabama
Visit beautiful Alaska
Visit beautiful New
Visit beautiful Mexico
$ cat test6
file="states"
while read state
do
  echo "Visit beautiful $state"
done < $file
$ ./test6
Visit beautiful Alabama
Visit beautiful Alaska
Visit beautiful New Mexico
$

# 20  
Old 05-05-2012
It is not necessarily for that reason that the for syntax is weak, though
Code:
oldIFS=$IFS IFS="
"
for state in $(cat "$file")
do
  echo "Visit beautiful $state"
done
IFS=$oldIFS

Code:
Visit beautiful  Alabama
Visit beautiful  Alaska
Visit beautiful  New Mexico

# 21  
Old 05-05-2012
There are indeed many ways to skin a cat Smilie.
You can save one that way:
Code:
for state in $(cat "$file")

->
Code:
for state in $(<"$file")

 
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