DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 8th June 2011
RJPugh RJPugh is offline
Port Guard
 
Join Date: Jun 2008
Location: Culpeper, VA
Posts: 42
Thanked 0 Times in 0 Posts
Default Learning to script...

Hi folks.

I want to start experimenting with basic shell scripting. I've found several web sites that describe how to do it, and I'll be looking into them in more detail soon. But I want to know which ones you more seasoned BSD users would recommend.

My supply of free time tends to fluctuate, so for now I want to stick with simple stuff. I want to start with scripts that essentially do what the old DOS *.bat files did. That is, execute a set of standard shell commands to complete a common task. I'm not ready for pipes, parameters and other such stuff. I'll get to those later on.

Thanks in advance,

RJPugh
Reply With Quote
  #2   (View Single Post)  
Old 8th June 2011
vermaden's Avatar
vermaden vermaden is online now
Administrator
 
Join Date: Apr 2008
Location: pl_PL.lodz
Posts: 1,052
Thanked 118 Times in 93 Posts
Default

First things first ... always use #! /bin/sh as script interpreter.

As for the guides, check these for start:

http://developer.apple.com/library/m...roduction.html
http://www.grymoire.com/Unix/Sh.html
http://www.unix.com/tips-tutorials/1...arse-file.html
http://www.ibm.com/developerworks/ai...GoodUnixHabits
http://sparky.rice.edu/~hartigan/sed.html
http://sparky.rice.edu/~hartigan/awk.html
http://www.ibm.com/developerworks/li...wk1/index.html
http://www.shell-fu.org/lister.php?top
http://www.commandlinefu.com/

Shell scripting is generally about efficient parsing/altering the output of commands with sed/awk/cut/find/column/grep/... and using PIPES a lot.
__________________
religions, worst damnation of mankind
"If 386BSD had been available when I started on Linux, Linux would probably never had happened." Linus Torvalds

Linux is not UNIX! Face it! It is not an insult. It is fact: GNU is a recursive acronym for “GNU's Not UNIX”.
vermaden's: links resources deviantart spreadbsd
Reply With Quote
  #3   (View Single Post)  
Old 8th June 2011
IdOp's Avatar
IdOp IdOp is offline
Too dumb for a smartphone
 
Join Date: May 2008
Location: twisting on the daemon's fork(2)
Posts: 557
Thanked 14 Times in 13 Posts
Default

Probably this is obvious, but it's time to familiarize with the man page for sh. This won't teach programming per se, but give essential information on the capabilities available to you and the syntax required to use them.

Looking at some shell scripts found on your OS itself can be a good idea, trying to figure out what they're doing ... you'll learn some about the OS at the same time. Though, these scripts can get quite complex so don't overdo it or get discouraged.

Doing common tasks is great due to the motivation behind it and seeing results, making mistakes in simple situations and fixing them. You'll soon find you need to add parameters and build up as you go.
Good luck!
Reply With Quote
  #4   (View Single Post)  
Old 8th June 2011
Oko's Avatar
Oko Oko is offline
Fsck Surgeon
 
Join Date: May 2008
Location: Kosovo, Serbia
Posts: 764
Thanked 36 Times in 32 Posts
Default

The following web-site and pages linked to it contains probably all the information you will ever need about the shell scripting.

http://www.shelldorado.com/links/

Call me and old fashion guy but I would by a book. My favorite is

Classic Shell Scripting by Nelson H.F. Beebe, Arnold Robbins

and if you like me use Korn Shell (pdksh with OpenBSD enhancements also branded by some people removed from OpenBSD project as Mir Korn Shell mksh)

Learning the Korn Shell, First Edition by Bill Rosenblatt (the second edition is worse and done after Bill's death).

I also like Unix Shell Programming, Third Edition by Stephen G. Kochan, Patrick Wood.
You can find on the Internet original Stephen's notes under the name An Introduction to the UNIX shell.

And of course the most important advise as with any programming is that unless you do scripting you are not going to learn it.
Reply With Quote
  #5   (View Single Post)  
Old 9th June 2011
thirdm thirdm is offline
Package Pilot
 
Join Date: May 2009
Posts: 198
Thanked 3 Times in 3 Posts
Default another book recommendation

I don't use sh much other than as an interactive shell (actually I kind of dislike shell script, along with Makefile syntax and its various macro/extension packages), but I thought this was a good book:

_The Unix Programming Environment_ Kernighan and Pike.
Reply With Quote
  #6   (View Single Post)  
Old 9th June 2011
Oko's Avatar
Oko Oko is offline
Fsck Surgeon
 
Join Date: May 2008
Location: Kosovo, Serbia
Posts: 764
Thanked 36 Times in 32 Posts
Default

Quote:
Originally Posted by thirdm View Post
I don't use sh much other than as an interactive shell (actually I kind of dislike shell script, along with Makefile syntax and its various macro/extension packages), but I thought this was a good book:

_The Unix Programming Environment_ Kernighan and Pike.
It is a VERY good introductory book on Unix as a whole but it is too short too cover any particular topic in great depth. However, highly recommended as the first reading for any novice Unix user.

Last edited by Oko; 9th June 2011 at 02:08 AM.
Reply With Quote
  #7   (View Single Post)  
Old 30th September 2011
raindog308 raindog308 is offline
Port Guard
 
Join Date: Sep 2011
Posts: 28
Thanked 0 Times in 0 Posts
Default

I like Chris F.A. Johnson's "Shell Scripting Recipes", though I don't recall at this moment which shell it discusses.

Personally, I would say that shell programming knowledge develops in these stages:

1. A list of commands executed in sequence

2. Same as #1 but verbosely telling the user what's doing and maybe writing to a log file

3. A situation where you might want something to happen, so you learn about if and maybe case

4. You realize you need to consider some input more deeply, so you need to learn basic text processing (cut, sed, maybe simple awk) and variables

5. You decide to make your program somewhat interactive so there's user processing

6. Very soon after, you learn more about error handling because users make mistakes :-)

7. Your programs get bigger and now you're into functions, little libraries, etc. You are using variable scoping, coroutines, arrays, and lots more awk, sed, etc. Maybe you go crazy and start using tput to write interactive curses menus :-)

8. You've outgrown shell and move on to perl, python, ruby, or really move on and start writing C.

I am not entirely serious about #8. I've maintained multi-thousand-line shell scripts and with enough programmer discipline, you can write huge things in shell.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Book(s) for learning C as a first language? guitarscn Programming 10 12th December 2010 02:34 PM
Learning how to program Solaris_Delta Programming 9 24th December 2008 07:58 PM
Learning Content Management System Oko General software and network 0 31st October 2008 04:02 AM
Learning Programming Crypt Programming 35 27th October 2008 04:54 PM
Learning Perl mtx Book reviews 7 22nd October 2008 05:55 PM


All times are GMT. The time now is 11:20 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick