👉 https://amzn.to/4aLHbLD 👈 You’re literally one click away from a better setup — grab it now! 🚀👑
As an Amazon Associate I earn from qualifying purchases. Unix & Linux: Shell script to create a file if it doesn't exist?
The Question: 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.
Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful
== This solution helped 6 people ==
You have a syntactical error. You require spaces before and after the [ and ].
== This solution helped 31 people ==
You are getting the error because there is no space between [ and ! however
there are also some flaws in your code. First you are checking if the file does
not exist, and if not you are doing nothing. If the file DOES exist you are
making a directory (but not doing anything to create the file).
You also don't need the null operation, you should be able to simply do:
#! /bin/bash -
if [[ ! -e /Scripts/file.txt ]]; then
mkdir -p /Scripts
touch /Scripts/file.txt
fi
[command2]
This is checking if /Scripts/file.txt does not exist it will create the /
Scripts directory and then the file.txt file. You could also check for the
existence of the directory separately if you wanted. Additionally notice I am
using -e instead of -f as you asked simply to check for the existence of a file
which is what -e will do where -f checks that it is a "regular file" http://
tldp.org/LDP/abs/html/fto.html
== This solution helped 3 people ==
#!/bin/bash
Check for the file that gets created when the script successfully finishes.
CHECKFILE="/Scripts/file.txt"
CHECKDIR=$( dirname "$CHECKFILE" )
The directory with the file must exist
mkdir -p "$CHECKDIR"
if [ ! -f "$CHECKFILE" ]; then
What to do if the file is not there
fi
touch "$CHECKFILE"
The above assumes that there are no "tricks" such as creating a directory
called /Scripts/file.txt (which could be a way of forcing the script to always
enter the if branch). If the "file" is a directory, the -f test will fail and
the touch command will not change anything.
my approach
#!/bin/sh
input might contains spaces and other characters
FILEPATH="/tmp/some where/the file.blah"
extract the file + dir names
FILE="`basename "${FILEPATH}"`"
DIR="`dirname "${FILEPATH}"`"
create the dir, then the file
mkdir -p "${DIR}" && touch "${DIR}/${FILE}"
show result
ls -l "$FILEPATH"
output
./dofile.sh
rw-r--r- 1 jmullee jmullee 0 Nov 15 21:23 /tmp/some where/the file.blah
.
== This solution helped 48 people ==
Possibly simpler solution, no need to do explicit tests, just use:
mkdir -p /Scripts
touch /Scripts/file.txt
If you don't want the "modification" time of an existing file.txt to be changed
by touch, you can use touch -a /Scripts/file.txt to make touch only change the
"access" and "change" times.
With thanks & praise to God, and with thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free... | Images: https://stocksnap.io/license & others | With thanks to user xenoid (https://unix.stackexchange.com/users/..., user Roger Lipscombe (https://unix.stackexchange.com/users/..., user LSerni (https://unix.stackexchange.com/users/..., user jrw (https://unix.stackexchange.com/users/..., user jmullee (https://unix.stackexchange.com/users/..., user jesse_b (https://unix.stackexchange.com/users/..., user James K. Lowden (https://unix.stackexchange.com/users/..., user DopeGhoti (https://unix.stackexchange.com/users/..., and the Stack Exchange Network (http://unix.stackexchange.com/questio.... Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything is amiss at Roel D.OT VandePaar A.T gmail.com.