Shell Script Basics Quiz
10 Questions
0 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

What is required to enclose a complete expression in a shell script?

  • Double quotes
  • Parentheses
  • Single quotes
  • Backticks (correct)
  • Which command correctly adds five numbers provided as command line arguments in a shell script?

  • a=$[$1 + $2 + $3 + $4 + $5]
  • a=`echo $1 + $2 + $3 + $4 + $5`
  • a=`expr $1 + $2 + $3 + $4 + $5` (correct)
  • a=add($1, $2, $3, $4, $5)
  • What type of variable in shell scripting can store multiple values simultaneously?

  • Constant variable
  • Global variable
  • Array variable (correct)
  • Scalar variable
  • How can an individual value be assigned to an array in shell scripting?

    <p>array_name[index] = value</p> Signup and view all the answers

    What is the primary purpose of using arrays in shell scripting?

    <p>To group multiple variables under one name</p> Signup and view all the answers

    Match the following commands with their descriptions:

    <p>NAME[0]=&quot;Zara&quot; = Assigns 'Zara' to the first index of the array echo &quot;First Index: \${NAME[0]}&quot; = Displays the value of the first index \${NAME[*]} = Accesses all items in the array for i in \$list[@]; do = Iterates over each item in the array 'list'</p> Signup and view all the answers

    Match the array access methods with their usage:

    <p>\$array_name[*] = Accesses all elements in the array as a single word \$array_name[@] = Accesses all elements in the array individually \ = Starts the command line with a new line if [ ! -f \$i ]; then = Condition to check if a file does not exist</p> Signup and view all the answers

    Match the shell script outputs with their corresponding commands:

    <p>echo &quot;First Method: \$NAME[*]&quot; = Displays all names in one line echo&quot;Second Method: \$NAME[@]&quot; = Displays all names individually in separate lines echo &quot;Warning, \$i not found!&quot; = Alert for missing files during iteration echo \$i = Outputs the current item in the loop</p> Signup and view all the answers

    Match the index with the corresponding name in the array:

    <p>NAME[1] = Qadir NAME[3] = Ayan NAME[4] = Daisy NAME[2] = Mahnaz</p> Signup and view all the answers

    Match the script type with its example usage:

    <p>#!/bin/sh = Used for shell scripts list=(File1 File2 File3 File4) = Defines an array called 'list' echo &quot;Second Index: \$NAME[1]&quot; = Displays the name at index 1 of the 'NAME' array if [ ! -f \$i ]; then = Checks for the non-existence of a file</p> Signup and view all the answers

    Study Notes

    Shell Script Basics

    • To create a new shell script, use a text editor like vi, then enter commands starting with #!/bin/bash.
    • Use echo to output messages or variable contents.
    • Save changes in vi by pressing ESC, typing :wq, and hitting Enter.

    Making Scripts Executable

    • Change the script's permissions to executable with chmod 700 script_name.sh.
    • Run the script using ./script_name.sh.

    Copying and Deleting Files in Directories

    • Create a directory with mkdir directory_name.
    • Copy all files into this directory using cp * directory_name.
    • Delete the directory and its contents with rm -rf directory_name.

    Using echo for Output

    • The echo command is used to display strings, variables, and command outputs.
    • Variables are referenced with a $ (e.g., echo $NAME).
    • Command output can be included via backticks (`command`) or $().

    Input and Output in Scripts

    • Prompt for user input using read (e.g., read NAME).
    • Capture user input with read and reference it later (e.g., echo My name is $name).

    Special Shell Variables

    • Positional parameters store command-line arguments (e.g., $1, $2).
    • Special variables:
      • $0: script name
      • $?: exit status of the last command
      • $$: current process ID
      • $!: last background command’s process ID
      • $@ and $*: all positional parameters

    Arithmetic Operations

    • Use expr for basic arithmetic calculations.
    • Syntax: expr op1 operator op2.
    • Ensure spaces around operators and use backticks for command evaluation.

    Shell Arrays

    • Arrays can hold multiple values, unlike scalar variables which hold a single value.
    • Define an array using syntax: array_name[index]=value.
    • This allows better organization of related data, such as grouping names or numbers.

    Script Examples

    • Examples include prompting for input, creating directories, copying files, and performing arithmetic operations.
    • Scripts can interact with users and respond dynamically based on inputs.

    Environment Variables

    • Shell scripts can access predefined environment variables like $USER, $HOME, and $SHELL for information about the user and runtime environment.

    Practical Usage

    • Use scripts to automate repetitive tasks and manage files efficiently.
    • Utilize input/output strategies to create responsive scripts that interact with users.

    Shell Scripting Basics

    • A shell script can output text using the echo command.
    • Example scripted outputs include personal information and location.

    Saving and Executing Scripts

    • Save changes in vi by pressing the Esc key followed by :wq.
    • Make the script executable with chmod 700 address.sh.
    • Run the script using ./address.sh.

    File Management Commands

    • Create a directory named trash with mkdir trash.
    • Copy all files into the trash directory using cp * trash.
    • Remove the trash directory along with its contents using rm --rf trash.
    • Recreate the trash directory.

    Using Command-Line Arguments

    • Positional parameters in scripts are represented by $1, $2, etc., where $0 indicates the script name.
    • Escape sequences and special characters can be managed using echo with the -e option.
    • Examples of special variables include:
      • $# - Number of arguments supplied.
      • $* - All arguments as a single string.
      • $@ - All arguments as individually quoted strings.

    Input and Output in Shell Scripts

    • Utilize expr to perform arithmetic operations such as addition directly in scripts.
    • Example script to add five command-line numbers outputs their sum.

    Shell Arrays

    • Arrays in shell scripting allow grouping multiple values under one variable name.
    • Array elements are defined using array_name[index]=value.
    • Access array elements with ${array_name[index]}.
    • Iterate through arrays using for loops to reference individual elements.

    Example of Shell Script for Arrays

    • Create an array named NAME with several student names.
    • Access array values using indexed variables.
    • Print out all names in the array using ${array_name[@]} and ${array_name[*]} for demonstration.

    Error Handling

    • Script can include checks for file existence using conditional statements.
    • A warning can be issued if a file is not found during iteration.

    Summary

    • Shell scripting involves creating executable commands, managing files and outputs, handling input arguments, and utilizing arrays for data organization.
    • Mastery of these concepts enables efficient automation of tasks in a Unix-like environment.

    Studying That Suits You

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

    Quiz Team

    Related Documents

    lab8.docx

    Description

    Test your knowledge on the fundamentals of shell scripting. This quiz covers the creation, execution, and management of shell scripts, including permissions and file operations. Perfect for those beginning their journey into shell programming.

    More Like This

    Bash Shell Scripting Parameters Quiz
    15 questions
    Shell Scripting Basics
    40 questions

    Shell Scripting Basics

    RevolutionaryFern2779 avatar
    RevolutionaryFern2779
    Einführung in Shell Scripting
    40 questions

    Einführung in Shell Scripting

    RevolutionaryFern2779 avatar
    RevolutionaryFern2779
    Shell-Scripting Grundlagen
    40 questions

    Shell-Scripting Grundlagen

    RevolutionaryFern2779 avatar
    RevolutionaryFern2779
    Use Quizgecko on...
    Browser
    Browser