This simple shell script checks if a file named test01.txt exists in the current directory.
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".
Follow these steps to set up and run the script:
cd ~/RZSH103which bash >> /usr/bin/bashbase01.sh and paste the code below.sudo chmod +x base01.sh./base01.sh
#!/bin/bash
if [ -e ./test01.txt ]; then
echo "test01.txt is exist"
else
echo "test01.txt is not exist"
fi
exit
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
#!/bin/bash: This is the shebang line, which tells the system to execute the script using the Bash shell.if [ -e ./test01.txt ]; then ... else ... fi: This is a conditional statement. -e checks if the specified file exists.echo: This command prints text to the console.exit: This command terminates the script.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.