This exercise guides you through creating a Bash script that creates a directory, changes into it, and demonstrates conditional execution using logical operators.

Steps:

  1. Create a Directory:

    • Use the mkdir command to create a new directory named hw2 in your root directory.
  2. Write the Bash Script:

    • Open a text editor and copy the following code into a new file named hw2.sh:
#!/bin/bash

# Assign the first argument as the directory name
dir_name=$1

# Create the directory and change into it
mkdir '$dir_name' && cd '$dir_name'

# Run the commands and observe the results
false || echo 'Oops, fail'

true || echo 'Will not be printed'

true && echo 'Things went well'

false && echo 'Will not be printed'

true ; echo 'This will always run'

false ; echo 'This will always run'
  1. Make the Script Executable:

    • Open your terminal and run the command chmod +x hw2.sh to make the script executable.
  2. Run the Script:

    • Execute the script by typing ./hw2.sh <directory_name> in your terminal, replacing <directory_name> with the desired directory name.

Code Breakdown:

  • #!/bin/bash: This line tells the system to use the Bash interpreter to execute the script.
  • dir_name=$1: Assigns the first argument passed to the script to the variable dir_name.
  • mkdir '$dir_name' && cd '$dir_name': Creates the directory named $dir_name and changes the current directory into it. The && operator ensures that the cd command only executes if the mkdir command succeeds.
  • false || echo 'Oops, fail': This line demonstrates the || (OR) operator. The false command always returns false. If false is true, the echo command will not be executed. In this case, the echo command will execute and print 'Oops, fail'.
  • true || echo 'Will not be printed': The true command always returns true. As the true command is true, the echo command will not execute.
  • true && echo 'Things went well': This line uses the && (AND) operator. Since true is true, the echo command will execute and print 'Things went well'.
  • false && echo 'Will not be printed': As false is false, the echo command will not execute.
  • true ; echo 'This will always run': The semicolon (;) operator executes the commands sequentially, regardless of whether the previous command succeeded or failed. Therefore, the echo command will always execute.
  • false ; echo 'This will always run': Similar to the previous line, the echo command will always execute.

Key Concepts:

  • Variable Assignment: Bash uses the syntax variable_name=value to assign values to variables.
  • Directory Manipulation: mkdir creates a directory, and cd changes the current directory.
  • Logical Operators:
    • && (AND): Executes the second command only if the first command is successful.
    • || (OR): Executes the second command only if the first command fails.
  • Sequential Command Execution: The semicolon (;) executes commands one after another, regardless of the success or failure of previous commands.

Experiment:

  • Modify the script to include other Bash commands and experiment with different conditional statements using the &&, ||, and ; operators.
  • Explore other Bash commands related to directory manipulation, file management, and process control.
Bash Scripting Exercise: Directory Creation and Conditional Execution

原文地址: https://www.cveoy.top/t/topic/jqM6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录