Shell Scripting Basics
40 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

Match the Bash concepts with their correct descriptions:

Arguments = Used to pass input to scripts Conditions = Control the flow based on tests Switch Case = Handle multiple choices Arrays = Store multiple values in a single variable

Match the Bash syntax with its purpose:

$1, $2 = Refer to positional parameters if [[ condition ]] = Start a conditional block case $variable in = Initiate a switch statement echo = Output text to the console

Match the condition check with its outcome:

if [[ -z ${name} ]] = Check if variable 'name' is empty if [[ some_test ]] = Execute if some condition is true case $car in = Evaluate different car brand patterns [*] = Default case in switch statement

Match the UNIX directories with their descriptions:

<p>/ = Root directory, the top of the file system hierarchy bin/ = Contains executable programs dev/ = Contains device files for hardware access etc/ = Holds configuration files for the system</p> Signup and view all the answers

Match the array declaration with its correct syntax:

<p>my_array=(&quot;value 1&quot; &quot;value 2&quot;) = Declare an array with two values echo ${my_array} = Print the elements of an array my_array[0] = Access the first element in an array my_array+=(&quot;value 3&quot;) = Add a new value to the array</p> Signup and view all the answers

Match the shell script principles with their explanations:

<p>Documenting = Use # for comments in scripts Set shell = First line in a script should be #!/bin/bash Modular programming = Follow KISS and UNIX philosophy Clean coding = Check if parameters and file availability are correct</p> Signup and view all the answers

Match the commands with their expected outcomes:

<p>read -p &quot;What is your name?&quot; = Prompt user for input echo &quot;Hi there ${name}&quot; = Greet the user with their name echo -n &quot;${car} is an unknown brand.&quot; = Inform about an unrecognized car brand echo –n &quot;${car}’s factory is in USA.&quot; = Output the factory location for Tesla</p> Signup and view all the answers

Match the command outputs with their functions in the example script:

<p>netstat -nat = Lists all network connections grep -E 'LISTEN' = Filters active ports that are listening fuser -n tcp $PORT = Finds processes using the specified port ps -f -q $PID = Displays detailed information about the process</p> Signup and view all the answers

Match the scripting control structures with their use cases:

<p>If statement = Conditionally execute code Switch case = Select between several alternatives For loop = Iterate over a sequence While loop = Repeat while a condition is true</p> Signup and view all the answers

Match the elements of the UNIX file system hierarchy with their roles:

<p>Root (/) = Top-level of the filesystem home/ = User files and directories lib/ = Contains essential libraries bin/ = Storage for binary executables</p> Signup and view all the answers

Match the syntax with its execution result:

<p>pattern_1) = Define a case action in switch ;; = End a case statement -n = Check if variable is not empty echo &quot;Please enter your name!&quot; = Request name input if empty</p> Signup and view all the answers

Match the components of the shell script with their purposes:

<p>for PORT in ... = Iterates over each listening port echo 'INFORMATION FOR OPEN PORT $PORT' = Outputs information for the current port cut -f2-4 -d':' = Extracts specific fields from data sort -nu = Sorts the output uniquely</p> Signup and view all the answers

Match the command structure with its example:

<p>if [[ -z condition ]] = Checks if a variable is empty read -p &quot;Prompt&quot; = Gets user input with a message case variable in = Starts a selection statement my_array+=(&quot;new value&quot;) = Adds an element to an array</p> Signup and view all the answers

Match the descriptions of UNIX directory contents:

<p>bin/ = Executables and binaries dev/ = Device files for system interaction etc/ = System and configuration settings home/ = Personal directories for users</p> Signup and view all the answers

Match the script behavior with its expected results:

<p>Running the script as superuser = Access to all network ports Using 'sed -e 's/://g'' = Removes colons from the port output Employing 'sort -nu' = Eliminates duplicate values Echo command in loop = Displays status information continuously</p> Signup and view all the answers

Match the key programming concepts with their functions in shell scripting:

<p>KISS = Keep it simple, stupid UNIX philosophy = Follow principles of modularity and clarity Parameter checking = Ensure expected input is provided Commenting = Documenting code for clarity and maintenance</p> Signup and view all the answers

Match the following Bash loop types with their descriptions:

<p>For loop = Iterates over a list of items While loop = Continues executing as long as a condition is true Until loop = Executes until a condition becomes true Do-while loop = Executes at least once before checking the condition</p> Signup and view all the answers

Match the following Bash commands with their purposes:

<p>echo = Displays a line of text read = Takes input from the user set -x = Activates debugging mode ((...)) = Performs arithmetic operations</p> Signup and view all the answers

Match the following Bash constructs with their roles:

<p>Function = Reusable block of code Variable = Stores a value for later use Command = An instruction to perform an action Script = A file containing a sequence of commands</p> Signup and view all the answers

Match the following Bash function components with their descriptions:

<p>function_name = The name of the function { ... } = Encloses the commands of the function hello = A call to the function echo 'Hello Function!' = Output statement in the function</p> Signup and view all the answers

Match the following Bash syntax elements with their functions:

<p>$users = Holds a list of user names ${list} = Used to reference a variable counter=1 = Initializes a counter variable -z ${name} = Checks if a variable is empty</p> Signup and view all the answers

Match the following debugging techniques with their descriptions:

<p>set -x = Enables step-by-step command output bash -x ./my_script = Runs a script in debug mode echo = Outputs text to the terminal read -p = Prompts the user for input</p> Signup and view all the answers

Match the following elements with their usage in shell scripting:

<p>for var in ... = Starts a for loop while [[ ... ]] = Starts a while loop read -p ... = Collects user input ((counter++)) = Increments the counter variable</p> Signup and view all the answers

Match the following keywords with their corresponding Bash loop types:

<p>for = Repeats a block for each item in a list while = Checks a condition before each iteration until = Continues until a condition becomes true do = Indicates the start of a loop block</p> Signup and view all the answers

Match the following file permissions with their description:

<p>r = Read permission w = Write permission x = Execute permission</p> <ul> <li>= No permission</li> </ul> Signup and view all the answers

Match the following directory paths with their purpose:

<p>/tmp/ = Stores temporary files /usr/ = Holds files and installed programs system-wide /home/ = User home directories /var/ = Files that change frequently</p> Signup and view all the answers

Match the types of files in UNIX with their symbols:

<p>d = Directory</p> <ul> <li>= Regular file l = Soft link c = Character device</li> </ul> Signup and view all the answers

Match the following command with its function:

<p>ls -al = List all files in long format chmod = Change file permissions cp = Copy files mv = Move or rename files</p> Signup and view all the answers

Match the following user statuses with their corresponding access levels:

<p>Owner = Has full permissions Group = Limited permissions within a group Others = Minimal or no permissions Admin = Can modify all files</p> Signup and view all the answers

Match the following file types with their attributes:

<p>drwx = Directory with read, write, execute permissions -rw- = Regular file with read, write permissions lrwx = Symbolic link file dr-x = Directory with read and execute permissions only</p> Signup and view all the answers

Match the following command usage with their corresponding command:

<p>ls = List directory contents cat = Concatenate and display file content rm = Remove files or directories mkdir = Create a new directory</p> Signup and view all the answers

Match the following file descriptions with their terms:

<p>User = The individual who owns a file Group = A set of users that share permissions Inode = Structure that stores file metadata Directory = A container for files</p> Signup and view all the answers

Match the following file permissions with their corresponding octal representation:

<p>Read owner = 400 Write owner = 200 Execute owner = 100 Read group = 040</p> Signup and view all the answers

Match the following commands with their descriptions:

<p>chmod 600 ~/.bashrc = Sets read/write permissions for owner ls -al ~/.bashrc = Shows file details in long format rm -fr /home/rakeller/ = Attempts to remove a directory ls -a = Displays hidden files</p> Signup and view all the answers

Match the following types of users with their permissions:

<p>Owner = Can read, write and execute if set to 700 Group = Can read if set to 040 Other = Can write if set to 002 Group member = Can execute if set to 010</p> Signup and view all the answers

Match the following examples of permissions with their meanings:

<p>chmod 700 = Owner can read, write, execute chmod 600 = Owner can read, write, cannot execute chmod 400 = Owner can only read chmod 000 = No permissions granted</p> Signup and view all the answers

Match the following terms with their definitions:

<p>Remove command = Command to delete files and directories chmod = Command to change file permissions ls = Command to list directory contents Hidden files = Files that start with a dot (.)</p> Signup and view all the answers

Match the characters used for file permissions with their descriptions:

<p>r = Read permission w = Write permission x = Execute permission</p> <ul> <li>= No permission</li> </ul> Signup and view all the answers

Match the following groups with the permissions they have:

<p>Owner = 600 allows read and write Group members = 040 allows read Other users = 004 allows read Group write = 020 allows write</p> Signup and view all the answers

Match the following numerical permissions with their applicable users:

<p>400 = Owner can read 200 = Owner can write 100 = Owner can execute 040 = Group can read</p> Signup and view all the answers

Study Notes

Shell Scripting

  • Bash offers several pre-defined variables and functions for creating shell scripts.
  • Arguments are passed to a script using the format ./myscript args1 args.
  • The $1 variable represents the first argument, $2 the second, and so on.
  • Conditions can be used to execute code blocks based on certain conditions.
    • The if statement evaluates a test condition.
    • If the condition is true, then the code inside the then block is executed.
  • Switch Case statements provide a way to execute different code blocks based on the value of a variable.
    • The case statement uses pattern matching to compare the value of a variable to different patterns.
    • Each pattern is followed by a right parenthesis and a ) and a corresponding code block.
    • The ;; marks the end of each case block.
    • The *) is used as a default case when none of the patterns match.
  • Arrays can be used to store a collection of values.
    • They are declared using the format my_array=("value 1" "value 2" "value 3" "value 4").
    • The values can be accessed using the array name followed by the index in brackets, e.g., echo ${my_array[0]}.
  • For Loops iterate through a list of items.
    • The syntax is: for var in ${list}; do # do something; done.
    • The loop variable (var) takes on each value from the list (${list}) in turn.
  • While Loops execute a code block as long as a condition is met.
    • The code block is executed repeatedly until the condition becomes false.
    • The syntax is: while [[ $condition ]]; do # do something; done.
    • The read command can be used to read input from the user inside a loop.
  • Functions define reusable blocks of code.
    • They begin with the function keyword followed by the function name and parentheses.
    • The code within the function is executed when the function is called by its name.
  • Debugging
    • The set -x command to activate debug mode and display each executed command step-by-step in the terminal
    • Alternatively, you can execute a script in debug mode using the command bash -x ./my_script.
  • Rules for Scripting
    • Documentation is essential. Comments are denoted using the # symbol.
    • Shell Specification should be defined on the first line using #!/bin/bash.
    • Modular Programming should be adhered to for better organization. The KISS principle and UNIX philosophy are recommended.
    • Clean Code is important for readability and maintainability.

Unix File System

  • The Unix File System presents a hierarchical structure, where files are organized in a tree-like fashion.
    • The root directory of the file system is represented by /.
    • Paths are separated by /.
  • Important directories include:
    • /bin : Contains binary executables for the system.
    • /dev/: Contains device files for accessing various hardware devices.
    • /etc/: Contains configuration files for the system and its applications.
    • /home/: Contains home directories for users.
    • /lib/: Contains libraries used by programs.
    • /tmp/: Contains temporary files.
    • /usr/: Contains system-wide files and programs for users.

File Permissions

  • Unix allows for setting specific permissions for accessing each file in the file system.
  • Permissions define the read (r), write (w), and execute (x) abilities for different user groups.
  • Permissions are represented using an octal notation:
    • 4: read access
    • 2: write access
    • 1: execute access
    • 7 : read, write, and execute access (4+2+1).
  • The ls -al command displays file permissions in a long format, showing the following information:
    • The number of references to the file.
    • The size of the file in bytes.
    • The date and time of the last modification.
    • The user who owns the file.
    • The group that owns the file.
    • The name of the file or directory.
  • The first character of the file permission string (e.g., d, -, l) indicates the file type:
    • d: Directory
    • -: Regular File
    • l: Symbolic Link
  • The chmod command modifies file permissions.
    • The first three digits of the permission code represent the access rights for the following user groups:
      • 400: Owner
      • 040: Group
      • 004: Other
  • Example:
    • chmod 600 ~/.bashrc sets the permissions for the ~/.bashrc file to allow only the owner to read and write.
  • Removing a Directory
    • The rm command is used to remove files or directories.
    • The -rf options are used to remove files and directories recursively, including their contents.
    • The command rm -rf /home/rakeller/ will attempt to remove the directory /home/rakeller/ and all its contents.
  • Error Messages - Removing Directories
    • Removing a directory using rm -rf /home/rakeller/ might fail if the user does not have sufficient permissions to remove the directory, or if the directory is not empty.

Studying That Suits You

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

Quiz Team

Related Documents

Description

This quiz explores fundamental concepts in shell scripting, including predefined variables, argument passing, conditional statements, and the use of arrays. Test your knowledge on how to use 'if' conditions and 'case' statements effectively in Bash scripting.

More Like This

Bash Shell Scripting Parameters Quiz
15 questions
Shell Script Basics Quiz
10 questions

Shell Script Basics Quiz

StylishSpessartine avatar
StylishSpessartine
Bash Scripting 3
40 questions

Bash Scripting 3

RevolutionaryFern2779 avatar
RevolutionaryFern2779
Shell Scripting Grundlagen
40 questions

Shell Scripting Grundlagen

RevolutionaryFern2779 avatar
RevolutionaryFern2779
Use Quizgecko on...
Browser
Browser