TweetFollow Us on Twitter

Back to the Shell

Volume Number: 22 (2006)
Issue Number: 9
Column Tag: Mac in the Shell

Back to the Shell

Revisiting the basics in an advanced kind of way.

by Edward Marczak

Introduction

Recently, I've gotten a number of people contacting me asking if I could write some more about "using the shell". Well, for close to two years, that's just about all this column talks about! But, that means it's time for me to get back to basics. For the long-time reader, though, I'll get into some command-line goodness that I haven't tread upon quite yet.

The Shell

When you run Terminal.app, that's your gateway to a shell: the text-based command interpreter. This is similar to a DOS shell or MSH under Windows, an xterm in X, or, a dedicated hardware terminal that is serially patched into a host. This is the original CRT interface into a system (props to punch cards and paper-based teletypes).

Some people also call a GUI interface a "shell". While that's probably partially correct, for the purposes of this column, "shell" will always refer to the text-based, command-line driven variety. Nor will it refer to file manipulation shells like Midnight Commander, et al.

Different shells have arisen over the years, with the Bourne Shell, or "sh" being the original Unix shell. The C-Shell, or "csh", became a popular alternative. Finally, "bash", or, the "Bourne Again Shell" added many features to the original sh, and is now the default shell in OS X (starting with 10.3 - 10.2 and earlier used csh as the default). When I say "default", I mean just that: it's a nice gesture that the OS chooses something for you, but you can choose any shell you like as your default. A user's shell is stored in their user record, either in NetInfo or OpenDirectory.

That's the abridged (re)introduction: A shell is a user interface that accepts input, processes that input, and produces output. For a much deeper introduction, please refer to my March 2005 column in MacTech. (Available now, by the way as part of the MacTech CD - <http://www.mactech.com/cd>).

Father's Day

Father's Day, 2006: Not only am I a father, but I happened to be at my Father's house. Of course, I'm also a consultant, and holidays don't stop clients from calling when there is a problem! So, when I got a "but I'm sure it will only take you a minute" call during the day, I figured I'd make a client happy. But I didn't have my laptop, or any OS X box for that matter. I have to admit that my Father is a Windows guy, so I had access to his machine. There are plenty of ssh clients for Windows (with PuTTY being my favorite <http://www.chiark.greenend.org.uk/~sgtatham/putty/>), and OS X Server has ssh enabled by default. Feel free to enable it in OS X by opening the Sharing Pref Pane and placing a check mark next to "Remote Login". ssh, if you are unfamiliar with it, is the Secure Shell. Now, it's not really a shell in and of itself, but a way to access a remote shell - typically one on another machine. It's 'secure' because all traffic between the ssh client and the ssh server is encrypted. The moral of this tale turns out to be the 'why' section of this article. "Why should I use a text-based interface when I have yummy Aqua?" Let me (briefly) count the ways:

    1. Power: Quickly find and affect a huge number of surgically selected files.

    2. Power: Many times, in many ways, it's the GUI that's still catching up with the shell. There are options in many of the shell tools that just can't be performed with the GUI.

    3. Power: think about the reach that a tool like ssh gives you to access, install, troubleshoot and diagnose remote machines. (I never had to leave my Father's house that day).

Shell Lite

As it turns out, it only did take me a minute with that problem: Windows services just got a little funky on an OS X Server machine. smbd was running, but nmbd had died off. This meant OS X machines could, in tests, access the server using smb://ip.ad.dr.ess, but genuine Windows machines couldn't browse for shares. So, I stopped smb, and started it up again - problem solved. How'd I do that without the GUI-based Server Admin? Easy: the shell-based serveradmin.

I detest opening a console on an OS X Server if it can be avoided. 99% of the time it can be (or close to it. Did you know that 47% of all statistics are made up?). Yes, there are applications that require a GUI session to remain logged in. Thankfully, those are a dying breed. So rather than fire up a resource-heavy GUI, I ssh in and use serveradmin:

# serveradmin stop smb
# serveradmin start smb

As you've probably guessed, the first line has server admin stop Windows services (smb stands for Server Message Block, which is the protocol that Windows uses, and from where SaMBa derives its name). Also note that this is only available on OS X Server. Let's take a closer look:

# serveradmin list
afp
appserver
dhcp
dirserv
dns
filebrowser
ftp
info
ipfilter
jabber
mail
nat
netboot
network
nfs
print
privs
qtss
qtsscontents
signaler
smb
swupdate
vpn
web
webobjects
xgrid
xserve

This displays a list of all services that serveradmin knows how to control. This provides a way to stop, start and get status on each service. Better yet, you can read all of the settings for any particular service. Let's capture our settings for OpenDirectory:

# serveradmin settings dirserv
dirserv:secureConfigRecord:errorValue = 2
dirserv:secureConfigRecord:statusMessage = "Unable to find a computer record for this computer"
dirserv:canKerberize = no
dirserv:LDAPSettings:LDAPDataBasePath = "/var/db/openldap/openldap-data"
dirserv:LDAPSettings:searchTimeout = 3600
dirserv:LDAPSettings:LDAPSearchBase = "dc=radiotope,dc=com"
dirserv:LDAPSettings:LDAPSSLCertificatePath = ""
dirserv:LDAPSettings:LDAPCACertificatePath = ""
dirserv:LDAPSettings:LDAPServerBackend = "bdb"
dirserv:LDAPSettings:useSSL = no
dirserv:LDAPSettings:maxSearchResults = "11000"
dirserv:LDAPSettings:LDAPTimeoutUnits = "seconds"
dirserv:LDAPSettings:LDAPSSLKeyPath = ""
dirserv:masterConfig:replicas:_array_index:0:replicaAddress = "192.168.30.8"
dirserv:masterConfig:replicas:_array_index:0:replicaStatus = "OK"
...
dirserv:replicaLastUpdate = ""
dirserv:masterServer = ""
dirserv:LDAPServerType = "master"
dirserv:MacOSXODPolicy:Configured Security Level:Binding Required = no
dirserv:MacOSXODPolicy:Configured Security Level:Packet Encryption = no
dirserv:MacOSXODPolicy:Configured Security Level:Man In The Middle = no
dirserv:MacOSXODPolicy:Configured Security Level:No ClearText Authentications = yes
dirserv:MacOSXODPolicy:Directory Binding = yes
...
dirserv:kerberizedRealmList:availableRealms:_array_index:0:dirNodePath = "/LDAPv3/127.0.0.1"
dirserv:LDAPDefaultPrefix = "dc=lycaeum,dc=radiotope,dc=com"
...
dirserv:PWPolicyInfo:passwordNotAccount = 1
dirserv:PWPolicyInfo:passwordDisableDate = 0.000000
dirserv:PWPolicyInfo:passwordDisableFailedLogins = 0
We can save all of those settings in a file by redirecting the output:
# serveradmin settings dirserv > od_settings.txt

We can restore those settings by redirecting that saved file back into serveradmin:

# serveradmin settings < od_settings.txt

Nicely, you can gather all settings:

# serveradmin settings all > all_settings.txt

Of course, you can set most of those values, too:

# serveradmin settings afp:guestAccess = yes

The Locked Finder

Ever have the Finder lock up on you? You may still see certain apps running, but you just can't interact with anything. Well, the Finder is just another program running on your system (and one that's severely deprecated since the OS 6/7/8/9 days). If you want to shut down cleanly, you can ssh into the machine in question and issue a shutdown:

# shutdown -h now

The "-h" switch stands for "halt" (power down). You can also reboot the machine with the "-r" switch:

# shutdown -r now

Depending on the state of the machine, 'shutdown' may even hang. But reboot goes to the heart of the matter a little more quickly. Just issue:

# reboot

The box should come down in a hurry and reboot. Sometimes, of course, you just have to accept defeat and understand that a hung box just isn't coming back.

Invisible

Without third-party add-ons, the Finder isn't able to mark a file visible or invisible (or, "hidden"). With Apple's Developer Tools installed, you have access to some utilities that can manipulate Finder-level metadata. Inside /Devloper/Tools, you'll find SetFile and GetFileInfo nestled among many other programs. Use GetFileInfo to peek at a file's current settings:

$ ./GetFileInfo /Users/marczak/Pictures/iChat\ Icons/Flags/UK.gif 
file: "/Library/Application Support/Apple/iChat Icons/Flags/UK.gif"
type: ""
creator: ""
attributes: avbstclinmedz
created: 03/21/2005 00:08:23
modified: 03/21/2005 00:08:23

(note how GetFileInfo properly de-referenced the alias in use here). This should all be pretty self-explanatory with the exception of the 'attributes' line. Each letter represents one attribute; if it's a lower-case letter, that attribute is off, upper-case is on. The attributes are:

    A Alias file

    B Bundle

    C Custom icon (files and folders)

    D Desktop (files and folders)

    E Hidden extension (files and folders)

    I Inited (files and folders)

    M Shared (can run multiple times)

    N No INIT resources

    L Locked

    S System (name locked)

    T Stationery

    V Invisible (files and folders)

    Z Busy (files and folders)

Now, you can use SetFile to change any of those attributes. To make a file hidden to the Finder:

$ /Developer/Tools/SetFile -a V /Users/erm/Applications/Secret.app

...and to bring it back:

$ /Developer/Tools/SetFile -a v /Users/erm/Applications/Secret.app

The behavior for hiding and unhiding has changed somewhere along the line. While the change for both used to be immediate, only hiding is now. Once you use "-a v" - the 'visible' switch, the Finder doesn't pick up the change, and requires a reboot (or, perhaps a log out and log in).

Full Circle

Just to make this complete, I want to follow-up on the 'shell' issue. As I mentioned, you're free to select any shell you like. By default, new accounts in Panther and Tiger (10.3 and 10.4) will setup bash as the default. However, if you've upgraded from 10.2 or earlier, your user record came over intact, and will still retain your setting for C-shell (csh). You can determine which shell you use in a few ways. If you already have a shell open, type 'set'. This displays a list of environment variables. If you're using bash, you'll have some variables defined that start with "BASH". C-Shell defines a 'version' variable. Z-Shell (zsh) defines some variables that start with "ZSH". Perhaps even more definitive would be to run a process status and look for your user name:

$ ps aux | grep marczak | grep sh
marczak    496   0.7 -0.0    27812    736  p7  Ss   Thu03PM   0:01.82 /bin/bash
marczak   3432   0.5 -0.0    27812    364  p7  R+    6:40AM   0:00.00 grep sh
marczak    323   0.0 -0.0    27812    188  p1  S    Thu03PM   0:00.02 -bash
marczak    486   0.0 -0.0    27812    188  p2  Ss+  Thu03PM   0:00.01 /bin/bash
marczak    490   0.0 -0.0    27812    648  p4  Ss+  Thu03PM   0:00.18 /bin/bash
marczak    492   0.0 -0.0    27812    664  p5  Ss+  Thu03PM   0:00.18 /bin/bash
marczak    494   0.0 -0.0    27812    612  p6  Ss+  Thu03PM   0:00.15 /bin/bash

We can check our user record to see what our default is:

$ dscl localhost read /NetInfo/root/Users/marczak | grep -i shell
UserShell: /bin/bash

However, just because we have a default shell doesn't mean we can't override it in some way. You can simply run another shell by typing its name:

Jack-Kerouak:~ marczak$ set | grep BASH

BASH=/bin/bash
BASH_VERSINFO=([0]="2" [1]="05b" [2]="0" [3]="1" [4]="release" [5]="powerpc-apple-darwin8.0")
BASH_VERSION='2.05b.0(1)-release'
Jack-Kerouak:~ marczak$ csh
[Jack-Kerouak:~] marczak% set | grep version
version tcsh 6.12.00 (Astron) 2002-07-23 (powerpc-apple-darwin) options 8b,nls,dl,al,kan,sm,rh,color,
  dspm,filec
[Jack-Kerouak:~] marczak% exit
exit
Jack-Kerouak:~ marczak$

If you're a csh user, you may need to run an installer that depends on bash. That's easy to deal with without changing your login shell. Either type 'bash' to run a bash shell, and then exit it when you're done, or, prepend the command with 'bash'.

The traditional way to change your default shell is to use 'chsh' (change shell) while logged in at a shell. This continues to work in OS X. Running chsh will present you with a vi editor (by default) that lets you update your shell. Save the file, and at next login, you'll see the new shell in action. chsh has been patched on OS X to reach into the appropriate place to update your shell in your user record.

Of course, you're free to update your user record directly using dscl or niutil.

Finally, you can have Terminal.app run any shell (or app) you'd like, despite your default shell settings. Figure 1 shows the Terminal.app preference that lets you choose a shell to run: (top of next column)

The "Execute this command" option can be chosen rather than simply running the default shell from your user record. I use 'screen' as the shell on my personal setup. (See "Screen: Living in A Virtual World", MacTech, September 2005, or, find it on the MacTech CD).



Figure 1: Terminal.app Prefs

Finish

In short, there's no magic. "Using the shell" is just a matter of practice, even for those experienced and familiar with it. Sometimes, "using the shell" involves knowledge of one particular tool, which may run deep. Reading about it only gets you so far, though. Open up Terminal.app (or iTerm, or ssh in from a Linux or Windows box) and start typing! You won't break anything. If you're really paranoid, clone your setup or run on a test system. In any case, getting in an really doing it is what it's all about.

Media of the month! I think this is the first time that I'm going to recommend a Mac-centric title! Amit Singh has released his long awaited "Mac OS X Internals - A Systems Approach". Now, this is not light reading in any sense of the word. The book is 1,600+ pages (physically heavy), and tends toward the deeper, more techy, only-5-people-at-Apple-know-this kind of material. However, you will gain OS X knowledge from this book, even if you do not understand it all! Refer to Amit's profile in last month's MacTech 25 for a little more understanding about his works. In any case, it's highly recommended! Check it out at <http://www.osxbook.com>.

Next month, more shell nuggets. Until then, keep practicing!


Ed Marczak often finds himself living in a shotgun shack, in another part of the world, behind the wheel of a large automobile, in a beautiful house, with a beautiful? wife. After that, it's all about guiding people in their technology endeavors. Find out more at http://www.radiotiope.com

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.