Shell script tutorial mac os x

She Sells C Shells
Contents:
  1. Organization of This Document
  2. 1. Inside the Terminal - Mac OS X For Unix Geeks, 4th Edition [Book]
  3. Shell Scripting Primer
  4. 2 comments

This script provides an example of a variable assignment. You should immediately notice that variables may or may not begin with a dollar sign, depending on how you are using them. If you want to dereference a variable, you precede it with a dollar sign. The shell then inserts the contents of the variable at that point in the script. For all other uses, you do not precede it with a dollar sign.

You should also notice that the argument to echo is surrounded by double quotation marks. Notice that the echo statement is followed by a string surrounded by quotation marks. Normally, the shell uses spaces to separate arguments to commands. By surrounding the string with double quote marks, the shell treats the entire string as a single argument to echo even though it contains spaces.

To see how this works, save the script above as test. Notice also that there are similar quotation marks on the right side of the assignment statement:. With most modern shells, these double quotation marks are not required for this particular assignment statement because there are no literal spaces on the right side , but they are a good idea for maximum compatibility.

When assigning literal strings rather than variables containing strings to a variable, however, you must surround any spaces with quotation marks. For example, the following statement does not do what you might initially suspect:. The reason for this seemingly odd error is that the assignment statement ends at the first space, so the next word after that statement is interpreted as a command to execute. Using quotation marks is particularly important when working with variables that contain filenames or paths.

For example, type the following commands:. It then attempts to list the files in that directory. The first time, it uses quotation marks. The second time, it does not. In modern Bourne shells, expansion of variables, occurs after the statement itself is fully parsed by the shell. However, if you are using double quote marks within a literal string, you must quote that string properly.

This quoting technique also applies to literal strings within commands entered on the command line. For example, using the script from earlier in Shell Variables and Printing , the command:. The details of quotes as they apply to variable expansion are explained in Parsing, Variable Expansion, and Quoting. Variable safety with shells that predate this behavior is generally impractical. Fortunately, the modern behavior has been the norm since the mids. Shell scripts also allow the use of single quote marks.

Variables between single quotes are not replaced by their contents. Be sure to use double quotes unless you are intentionally trying to display the actual name of the variable. You can also use single quotes as a way to avoid the shell interpreting the contents of the string in any way. These differences are described further in Parsing, Variable Expansion, and Quoting.

Organization of This Document

One key feature of shell scripts is that variables are typically limited in their scope to the currently running script. The scoping of variables is described in more detail in Subroutines, Scoping, and Sourcing. For now, though, it suffices to say that variables generally do not get passed on to scripts or tools that they execute. Normally, this is what you want. Most variables in a shell script do not have any meaning to the tools that they execute, and thus represent clutter and the potential for variable namespace collisions if they are exported.

Occasionally, however, you will find it necessary to make a variable's value available to an outside tool. To do this, you must export the variable. These exported variables are commonly known as environment variables because they affect the execution of every script or tool that runs but are not part of those scripts or tools themselves. A classic example of an environment variable that is significant to scripts and tools is the PATH variable. This variable specifies a list of locations that the shell searches when executing programs by name without specifying a complete path.

For example, when you type ls on the command line, the shell searches in the locations specified in PATH in the order specified until it finds an executable called ls or runs out of locations, whichever comes first. The details of exporting shell variables differ considerably between the Bourne shell and the C shell. Thus, the following sections explain these details in a shell-specific fashion. Generally speaking, the first time you assign a value to an environment variable such as the PATH variable, the Bourne shell creates a new, local copy of this shell variable that is specific to your script.

1. Inside the Terminal - Mac OS X For Unix Geeks, 4th Edition [Book]

Any tool executed from your script is passed the original value of PATH inherited from whatever script, tool, or shell that launched it. With the BASH shell, however, any variable inherited from the environment is automatically exported by the shell. Thus, in some versions of OS X, if you modify inherited environment variables such as PATH in a script, your local changes will be seen automatically by any tool or script that your script executes. Because different Bourne shell variants handle these external environment variables differently even among different versions of OS X , this creates two minor portability problems:.

A script written without the export statement may work on some versions of OS X, but will fail on others.

You can solve this portability problem by using the export builtin, as described in this section. A shell script that changes variables such as PATH will alter the behavior of any script that it executes, which may or may not be desirable. To guarantee that your modifications to a shell variable are passed to any script or tool that your shell script calls, you must use the export builtin. You do not have to use this command every time you change the value; the variable remains exported until the shell script exits. Either of these statements has the same effect—specifically, they export the local notion of the PATH environment variable to any command that your script executes from now on.

There is a small catch, however. You cannot later undo this export to restore the original global declaration. Thus, if you need to retain the original value, you must store it somewhere yourself. In the following example, the script stores the original value of the PATH environment variable, exports an altered version, executes a command, and restores the old version. If you need to find out whether an environment variable whether inherited by your script or explicitly set with the export directive was set to empty or was never set in the first place, you can use the printenv command to obtain a complete list of defined variables and use grep to see if it is in the list.

The resulting variable will contain 1 if the variable is defined in the environment or 0 if it is not. Because the BASH Bourne shell variant automatically exports all variables inherited from its environment, any changes you make to preexisting environment variables such as PATH are automatically inherited by any tool or script that your script executes.

This is not true for other Bourne shell variants; see Using the export Builtin Bourne Shell for further explanation. While automatic export is usually convenient, you may sometimes wish to change a preexisting environment variable without modifying the environment of any script or tool that your script executes. This problem is easily solved by overriding the environment variable PATH on a per-execution basis.

Consider the following script:. Normally, this variable is empty, so this script just prints a blank line. Save the script as printmyvar.


  1. pokertracker 4 mac zoom hud!
  2. dragon dictate mac demo download.
  3. mac compatible touch screen display.
  4. best mac software for web design?
  5. Shell Script Dialects.
  6. Understanding the syntax.

However, the original empty value is restored after executing that command, so the echo statement afterwards prints an empty string for the value of MYVAR. In the C shell, variables are exported if you set them with setenv , but not if you set them with set. Thus, if you want your shell variable modifications to be seen by any tool or script that you call, you should use the setenv builtin. This builtin is the C shell equivalent to issuing an assignment statement with the export builtin in the Bourne shell.

If you want your shell variables to only be available to your script, you should use the set builtin described in Shell Variables and Printing. This also works with other special characters, such as parentheses. The following special characters have meaning to the shell and so must be escaped: Here is an example of how to use a backslash to escape a space character in a file or directory name:. Alternatively, you can use quotation marks around the file or directory name that contains the space, as follows: There is one other way to get around this problem, but it involves using the Finder in combination with the Terminal application.

To launch Microsoft Word from the Terminal, you could enter the path as follows, using escape characters:. As you can see, neither method is very pretty, and both require you to know a lot of detail about the path. Now for the easy way:. Type the name of the command and any initial arguments it requires on the command line in this case, open -a , followed by a space. Locate Microsoft Word in the Finder, and drag its icon to the Terminal window to insert the path after the space.

When you do this, the spaces and any other special characters in the path will automatically be escaped with backslashes:. You can also drag and drop URLs from a web browser. For example, to use curl to download files from the command line:. Open a new Terminal window and type curl -O , with a space after the -O switch. Bring up your web browser and navigate to http: Drag the image at the top of the page to the Terminal window.

You should now see the following in the Terminal window:.

Bash Shell Scripting On Mac OS X

If you want to type a long pathname, you can cut down on the number of keystrokes required by using tab completion. Early versions of Mac OS X shipped with tcsh as the default shell.

You can change the default shell for Terminal in its Preferences menu, but this does not affect the login shell used for remote or console logins. In the case of the Terminal, the services operate on text that you have selected the pasteboard. To use a service, select a region of text in the Terminal window and choose one of the following items from the Services menu:.

This service can be used to convert selected text to either simplified Chinese or traditional Chinese. This service opens the URL specified by the selected text in your default web browser. This service gets the result of an AppleScript after running the highlighted text as an AppleScript , makes a new AppleScript in the Script Editor , or runs the selected text as an AppleScript without returning the result. This service searches for the selected text using http: This service displays a dialog that lets you choose a file to send to a Bluetooth device. This service begins speaking the selected text.

Mac OS X provides command-line utilities for working with Spotlight. This service condenses the selected text into a summary document. The summary service analyzes English text and makes it as concise as possible while retaining the original meaning. The TextEdit service can open a filename, or open a new file containing the selected text. As far as the service is concerned, you are invoking it on a string of text. Bonjour http: It is based on a standard called ZeroConf http: Like many Mac OS X applications e. If you have computers with other operating systems on your network, they may be able to take advantage of Bonjour, too.

Apple makes a Bonjour for Windows available at http: As noted earlier, other Aqua-native terminal applications are available; the freeware iTerm http: Sathyam, is a particularly attractive one. Before getting into what makes iTerm distinct, here are some similarities between iTerm and Terminal:. Both iTerm and Terminal support transparency, language encodings, and AppleScript, and both have contextual menus that can be accessed by Control-clicking or right-clicking, if you have a two- or three-button mouse in a window. Particularly interesting features of iTerm include support for multiple tabbed terminal sessions within each window, support for background images, profiles that allow you to open new iTerm sessions with preset terminal settings, and bookmarks for launching iTerm windows or tabs that automatically execute commands.

What is the command line?

Tabs in iTerm can be dragged to new windows, and one iTerm window can be dragged onto another to form a new tab. The effect of dragging and dropping windows and tabs in iTerm is no different from the same set of actions in Terminal. Bookmarks are used to define iTerm sessions with preset terminal settings.

http://coleslandlord.com/components/month/3357-astrology-for-the.php

Shell Scripting Primer

For example, you can define the color or typeface to use for text as well as a command to execute upon opening a new tab or window with a given bookmark. Aside from the command that a bookmark will execute, other attributes must be selected from predefined profiles. There are three types of profiles in iTerm: Keyboard, Terminal, and Display profiles.

Click the Add button at the bottom left of the Profiles window to add a new profile in any of the three categories. While a profile determines certain attributes of an iTerm window or tab , bookmarks are used to open windows and tabs with those attributes. A bookmark may also be defined to execute commands within a window or tab. The default bookmarks are Default, which specifies the default login shell, and Bonjour, which includes ssh and sftp connections to SSH- and Bonjour-enabled computers on the LAN. Then, click the plus sign in the bottom-left corner of the Bookmarks window to add a new bookmark.

If no iTerm window is open, the bookmark will open in a new iTerm window; otherwise, the bookmark will open in the frontmost iTerm window. In this case, the set of bookmarks associated with the group will open as tabs within the same iTerm window. If at least one iTerm window is already open, the group will open as tabs in the frontmost window, adding to whatever tabs are already present in that window.

The effect of an iTerm bookmark group is similar to that of a window group in Terminal, but Terminal window groups are not restricted to opening as tabs in the same window. New Tab allows you to choose a session from the bookmarks , Select selects a tab from the current window , Browser opens the selected URL in your default web browser , Mail opens a compose mail window with the selected email address as the recipient , Copy, Paste, Save, Select All, Clear Buffer, Info, and Close.

The one feature that could tip the scale in favor of iTerm is its support of background images in iTerm windows, assuming that this feature is of paramount importance to you. Terminator http: Though it supports tabs, its feature set seems to come up short when compared to Terminal and iTerm.

On the other hand, if having a cross-platform terminal emulator program is important to you, this one is worth a try. The open shell command lets you open Finder windows and launch Aqua applications. To open a directory in the Finder, use open followed by the name of the directory.

For example, to open a Finder window containing the current directory, enter the following command:. To open an application, you need only its name and the -a switch. To open multiple instances of a program, give the -n switch a whirl but note that some applications, such as Firefox, may refuse to open a second instance:. You are not required to enter the path for the application, only its name. The only time you are required to enter the path is if you have two different versions of an application with similar names on your system.

The -e option directs the file to be opened in TextEdit; it cannot be used to open a file in another text editor, such as BBEdit, Smultron, or TextMate However, if you want to open a file using BBEdit, you can use the following:. Many popular text editors include their own command-line applications for editing documents from the command line: For example, you could enter the following into your. Then, the next time you want to open a text file that would otherwise require use of the sudo command, you could just enter the following:.

In Leopard, you can simply launch the X11 application as you would any other Unix application, as in:. To install OpenTerminal, download it from the OpenTerminal website, unzip the downloaded file, then drag and drop the enclosed OpenTerminal application icon to a convenient location e. Stay ahead with the world's most comprehensive technology and business learning platform. With Safari, you learn the way you learn best.

Get unlimited access to videos, live online training, learning paths, books, tutorials, and more. Start Free Trial No credit card required. Inside the Terminal. Mac OS X Shells. The Terminal and xterm Compared. Note Beginning with Leopard, the X11 package is installed by default. Enabling the root User. Using the Terminal. Launching Terminal Windows. A script to open a new Terminal window. Another script to open a new Terminal window.

Note To cycle between open Terminal windows, you can use the same keystroke that most other Mac OS X applications use: Double-clickable shell scripts. Launching the vim editor.

2 comments

Copy the desired icon to the clipboard. The Contextual Menu. Customizing the Terminal.


  • fleetwood mac live 2013 mts centre may 12.
  • Stay ahead with the world's most comprehensive technology and business learning platform.?
  • logos bible software mac slow.
  • call of duty 4 maps download mac.
  • drop down in excel for mac;
  • Note You can change the default shell in the Terminal Preferences, but this change applies only to Terminal i. Options for Terminal settings. Tab Options Text Font: Choose your font. Window Title: Shell Startup: Advanced Declare your terminal i. Window groups. Customizing the Terminal on the Fly. Setting the Terminal title in a shell script. Exporting and Importing Terminal Settings. An execution string to connect to a remote host.

    Warning Each time you add a new setting, that new setting becomes your default. Warning Although double-clicking a. Working with File and Directory Names. Note By default, files whose names begin with a dot are invisible in the Finder. When you do this, the spaces and any other special characters in the path will automatically be escaped with backslashes: You should now see the following in the Terminal window: Tab Completion. Changing Your Shell. The Services Menu. ChineseTextConverter This service can be used to convert selected text to either simplified Chinese or traditional Chinese.

    Font Book Not supported by the Terminal. Grab Not supported by the Terminal.