Computing.Net > Forums > Unix > KSH script

KSH script

Reply to Message Icon

Original Message
Name: bergerbytes
Date: April 9, 2005 at 06:20:00 Pacific
Subject: KSH script
OS: n/a
CPU/Ram: n/a
Comment:


PLEASE HELP me!!
Anyone know what this line means?

for I in `cat /etc/mnttab |grep ufs | cut-f2`

Thank you muchly


Report Offensive Message For Removal


Response Number 1
Name: Jim Boothe
Date: April 9, 2005 at 06:55:04 Pacific
Reply: (edit)

A for-loop is executed once for each word in its argument list, and that word is placed in the specified variable.

Here is a simple example that should make it clear.  Run this example for yourself and you will see the results (shown below in green).

for i in this is a test
do
echo "Processing word: $i"
done

Processing word: this
Processing word: is
Processing word: a
Processing word: test


I will follow up with an explanation of your specific for command.


Report Offensive Follow Up For Removal

Response Number 2
Name: Jim Boothe
Date: April 9, 2005 at 07:24:38 Pacific
Reply: (edit)

for I in `cat /etc/mnttab |grep ufs | cut-f2`

The main line of your for-loop is executing a nested command (in this case it is a series of piped commands) to constuct the list of words to be processed.  It may have just been a typo when you posted, but the cut-f2 needs to be two words like cut -f2.

The easiest way to see the list of words being constructed is to just run the nested command by itself.  And for a deeper understanding, take it one step at a time.  First run this:

cat /etc/mnttab

On my system that displays 9 lines.  Next do this:

cat /etc/mnttab | grep ufs

The output of the cat command (my 9 lines) is piped into the grep command which will filter it down to only those lines containing the string ufs.

And finally, do this:

cat /etc/mnttab | grep ufs | cut -f2

The output of cat command is filtered by the grep command, then those selected lines are sent (piped into) the cut command.

The cut command selects (cuts out) parts of each line it processes.  When using the -f option, it selects specified fields based on a defined field delimiter. If no delimiter is specified such as in this case, it selects the entire line. Thus, the cut command is not having an impact here, the way I see it.

If you want to cut out field 2 from a space-delimited line, you need to pipe your lines into:

    cut -d" " -f2

But be warned that the cut command will consider EACH space as a field delimiter, so if you have several spaces in a row, that is defining many null fields.  If you do not want to worry about how many spaces are between each of your fields, then pipe your lines into:

    awk {print $2}


Report Offensive Follow Up For Removal

Response Number 3
Name: WilliamRobertson
Date: April 9, 2005 at 10:45:12 Pacific
Reply: (edit)

It also means that the original author did not realise that you can give grep a file argument and it will search that file - the "cat" part is redundant and inefficient (not that it will probably make a noticeable difference, but it bugs me all the same when I see this).

They were also not aware that ksh has its own "$(commands here)" syntax which is an improvement on the old Bourne shell backticks (it nests better for one thing), although it supports them for backward compatibility. I can never understand why people put "#!/bin/ksh" at the top and then proceed to write a Bourne shell script.

Also, grep + cut = awk. The one thing anyone should learn how to do in awk, even if they never use any other features, is how to filter for a pattern (equivalent to grep) and return a numbered field value (equivalent to cut):

awk '/pattern/ {print $2}' filename.ext

It is also a convention in shell scripting that uppercase is reserved for exported variables (like PATH, HOME etc) or sometimes constants, so the fact that the loop index is "I" tells us that the original author was not aware of that either.



Report Offensive Follow Up For Removal

Response Number 4
Name: bergerbytes
Date: April 9, 2005 at 12:34:55 Pacific
Reply: (edit)

Wow you guys are great!! I can't wait to know half as much...thank you so much, I greatly appreciate it. I will now go and study what you have written.

Thanks again :)


Report Offensive Follow Up For Removal

Response Number 5
Name: bergerbytes
Date: April 9, 2005 at 12:44:46 Pacific
Reply: (edit)

Hi again,

Do either of you (Mr. Jim Boothe or Mr.WilliamRobertson) or anyone know of a few really good books for sh or ksh shell scripting? Books that will give me a good understanding of how all the syntax's work, would be excellent!

Thank you,
greatly appreciated :)


Report Offensive Follow Up For Removal


Response Number 6
Name: Jim Boothe
Date: April 9, 2005 at 12:49:47 Pacific
Reply: (edit)

And just for the record, I wouldn't mind knowing half as much as Mr. WilliamRobertson.


Report Offensive Follow Up For Removal

Response Number 7
Name: David Perry
Date: April 11, 2005 at 06:49:46 Pacific
Reply: (edit)

I second Jim.
Ask Mr. Robertson about dates sometime.

Have a look at the book
"Learning the Korn Shell" O'Reilly
http://www.oreilly.com/catalog/korn2/index.html


Report Offensive Follow Up For Removal

Response Number 8
Name: WilliamRobertson
Date: April 12, 2005 at 16:56:54 Pacific
Reply: (edit)

Well, I second you guys :-)

The O'Reilly Korn Shell book is THE shell programming book. They also do a "sed & awk" book which I've had for ages but only ever dipped into for some reason.

Their "vi" book is worth reading cover to cover as well (and if you like vi you'll love Vim).

"A Quarter Century of Unix" by Peter Salus (ISBN 0201547775) is over ten years old now but it's a great read about the guys with beards who invented Unix and C in the seventies.


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: KSH script

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge