Bash Scripting Exercise: Directory Creation and Conditional Execution
This exercise guides you through creating a Bash script that creates a directory, changes into it, and demonstrates conditional execution using logical operators.
Steps:
-
Create a Directory:
- Use the
mkdircommand to create a new directory namedhw2in your root directory.
- Use the
-
Write the Bash Script:
- Open a text editor and copy the following code into a new file named
hw2.sh:
- Open a text editor and copy the following code into a new file named
#!/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'
-
Make the Script Executable:
- Open your terminal and run the command
chmod +x hw2.shto make the script executable.
- Open your terminal and run the command
-
Run the Script:
- Execute the script by typing
./hw2.sh <directory_name>in your terminal, replacing<directory_name>with the desired directory name.
- Execute the script by typing
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 variabledir_name.mkdir '$dir_name' && cd '$dir_name': Creates the directory named$dir_nameand changes the current directory into it. The&&operator ensures that thecdcommand only executes if themkdircommand succeeds.false || echo 'Oops, fail': This line demonstrates the||(OR) operator. Thefalsecommand always returns false. Iffalseis true, theechocommand will not be executed. In this case, theechocommand will execute and print 'Oops, fail'.true || echo 'Will not be printed': Thetruecommand always returns true. As thetruecommand is true, theechocommand will not execute.true && echo 'Things went well': This line uses the&&(AND) operator. Sincetrueis true, theechocommand will execute and print 'Things went well'.false && echo 'Will not be printed': Asfalseis false, theechocommand 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, theechocommand will always execute.false ; echo 'This will always run': Similar to the previous line, theechocommand will always execute.
Key Concepts:
- Variable Assignment: Bash uses the syntax
variable_name=valueto assign values to variables. - Directory Manipulation:
mkdircreates a directory, andcdchanges 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.
原文地址: https://www.cveoy.top/t/topic/jqM6 著作权归作者所有。请勿转载和采集!