New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus • Advice and insight from world-class Apple enthusiasts • Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Bash shell scripting in OS X


Post Reply New Thread Subscribe

 
Thread Tools
V6Pony

 
V6Pony's Avatar
 
Member Since: Oct 30, 2010
Location: Southern Illinois
Posts: 271
V6Pony has a little shameless behaviour in the past
Mac Specs: MBP 15" I7 first one and loving it.

V6Pony is offline
I'm wanting to learn bash scripting for OS X. Could someone recommend a good book or resources I could use?

I'm also interested in command line in terminal. I know Unix OS X commands are somewhat different than Linux or Unix. Also would like any resources for learning these commands.

Thanks
QUOTE Thanks
vansmith

 
vansmith's Avatar
 
Member Since: Oct 19, 2008
Location: Toronto
Posts: 18,203
vansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond repute
Mac Specs: 2012 13" MBP (2.5 i5, 8GB)

vansmith is offline
The Linux Documentation Project's Beginner Guide and Advanced Guide would be a good start. Although the beginner one is nearly 3.5 years old, it should still do the job (and worst case you Google the difference/problem).

Important Links: Community Guidelines : Use the reputation system if you've been helped.
M-F Blog :: Write for the blog
Writing a Quality Post
QUOTE Thanks
Dysfunction

 
Dysfunction's Avatar
 
Member Since: Mar 17, 2008
Location: Tucson, AZ
Posts: 6,638
Dysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant future
Mac Specs: 2008 and 2011 15" mbps, late 11 iMac, iPhone 4s, and too many ipods and other stuff

Dysfunction is offline
Quote:
Originally Posted by vansmith View Post
The Linux Documentation Project's Beginner Guide and Advanced Guide would be a good start. Although the beginner one is nearly 3.5 years old, it should still do the job (and worst case you Google the difference/problem).
yea, not much has really changed significantly in those 3.5 years.

mike
This machine kills fascists
Got # ? phear the command line!
QUOTE Thanks
V6Pony

 
V6Pony's Avatar
 
Member Since: Oct 30, 2010
Location: Southern Illinois
Posts: 271
V6Pony has a little shameless behaviour in the past
Mac Specs: MBP 15" I7 first one and loving it.

V6Pony is offline
Thanks looks like a good starting point.
QUOTE Thanks
scathe

 
scathe's Avatar
 
Member Since: Jan 12, 2009
Location: Prague, Czech Republic
Posts: 1,096
scathe is a jewel in the roughscathe is a jewel in the rough
Mac Specs: 2,4Ghz 15" unibody

scathe is offline
Mac OS has (from what I can tell) a standard bash shell, it's rare that I run into differences. So any bash scripting book as suggested above is great. Basically it's bash with some additional OSX delivered programs - e.g. changing defaults or ... ooooh, "say" :-D

If you want to have a look at something specific to Mac OS, have a look at AppleScript, which can be amusing :-D

Well and do what the rest of us do: need to accomplish a task? google the script someone else has already used and posted in a forum, on their blog and such ;-)
QUOTE Thanks
IvanLasston

 
IvanLasston's Avatar
 
Member Since: Feb 26, 2010
Location: Rocky Mountain High, Colorado
Posts: 2,116
IvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to behold
Mac Specs: 1.8 GHz i7 MBA 11" OSX 10.8.2

IvanLasston is offline
Shell Scripting Primer
https://developer.apple.com/library/...268-TP40003516

How to use Manpages
https://developer.apple.com/library/...ence/ManPages/

The Apple Manpages
https://developer.apple.com/library/...man/5/manpages

Install Command line tools from xcode for development
Xcode -> Xcode -> Preferences -> Downloads -> Command Line Tools -> Install

Shell Here Opens a shell in the current Finder Folder
Download ShellHere 1.0.5 Free - Open a new Terminal window from Finder - Softpedia

My setup for .profile
Code:
alias ls='ls -GF'
alias ll='ls -hGlF'
export PS1='\e[0;31m\w\e[m\n\T (\! \j) > '
#export CLICOLOR='1'
#ansi colors for ls
# a     black
# b     red
# c     green
# d     brown
# e     blue
# f     magenta
# g     cyan
# h     light grey
# A     bold black, usually shows up as dark grey
# B     bold red
# C     bold green
# D     bold brown, usually shows up as yellow
# E     bold blue
# F     bold magenta
# G     bold cyan
# H     bold light grey; looks like bright white
# x     default foreground or background
#order of LSCOLORS
# 1.   directory
# 2.   symbolic link
# 3.   socket
# 4.   pipe
# 5.   executable
# 6.   block special
# 7.   character special
# 8.   executable with setuid bit set
# 9.   executable with setgid bit set
# 10.  directory writable to others, with sticky bit
# 11.  directory writable to others, without sticky bit
#                1 2 3 4 5 6 7 8 9 1011
export LSCOLORS='Hxfxcxdxbxegedabagacad'
Books
Probably the best condensed reference for everything unix
http://www.amazon.com/Nutshell-Fourt.../dp/0596100299

Bash Shell Book
Amazon.com: Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly)) (9780596009656): Cameron Newham: Books

The best regular expressions book
http://www.amazon.com/Mastering-Regu...ar+expressions
QUOTE Thanks
vansmith

 
vansmith's Avatar
 
Member Since: Oct 19, 2008
Location: Toronto
Posts: 18,203
vansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond repute
Mac Specs: 2012 13" MBP (2.5 i5, 8GB)

vansmith is offline
Note that although IvanLasston's .profile is quite elaborate and might prove useful at some point, it's not necessary for the purposes of scripting.

Important Links: Community Guidelines : Use the reputation system if you've been helped.
M-F Blog :: Write for the blog
Writing a Quality Post
QUOTE Thanks
IvanLasston

 
IvanLasston's Avatar
 
Member Since: Feb 26, 2010
Location: Rocky Mountain High, Colorado
Posts: 2,116
IvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to behold
Mac Specs: 1.8 GHz i7 MBA 11" OSX 10.8.2

IvanLasston is offline
The OP mentioned
Quote:
I'm also interested in command line in terminal.
So I posted my .profile - as the dircolor command is quite different from Linux/Unix to Mac.
QUOTE Thanks
V6Pony

 
V6Pony's Avatar
 
Member Since: Oct 30, 2010
Location: Southern Illinois
Posts: 271
V6Pony has a little shameless behaviour in the past
Mac Specs: MBP 15" I7 first one and loving it.

V6Pony is offline
I'm finding it difficult to learn from the included links. As below

set VALUE ="Four"

echo $VALUE
# or...
setenv VALUE "Four"

echo "$VALUE score and seven years ago...."

This is from Shell Scripting Primer listed above. It is wrong as best as I can tell. For the bash shell it should be.

VALUE="Four"
echo $VALUE

I just keep running into tutorials that are not right. I spend most of my time trying to figure out what is wrong. Not sure if it is just the different commands in OS X command line or what. Anyone offer any guidance to me on this issue?
QUOTE Thanks
vansmith

 
vansmith's Avatar
 
Member Since: Oct 19, 2008
Location: Toronto
Posts: 18,203
vansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond repute
Mac Specs: 2012 13" MBP (2.5 i5, 8GB)

vansmith is offline
Quote:
Originally Posted by IvanLasston View Post
So I posted my .profile - as the dircolor command is quite different from Linux/Unix to Mac.
Would that not be shell dependent and not OS dependent?

Quote:
Originally Posted by V6Pony View Post
I just keep running into tutorials that are not right. I spend most of my time trying to figure out what is wrong. Not sure if it is just the different commands in OS X command line or what. Anyone offer any guidance to me on this issue?
You need to make sure you read Bash tutorials. There are multiple "shells" available to Unix and Unix-like operating systems and they each have their own idiosyncrasies. I've also noticed that it is very unforgiving with syntax errors.

Important Links: Community Guidelines : Use the reputation system if you've been helped.
M-F Blog :: Write for the blog
Writing a Quality Post
QUOTE Thanks
IvanLasston

 
IvanLasston's Avatar
 
Member Since: Feb 26, 2010
Location: Rocky Mountain High, Colorado
Posts: 2,116
IvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to behold
Mac Specs: 1.8 GHz i7 MBA 11" OSX 10.8.2

IvanLasston is offline
Quote:
Would that not be shell dependent and not OS dependent?
The profile itself is bash - so yes if you are using a csh or variant you need to adjust accordingly. The dircolor is the part that is really different from other OS variants.

@V6Pony
The example you are giving is csh. Generally - you'll have to pick a shell and stick with it when you are first learning. From the command line

Code:
echo $SHELL
It should be bash - so anything csh will not work. You'll need to follow the bash examples.
QUOTE Thanks
V6Pony

 
V6Pony's Avatar
 
Member Since: Oct 30, 2010
Location: Southern Illinois
Posts: 271
V6Pony has a little shameless behaviour in the past
Mac Specs: MBP 15" I7 first one and loving it.

V6Pony is offline
ivanlasston I see. I thought that was a bash tutorial. It seems also that Darwin Unix bash "Apple" is different than linux bash and so on. It's really hard to find a tutorial or book where the commands are correct. Where the commands work in apple terminal.
QUOTE Thanks
scathe

 
scathe's Avatar
 
Member Since: Jan 12, 2009
Location: Prague, Czech Republic
Posts: 1,096
scathe is a jewel in the roughscathe is a jewel in the rough
Mac Specs: 2,4Ghz 15" unibody

scathe is offline
Quote:
Originally Posted by V6Pony View Post
ivanlasston I see. I thought that was a bash tutorial. It seems also that Darwin Unix bash "Apple" is different than linux bash and so on. It's really hard to find a tutorial or book where the commands are correct. Where the commands work in apple terminal.
From what I can tell the OSX-delivered bash is the same as on Linux.

If you follow bash tutorials that were meant for various linux distributions, it will work as far as scripting or file management is concerned :-) But it can get a bit confusing from the beginning to get your head wrapped around the different shells, as recommended above, stick with bash - it's something like a standard or a default shell on most systems.

Just skip any tutorials and examples concerned with configuring the system for now (I mean stuff like setting the network, displays, devices ...)
QUOTE Thanks
vansmith

 
vansmith's Avatar
 
Member Since: Oct 19, 2008
Location: Toronto
Posts: 18,203
vansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond repute
Mac Specs: 2012 13" MBP (2.5 i5, 8GB)

vansmith is offline
Quote:
Originally Posted by IvanLasston View Post
The dircolor is the part that is really different from other OS variants.
Ah, didn't know that! Thanks.

Quote:
Originally Posted by V6Pony View Post
ivanlasston I see. I thought that was a bash tutorial. It seems also that Darwin Unix bash "Apple" is different than linux bash and so on. It's really hard to find a tutorial or book where the commands are correct. Where the commands work in apple terminal.
Shell scripting is fairly uniform across platforms. The major differences comes in to the tools you use during the actual scripting (aside from, as IvanLasston noted above, some minor differences). Care to post an example of something that is causing you some issues?

This is why I like scripting languages - everything works like a charm regardless of the shell or OS.

Important Links: Community Guidelines : Use the reputation system if you've been helped.
M-F Blog :: Write for the blog
Writing a Quality Post
QUOTE Thanks
IvanLasston

 
IvanLasston's Avatar
 
Member Since: Feb 26, 2010
Location: Rocky Mountain High, Colorado
Posts: 2,116
IvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to beholdIvanLasston is a splendid one to behold
Mac Specs: 1.8 GHz i7 MBA 11" OSX 10.8.2

IvanLasston is offline
The script is usually the same - where I've run into problems is in the options available between the different tools - as you said vansmith. What I tend to do is google both the linux and osx commands. So I google
Code:
man ls mac
man ls
man find
man find mac
All in different windows of course. The man/mac google should drop you into the developer man pages that I linked to before. The straight man - should find you a standard linux man page. You'll see that some of the built in commands do not match up in arguments. So if you do something like ls with options and dump that into a file to manipulate - you may get an error saying that option doesn't exist. (this is also why dircolor may bite you) Just an example

find command
https://developer.apple.com/library/...n1/find.1.html
UNIX man pages : find ()

ls command
Loading…
UNIX man pages : ls ()
QUOTE Thanks

Post Reply New Thread Subscribe


« Open a GUI app as root command line? | Now that is cool! »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Mapping AFP with Applescript for student macs DrunkMunki OS X - Operating System 3 11-04-2011 03:23 AM
Simple bash scripting question svenyonson OS X - Operating System 1 04-30-2009 10:18 PM
noob shell scripting question mikeb OS X - Development and Darwin 2 03-22-2009 01:36 PM
help with basic shell scripting jrcal OS X - Development and Darwin 0 06-11-2007 01:08 PM
Shell scripting badmuthahubbard OS X - Development and Darwin 1 09-13-2006 05:16 AM

All times are GMT -4. The time now is 02:25 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?