Shell script to create a file if it doesn't exist?
63
votes
6
answers
219966
views
I need to create a shell script that checks for the presence of a file and if it doesn't exist, creates it and moves on to the next command, or just moves on to the next command. What I have doesn't do that.
#!/bin/bash
# Check for the file that gets created when the script successfully finishes.
if [! -f /Scripts/file.txt]
then
: # Do nothing. Go to the next step?
else
mkdir /Scripts # file.txt will come at the end of the script
fi
# Next command (macOS preference setting)
defaults write ...
Return is
line 5: [!: command not found
mkdir: /Scripts: File exists
No idea what to do. Every place a Google search brings me indicates something different.
Asked by jrw
(739 rep)
Nov 15, 2017, 07:30 PM
Last activity: Jul 2, 2025, 01:51 PM
Last activity: Jul 2, 2025, 01:51 PM