Concept Flow - File existence checks
Start
Check if file exists
End
The script checks if a file exists, then prints a message based on the result.
filename="test.txt" if [ -e "$filename" ]; then echo "File exists" else echo "File does not exist" fi
| Step | Action | Condition | Result | Output |
|---|---|---|---|---|
| 1 | Set filename to 'test.txt' | N/A | filename='test.txt' | |
| 2 | Check if file exists with [ -e "$filename" ] | [ -e "test.txt" ] | True if file exists, False if not | |
| 3 | If condition True | File exists? | Yes | echo 'File exists' |
| 4 | Print output | N/A | N/A | File exists |
| 5 | If condition False | File exists? | No | echo 'File does not exist' |
| 6 | Print output | N/A | N/A | File does not exist |
| Variable | Start | After Step 1 | Final |
|---|---|---|---|
| filename | undefined | test.txt | test.txt |
File existence check in bash: Use [ -e "$filename" ] to test if a file exists. If true, run commands in then-block. Else, run commands in else-block. Always quote variables to handle spaces. Print messages to confirm existence.