This is a simple script I wrote to quickly open a new file from the command line prompt. I have workon
as an alias in my shell profile, and I use this any time I want to test a snippet or start a new script, but it can also be used to generate Word documents or Excel files.
#!/bin/bash
#set -x
#Check for an empty argument
if [ "${1}" = '' ]; then
echo "Provide a file name to create or open a file."
exit 1
fi
#Check if the file already exists
if [ -e "${1}" ]; then
open "${1}"
exit 0
fi
#Create the file
touch "${1}"
#Check for executable file extensions.
if [[ "${1}" == *".sh" ]] || [[ "${1}" == *."py" ]] ; then
chmod +x "${1}"
fi
if [[ "${1}" == *".sh" ]]; then
cat >> "${1}" << SCRIPTGOESHERE
#!/bin/zsh
set -x
SCRIPTGOESHERE
fi
open "${1}"
exit 0
What Does This Do?
This script will create a new empty file and open it in the default application for the file type. If the file already exists, it just opens it.
If the file type is .sh
, then the #!/bin/zsh
line and set -x
are automatically added. The file is also set to be executable.
Caveats
- This script doesn’t create folders. This could be added easily enough.
- The script doesn’t error check itself