Preview

Basic Shell Scripting

Powerful Essays
Open Document
Open Document
33932 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Basic Shell Scripting
Shell Scripting Primer

Contents

Introduction 12
Organization of This Document 12

Before You Begin 14
Obtaining a Shell Prompt 14 In Mac OS X 14 In Other UNIX Variants or Linux Variants 15 In Windows 15 Familiarize Yourself With the Command Line 15 Tips for Shell Users 15 The alias Builtin 15 Entering Special Characters 17 Creating Text Files in Your Home Directory 17 In TextEdit 17 In pico or nano 18

Shell Script Basics 19
Shell Script Dialects 19 She Sells C Shells 21 Shell Variables and Printing 21 Using Arguments And Variables That Contain Spaces 23 Handling Quotation Marks in Strings 25 Exporting Shell Variables 26 Using the export Builtin (Bourne Shell) 27 Overriding Environment Variables for Child Processes (Bourne Shell) 28 Using the setenv Builtin (C shell) 29 Overriding Environment Variables for Child Processes (C Shell) 31 Deleting Shell Variables 32

Shell Input and Output 33
Shell Script Input and Output Using printf and read 33 Bulk I/O Using the cat Command 35 Pipes and Redirection 38 Basic File Redirection 38

2011-07-27 | © 2003, 2011 Apple Inc. All Rights Reserved.

2

Contents

Pipes and File Descriptor Redirection (Bourne Shell) 40 Pipes and File Descriptor Redirection (C Shell) 42

Flow Control, Expansion, and Parsing 44
Basic Control Statements 44 The if Statement 44 The test Command and Bracket Notation 46 The while Statement 48 The for Statement 50 The case statement 53 The expr Command 56 Parsing, Variable Expansion, and Quoting 59 Variable Expansion and Field Separators 60 Special Characters Explained 61 Quoting Special Characters 64 Inline Execution 66

Result Codes, Chaining, and Flags 68
Working with Result Codes 68 Chaining Execution 69 Handling Flags and Arguments 72 Special Multi-argument Variables 72 The shift Builtin 74 The getopts builtin and the getopt command 75

Subroutines, Scoping, and Sourcing 81
Subroutine Basics 81 Anonymous Subroutines 82 Variable Scoping 84 Declaring a Local Variable

You May Also Find These Documents Helpful

  • Better Essays

    Nt1310 Unit 4

    • 3614 Words
    • 15 Pages

    13. If the following program (myprog) is run from the command line as myprog monday tuesday wednesday thursday? What would be the output?…

    • 3614 Words
    • 15 Pages
    Better Essays
  • Powerful Essays

    NT1430 Lab2 Joy Brewer

    • 989 Words
    • 4 Pages

    4. What is a Process? A process is the execution of a command by Linux (they can communicate with each other) What Utility provides communication between processes where output of one process provide input for another? pipe…

    • 989 Words
    • 4 Pages
    Powerful Essays
  • Satisfactory Essays

    PT1420week5exdkinney

    • 404 Words
    • 2 Pages

    Built in functions are valuable and convenient because they can cut down the time and energy it takes into writing the entire program repeatedly. The built-in functions work like mini programs that perform simple tasks so that you can concentrate your efforts elsewhere. As with all functions, the built-in functions return values and, optionally, accept arguments that you pass to the functions when you call the functions. This essay will list different Visual Basic’s built in functions.…

    • 404 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    NT1430

    • 369 Words
    • 2 Pages

    The Bourne shell is the original unix shell, often called a command interpreter, that was developed at AT&T. Named for its developer, Stephen Bourne, the Bourne shell is also known by its program name, sh. The shell prompt (character displayed to indicate readiness for input) used is the $ symbol. The Bourne shell family includes the Bourne, korn shell, bash, and zsh shells.…

    • 369 Words
    • 2 Pages
    Satisfactory Essays
  • Powerful Essays

    Mat 540 Week 4 Paper

    • 1775 Words
    • 8 Pages

    * Provide a set of values that will test the normal operation of this program segment. Defend your choices.…

    • 1775 Words
    • 8 Pages
    Powerful Essays
  • Good Essays

    This paper will describe the variable naming rules of three different programming languages. These three languages are Visual Basic, Python, and Java. Each of these languages has different rules that apply to them and some similarities. I will describe the rules and the similarities and differences in the next few paragraphs.…

    • 878 Words
    • 4 Pages
    Good Essays
  • Satisfactory Essays

    It 250 Homework 4

    • 769 Words
    • 4 Pages

    1. What does the shell ordinarily do while a command is executing? What should you do if you do not want to wait for a command to finish before running another command?…

    • 769 Words
    • 4 Pages
    Satisfactory Essays
  • Powerful Essays

    4. What is a shell? How does it work with the kernel? With the user?…

    • 1455 Words
    • 4 Pages
    Powerful Essays
  • Good Essays

    Unit 5 PT1420

    • 959 Words
    • 7 Pages

    The case structure lets the value of a variable or an expression determine which path of execution the program will take.…

    • 959 Words
    • 7 Pages
    Good Essays
  • Satisfactory Essays

    I Like to 1321

    • 459 Words
    • 2 Pages

    statement. Also included is a for loop (repetition statement). Pay close attention to these as you…

    • 459 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    The expression 1 To 5 in the Case 1 To 5 clause specifies the range of numbers from ____.…

    • 595 Words
    • 3 Pages
    Satisfactory Essays
  • Powerful Essays

    Level: Intermediate Shiv Dutta, Senior Software Engineer, IBM 06 May 2003 Updated 24 Oct 2006 Do you ever feel you wish you could answer some of your own questions when you work with AIX® and your System p™ server? Do you ever feel you could save time by not having to call on the support professionals all the time? Well, wish no more. Shiv Dutta discusses some of the AIX commands that answer those questions and tells you how to enlarge the list of such answers.…

    • 3391 Words
    • 14 Pages
    Powerful Essays
  • Satisfactory Essays

    UNIX Commands Table

    • 426 Words
    • 3 Pages

    Change the current directory. IF an error message appears it will state directory does not exist.…

    • 426 Words
    • 3 Pages
    Satisfactory Essays
  • Good Essays

    Users of the UNIX operating system, for example, can select among a variety of shells including the Bourne shell, the C shell, and the Korn shell, as…

    • 491 Words
    • 2 Pages
    Good Essays
  • Good Essays

    The switch statement is wondrous and magic. It's a piece of the language that allows you to select between different options for a value, and run different pieces of code depending on which value is set.…

    • 7287 Words
    • 30 Pages
    Good Essays