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

    15. If the following program (myprog) is run from the command line as myprog friday tuesday sunday, 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

    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

    3. If you need to a test the value of a variable and use that value to determine which statement or set of statements to execute, which structure would be the most straightforward to use?…

    • 959 Words
    • 7 Pages
    Good Essays
  • Satisfactory Essays

    Week 5 Assignments New

    • 754 Words
    • 6 Pages

    8.) A nested if statement is used to ask multiple questions before deciding what processing path to take.…

    • 754 Words
    • 6 Pages
    Satisfactory Essays
  • Good Essays

    W8 Comp

    • 898 Words
    • 4 Pages

    3) What VBScript decision-making statement would be for a single condition and a single code block…

    • 898 Words
    • 4 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
  • 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

    Macro Examples

    • 1120 Words
    • 5 Pages

    7. The If…Then…Else…End If code allows code to be conditionally executed depending on whether a specified logical condition has been met.…

    • 1120 Words
    • 5 Pages
    Good 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
  • 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
  • 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