shell scripting
21 Questions
6 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Which command is used to run a command in the background?

  • & (correct)
  • |
  • cd
  • echo
  • The echo command cannot be used to display the values of shell variables.

    False

    What is the purpose of the pipe operator in shell scripting?

    To use the standard output of one command as the standard input to another command.

    The command to display text files on the screen is called __________.

    <p>cat</p> Signup and view all the answers

    Match each command with its corresponding functionality:

    <p>echo = Display information to standard output cd = Change directory whoami = Display the current user's username date = Display the current date and time</p> Signup and view all the answers

    Which variable contains the full pathname of your login shell?

    <p>SHELL</p> Signup and view all the answers

    Semicolons can be used to separate multiple commands on a single command line.

    <p>True</p> Signup and view all the answers

    What command is used to assign a value to a shell variable?

    <p>The variable name followed by an equal sign and the value (e.g., VARIABLE=value).</p> Signup and view all the answers

    Which of the following is correct regarding the 'let' command in shell scripting?

    <p>It simplifies mathematical operations.</p> Signup and view all the answers

    The while loop continues executing as long as the condition is false.

    <p>False</p> Signup and view all the answers

    What is the purpose of the alias command in shell scripting?

    <p>To shorten names of frequently used commands or change command names.</p> Signup and view all the answers

    The command to keep a list of all entered commands during a session is called the ______.

    <p>history command</p> Signup and view all the answers

    Match the following startup files with their description:

    <p>/etc/profile = System-wide startup file executed first ~/.bashrc = User-specific settings for interactive shells ~/.bash_logout = Executed at logout ~/.bash_profile = User-specific startup script for login shells</p> Signup and view all the answers

    What is the primary function of the tee command?

    <p>To look at output and store it in a file</p> Signup and view all the answers

    The grep command can only search for a specified pattern in a single file at a time.

    <p>False</p> Signup and view all the answers

    What command would you use to sort user IDs in the /etc/passwd file in reverse alphabetical order?

    <p>cut -d':' -f1 /etc/passwd | sort -r</p> Signup and view all the answers

    The ________ command is used to immediately terminate execution of your shell program.

    <p>exit</p> Signup and view all the answers

    Match the commands to their primary purposes:

    <p>tee = View output and save to a file grep = Search for patterns in files sort = Order contents of a file cut = Extract sections from lines of a file</p> Signup and view all the answers

    Which of the following is true about shell scripts?

    <p>They can contain a series of commands for the shell to execute.</p> Signup and view all the answers

    You must use spaces around the equal sign when defining a variable in a shell script.

    <p>False</p> Signup and view all the answers

    How do you make a shell script executable?

    <p>Use the chmod command.</p> Signup and view all the answers

    Study Notes

    Major Features of the Shell

    • Command Execution: The shell interprets and executes commands input by the user.
    • Filename Substitution: The shell uses wildcards (*, ?) to expand filenames, enabling efficient file manipulation.
    • I/O Redirection: The shell allows redirecting standard input (stdin) and output (stdout) using operators like '<', '>', '>>'.
    • Pipes: The shell enables chaining commands using the pipe '|' operator, passing the output of one command as input to the next.
    • Environment Control: The shell manages environment variables, storing information and influencing program behavior.
    • Background Processing: The shell allows tasks to be executed in the background using the ampersand '&' operator.
    • Shell Scripts: The shell supports creating sequences of commands in files called shell scripts, automating tasks and improving efficiency.

    Shell Programs

    • Bourne Shell (sh/bash): This chapter will focus on utilizing the Bourne shell (sh or bash) for scripting.

    echo Command

    • Purpose: Displays information to the standard output (terminal).
    • Usage:
      • echo "Text to display": Prints the specified text.
      • echo -e "Text with escape sequences": Interprets escape sequences within the text, allowing for formatting (e.g., \n for newline).

    Escape Characters

    • \n: Represents a newline character.
    • \t: Represents a tab character.
    • \c: Prevents the newline character from being printed, causing subsequent output to appear on the same line.

    Shell Variables

    • Types:
      • Environment Variables: Global, available to all processes within a user's session.
      • Local Variables: Accessible only within the specific shell script or function where they are defined.
    • Displaying and Removing Variables:
      • set: Displays all shell variables and their values.
      • unset: Removes a variable from the environment.

    Assigning Values to Variables

    • Naming Conventions:
      • Variable names must start with a letter (upper or lowercase) and cannot begin with a digit.
      • No spaces should be used around the = sign when assigning values.
    • Displaying Variables:
      • echo $variable_name: Prints the current value stored in the specified variable.

    HOME Variable

    • Purpose: Stores the absolute path to the user's home directory.
    • Accessing the Value: $HOME

    Standard Shell Variables

    • PS1: Defines the prompt string displayed before commands are entered.
    • PS2: Sets the prompt string shown when pressing [Return] to continue a multi-line command.
    • CDPATH: A list of absolute pathnames used by the cd (change directory) command.
    • SHELL: The full pathname of the user's login shell.
    • TERM: Specifies the type of terminal being used.
    • TZ: Sets the time zone.

    Command Substitution

    • Purpose: To execute commands within a script and use their output.
    • Syntax: Grave accent marks (command)

    Sequencing the Commands

    • Semicolon (;): Separates commands, allowing multiple commands to be executed consecutively on the same line.
    • Parentheses (( )): Group commands together, making them execute as a single unit.

    Background Processing

    • Ampersand (&): Executes a command in the background while the user can continue working in the foreground.

    Chaining the Commands

    • Pipe Operator (|): Redirects the output of one command to the input of another, enabling sequential processing.

    cat Command

    • Purpose: Displays the contents of text files to the screen.

    tee Command

    • Purpose: Splits the output of a command, sending it to both the screen and a designated file.

    grep Command

    • Purpose: Searches files for specified patterns (text strings).
    • Filtering: Useful for extracting specific lines or data from a file.

    grep Command Options

    • -i: Case-insensitive search.
    • -v: Reverse matching, printing lines that don't contain the pattern.
    • -n: Display line numbers along with matching lines.

    sort Command

    • Purpose: Sorts the lines of a file in alphabetical or numerical order.
    • Sorting Criteria: Default is alphabetical, but options can be used to specify different sorting behaviors.

    sort Command Options

    • -r: Reverse sorting.
    • -n: Numerical sorting.
    • -k: Sort using a specific field (column).

    What is a Shell Script?

    • Interpreted Programming: Shell scripts are executed line by line, without compilation.
    • Purpose: Contain sets of commands, allowing for task automation.

    Writing a Simple Script

    • #!/bin/bash: The shebang line specifies the shell interpreter used to execute the script.
    • Comments (#): Text after the hash symbol is ignored by the shell, used for documentation.

    Executing a Script

    • chmod +x script_name: Makes the script file executable.
    • ./script_name: Runs the script within the current directory.
    • script_name: Executes the script if the directory containing the script is in the user's PATH environment variable.

    Command Line Parameters

    • $0: Represents the name of the script itself.
    • $1, $2, ...: Refer to the command-line arguments passed to the script.

    read Command

    • Purpose: Reads input from the user and stores it in a variable.
    • Syntax: read variable_name

    Comments and Variables

    • Comments: Lines beginning with # are treated as comments.
    • Variables: Assign values to variables using the format variable_name=value (no spaces around the equal sign).

    exit Command

    • Purpose: Terminates the execution of a shell script.
    • Exit Code: An optional argument (exit [code]) can specify a code indicating the success or failure of the script.
      • 0: Denotes successful execution.
      • Other values: Can be used to signal different types of errors.
    • Checking Exit Status: echo $? (prints the exit code of the most recently executed command).

    let Command

    • Purpose: Performs basic arithmetic operations.
    • Syntax: let variable=expression

    for-in-done Construct

    • Purpose: Loops through a set of values, executing a block of commands for each value.
    • Syntax:
      for variable in value1 value2 ... do
          commands
      done
      

    The while-do-done Construct

    • Purpose: Repeats a block of commands as long as a condition is true.
    • Syntax:
      while condition; do
          commands
      done
      

    The until-do-done Construct

    • Purpose: Executes a block of commands as long as a condition is false.
    • Syntax:
      until condition; do
          commands
      done
      

    STARTUP FILES

    • System Profile: /etc/profile - the first profile executed when a user logs in.
    • User Profiles:
      • /etc/profile: System-wide profile.
      • ~/.bash_profile: Bash-specific user profile.
      • ~/.bashrc: Bash-specific user profile (loaded when interactive shell is started).
      • ~/.bash_login: Bash-specific user profile .
      • ~/.profile: User-specific profile, typically used for shell settings.
    • At Logout: ~/.bash_logout: Executed when a user logs out.

    Command Line Editing

    • Alias: alias name='command': Creates a shortened name or alias for a command.
    • History: history: Displays a log of recently executed commands.
    • Restarting the History File: Deleting .sh_history (or similar) file in the user's home directory will clear command history.

    DEBUGGING SHELL PROGRAMS

    • Shell Command (sh):

      • -v: Enables verbose mode.
      • -x: Prints each command before it's executed.
      • -n: Reads and checks the script without actually running it.
    • Example: sh -x my_script.sh: Executes the script in debugging mode (verbose, printing each command before execution).

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    4- Shell Scripting PDF

    More Like This

    Shell Scripting Basics
    15 questions

    Shell Scripting Basics

    HallowedMajesty avatar
    HallowedMajesty
    Environment Variables in Shell Scripting
    10 questions
    LPIC-1 Topic 105: Shells and Shell Scripting
    5 questions
    Use Quizgecko on...
    Browser
    Browser