CEST-CE Term_2 GNU/LINUX (Week 9)
30 Questions
1 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

How is a shell variable assigned a value?

  • Using the equal sign without spaces around it (correct)
  • Using the dollar sign followed by the variable name
  • Using the equal sign with spaces around it
  • Using the colon followed by the value
  • What is the effect of using quotes around variable expansions?

  • It makes the variable expansion mandatory
  • It prevents word-splitting and GLOB expansion (correct)
  • It allows immediate variable expansion
  • It leaves the variable's value unchanged
  • What happens if an undefined variable is expanded without the nounset option?

  • The variable expands to an empty value (correct)
  • The variable is ignored during the operation
  • The shell throws an error
  • The shell sets the variable to zero
  • Which statement about local and environment variables is correct?

    <p>Environment variables are inherited by child processes</p> Signup and view all the answers

    Which of the following represents a best practice for variable naming in shell scripts?

    <p>Use descriptive names to clarify variable purpose</p> Signup and view all the answers

    What does the shell do if a command name is not recognized as a built-in command?

    <p>It looks for the executable file in directories listed in the PATH variable</p> Signup and view all the answers

    How are environment variables formatted in the PATH variable?

    <p>Each path is separated by a colon</p> Signup and view all the answers

    How does the shell determine which command to execute when multiple matches are found in the PATH?

    <p>It executes the first match found, scanning from left to right.</p> Signup and view all the answers

    What happens when a command is not found in any of the directories listed in PATH?

    <p>The shell returns a 'command not found' error.</p> Signup and view all the answers

    Why should the current directory (.) not be included in PATH?

    <p>It poses significant security risks.</p> Signup and view all the answers

    What is the primary purpose of the 'which' command?

    <p>To identify which directory in PATH contains a command.</p> Signup and view all the answers

    How should PATH be modified in a shell script to ensure commands are executed correctly?

    <p>Use export PATH= $PATH:/new/directory for safe inclusion.</p> Signup and view all the answers

    What is a key best practice regarding the use of PATH?

    <p>Understand and customize your PATH to avoid security risks.</p> Signup and view all the answers

    What should you do to execute a command from the current directory safely?

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

    What is the primary difference between interactive and scripted use of shells?

    <p>Scripted shells execute commands without user interaction.</p> Signup and view all the answers

    What is the purpose of the shebang (#!) in a script?

    <p>It indicates the interpreter to be used for execution.</p> Signup and view all the answers

    Which command is used to make a script executable?

    <p>chmod +x script.sh</p> Signup and view all the answers

    What happens when a script is executed in a shell process?

    <p>It runs in a new shell process independent of the interactive shell.</p> Signup and view all the answers

    How can you handle multiple command-line arguments safely within a script?

    <p>Use '$@' to reference all positional parameters.</p> Signup and view all the answers

    Which method can be used for redirecting output in a script?

    <p>Using pipes.</p> Signup and view all the answers

    Which of the following is a best practice for maintaining script security?

    <p>Manage input validation and permissions properly.</p> Signup and view all the answers

    What syntax is used for shell command substitution?

    <p>$(command)</p> Signup and view all the answers

    What will the command echo $(( 5 + 3 * 4 )) output?

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

    What is the exit status of a successfully executed command in Unix/Linux?

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

    How can you explicitly set the exit status of a shell script?

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

    Which tool is used for decimal point arithmetic in shell scripts?

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

    What will the command echo '22/7' | bc -l output?

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

    When a script finishes execution without any commands, what happens to its exit status?

    <p>It defaults to 0.</p> Signup and view all the answers

    Which operation is NOT supported in shell arithmetic?

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

    What command retrieves the exit status of the last executed command?

    <p>$?</p> Signup and view all the answers

    Study Notes

    Shell Variables

    • Variables store text or numeric values in shell scripts.
    • Assign values using = without spaces around it.
    • Use \$ to expand variables in commands or scripts.
    • Single quotes or backslashes prevent immediate variable expansion.
    • Always double-quote variables to avoid word-splitting and GLOB expansion.
    • Undefined variables expand to nothing unless the nounset option is set.
    • Append to a variable with the syntax: \$VARNAME=\$VARNAME:newtext.
    • Local variables are specific to the current shell; exported variables are inherited by child processes.
    • Common pre-set environment variables include \$PATH, \$USER, \$HOME, \$SHELL, \$TERM, and \$\$.
    • Best practices include using descriptive names and quoting variable expansions to prevent unexpected behavior.

    Search Path

    • The shell identifies commands by the first token after processing I/O redirection.
    • Built-in commands, like echo and cd, run directly without searching the PATH.
    • If a command name isn't built-in, the shell searches executable files in directories listed in the PATH environment variable, a colon-separated list.
    • Directly executing a command with its full pathname bypasses PATH searching.
    • The first match found among multiple directories in PATH is executed.
    • A "command not found" error occurs if a command isn't located in PATH.
    • Avoid including the current directory (.) or adjacent colons (::) in PATH for security reasons; use ./ to run commands explicitly.
    • The which command displays the directory containing a command, while whereis finds commands and their man pages ignoring PATH.
    • Modify PATH with the command: export PATH=\$PATH:/new/directory, ensuring proper colon separation.
    • Set PATH at the beginning of shell scripts to ensure correct command execution.

    Shell Scripts

    • Shell scripting automates tasks by creating scripts with lists of commands executed by a shell program like Bash.
    • Scripts can be interactive or text-file-based for greater flexibility.
    • Execute scripts by calling the shell with the script file (bash script.sh or sh script.sh) and make them executable with chmod +x script.sh.
    • Scripts run in their own shell process, so changes do not affect the interactive shell.
    • A standard script header includes a shebang (#!/bin/bash -u), sets PATH, and specifies umask.
    • Use positional parameters (e.g., \$1, \$2) to access command-line arguments; shift to process them sequentially, and "\$@" for multiple arguments.
    • Redirection (>, >>) and piping (|) enable input/output manipulation; for and while loops allow iteration.
    • Ensure script security by validating inputs and managing permissions with chmod.

    Command Substitution, Arithmetic, Exit

    • Shell command substitution uses \$(command) to output command results into strings or variables (e.g., echo "Your userid is \$(whoami)").
    • Integer arithmetic is performed using \$(()), supporting basic operations like addition and multiplication (e.g., echo \$(( 2 + 2 * 9 )) evaluates to 20).
    • For decimal calculations, bc is used with quoted expressions (e.g., echo '22/7' | bc -l outputs 3.14285714285714285714).
    • Commands return an exit status, ranging from 0 (success) to 255 (failures); captured with \$?.
    • A script inherits the exit status of the last executed command, and can set an explicit exit status with exit N (e.g., exit 99 sets it to 99).

    Studying That Suits You

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

    Quiz Team

    Related Documents

    Week 9.docx

    Description

    Dive into shell variables used in scripting with this Week 9 quiz. Learn about variable assignments, expanding variables, and the significance of quoting. Test your understanding of shell scripting concepts and best practices.

    More Like This

    Scripting and Shell Commands Quiz
    20 questions
    Bash Shell Scripting Parameters Quiz
    15 questions
    Environment Variables in Shell Scripting
    10 questions
    Shell Scripting Essentials
    78 questions
    Use Quizgecko on...
    Browser
    Browser