You can check if a file exists in a shell script using the test command or the [ ] syntax. Here’s a simple example in a shell script:
#!/bin/bash
FILENAME="CharacterSpace.java"
if [ -e "$FILENAME" ]; then
echo "File $FILENAME exists."
else
echo "File $FILENAME does not exist."
fi
Steps to Use the Script:
-
Create a new script file:
nano check_file.sh -
Copy and paste the above code into the file.
-
Save and exit the editor.
-
Make the script executable:
chmod +x check_file.sh -
Run the script:
./check_file.sh
This script will check if CharacterSpace.java exists and print the appropriate message.
