base01.sh - File Existence Check

This simple shell script checks if a file named test01.txt exists in the current directory.

Description

The script uses the -e option in a conditional statement to determine if the specified file exists. If the file exists, it prints "test01.txt is exist"; otherwise, it prints "test01.txt is not exist".

Environment

Setup and Execution

Follow these steps to set up and run the script:

  1. Navigate to the directory where you want to create the script: cd ~/RZSH103
  2. Ensure Bash is available: which bash >> /usr/bin/bash
  3. Create the script file base01.sh and paste the code below.
  4. Make the script executable: sudo chmod +x base01.sh
  5. Run the script: ./base01.sh

Code

#!/bin/bash if [ -e ./test01.txt ]; then echo "test01.txt is exist" else echo "test01.txt is not exist" fi exit

Expected Result

If test01.txt does not exist in the current directory, the script will output:

test01.txt is not exist

If test01.txt does exist in the current directory, it will output:

test01.txt is exist

Explanation of Key Concepts

For Beginners

This script is a basic example of how to check for file existence in Bash. It's a fundamental skill for writing more complex shell scripts. You can modify the script to check for other files or perform different actions based on the file's existence.