Soru

Zorluk: ZorBasic Scripting Languages and Constructs

A systems administrator is writing a Bash shell script (.sh) on a Linux workstation to parse log files located in a specific directory. The script must define a variable holding the target path /var/log/audit and reference that variable within a loop construct. Which TWO of the following scripting statements and syntax rules must the administrator follow for this Bash script to execute successfully? (Select TWO.)

  1. Define the variable using the syntax LOG_DIR="/var/log/audit" with no whitespace surrounding the assignment operator.Cevap
  2. Reference the stored variable value using LOGDIRorLOG_DIR or {LOG_DIR} syntax when building script constructs.Cevap
  3. C
    Define the variable using the syntax set LOG_DIR = "/var/log/audit" to register it within the current session environment.
  4. D
    Iterate through files using the construct for %f in ($LOG_DIR/*) do to evaluate each item in the directory.

Cevap

Defining the variable without spaces around the equals sign (LOG_DIR="/var/log/audit") and referencing its value using a dollar sign prefix (LOGDIRorLOG_DIR or {LOG_DIR}).
In Bash shell scripting, variables are declared without whitespace surrounding the assignment operator (e.g., LOG_DIR="/var/log/audit"). To expand and evaluate the variable's value in loops or commands, the variable name must be prefixed with a dollar sign (LOGDIRorLOG_DIR or {LOG_DIR}).

Adım Adım Çözüm

1
Analyze Bash variable assignment requirements
Bash syntax strictly enforces no whitespace around the assignment operator (=) when declaring user variables.
Including spaces causes the shell to misinterpret the variable name as an executable command.
2
Analyze Bash variable expansion syntax
Accessing the stored value of LOG_DIR requires referencing it as LOGDIRorLOG_DIR or {LOG_DIR}.
The dollar sign indicates to the shell interpreter that the variable name should be replaced with its assigned literal value.
3
Evaluate distractor syntax rules
The 'set' keyword with spaces and the 'for %f in (...) do' structure belong to Windows Command Prompt Batch scripting.
Mixing Windows CMD syntax into a Bash script causes execution and syntax errors.

Anahtar Kavram

Bash Environment Variable Assignment and Expansion Syntax
Tahmini Süre:1m 30s
Bu soruyu puanla