I am trying to make a script to automate the creation of files and input of certain data in the files, sort of like a template. For example, if the script is called cpp
(Just an example, not the actual script name), when I run cpp <filename>
it should create a file in the current directory without me having to tell it what the directory is. Right now, I have to hard code a root directory into the script. The basic functionality of the script is something like this (I have validations and stuff, but this is the main function):
touch "$FILE_PATH" (this is the part that I have to hard code)
echo -e "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n\t\n}" > "$FILEPATH"
nvim "$FILE_PATH"
In this situation, I have to declare the file path to be FILE_PATH="$HOME/<a specific directory>/$1.cpp"
, meaning when I call on the script, I have to pass an argument relative to the hard-coded location, like perhaps cpp automation/file_manager/shell/apartment
. Is there a way for a shell script to automatically find the directory it is called on, so it can be more dynamic and flexible (Go to the directory automation/file_manager/shell/apartment and call cpp apartment
to get the same result)? Kind of like vim or neovim, where it creates a file in the current directory.