1

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.

6
  • 1
    Remove the C++ tag. It isn't pertinent. Commented Jul 28, 2020 at 19:11
  • 1
    Why do you need the full path ? touch and nvim works on files in the current direcctory. (relative file names). Commented Jul 28, 2020 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. Commented Jul 28, 2020 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). Commented Jul 28, 2020 at 19:38
  • 2
    Off topic: Look at stackoverflow.com/q/2953081/3220113 for an easier way to write a number of lines in a file. Commented Jul 28, 2020 at 19:47

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
OK my bad. Hopefully this is helpful for others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.