0

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.

Geno C
  • 1,401
  • 3
  • 11
  • 26
Shourya Bansal
  • 324
  • 2
  • 16
  • 1
    Remove the C++ tag. It isn't pertinent. – sweenish Jul 28 '20 at 19:11
  • 1
    Why do you need the full path ? `touch` and `nvim` works on files in the current direcctory. (relative file names). – dash-o Jul 28 '20 at 19:15
  • 1
    What do you mean by the directory it is "called from"? If you mean the working directory of the process that started it (i.e. the working directory of the shell that it's run from), then it's easy: the script inherits that same working directory, so you can just use relative paths in the script and they'll automatically be resolved relative to that directory. – Gordon Davisson Jul 28 '20 at 19:17
  • 1
    BTW, I always recommend against using `echo -e` or `-n` -- some versions of `echo` just print those as part of their output, and I've had to rewrite scripts because an OS update changed that behavior. `printf` is much better (but more complicated to use right). – Gordon Davisson Jul 28 '20 at 19:38
  • 1
    Simply: `$(pwd)` give you the current directory. – Manuel Jul 28 '20 at 19:40
  • 2
    Off topic: Look at https://stackoverflow.com/q/2953081/3220113 for an easier way to write a number of lines in a file. – Walter A Jul 28 '20 at 19:47

1 Answers1

2

You can use

    `dirname $0`
in your bash script. This would take the value of the directory where your script is placed. You would be able to execute your bash from anywhere as long as you have the right access.

Good practice:

    #initiate the dir name 
    dir=`dirname $0`
    
    #initiate the name of your file
    file="my_filename"
    
    #use as needed:
    # create file in current bash directory 
    touch $dir/$file
    # append
    echo -e "my_cpp_script" >> $dir/$file
    #call
    cpp $dir/$file

naoul
  • 21
  • 4
  • This doesn't actually answer the question. This puts the file where the script is placed, which is not what the questions asked. The comments on the question are right, there is no need to specify a directory. – Shourya Bansal Jul 30 '20 at 17:38
  • OK my bad. Hopefully this is helpful for others. – naoul Jul 31 '20 at 22:43