0

I have been looking into bash and shell and have been trying to figure out how Git is able to run its C script in any directory. I have recently fiddled with environment variables, although this just redirects you to run a script in the location of that file, plus you have the $. How is Git able to integrate its scripts into the terminal? How would I be able to do the same with an arbitrary script in python?

It is really hard to find any information without knowing the correct terminology although here's my attempt of mimicking such a command line system as git.

I start with a command file in a folder in my home directory

something.command 

Which on the inside directs me to a python script. e.g

cd
cd python_script_location
python3 script.py

Then I make this an environment variable through editing the .bash_profile.

export pyinit=/Users/Charlkie/vcscmd/pyinit.command

This although will just run a script in another directory. How can I make a terminal command that runs a python script in the current directory? Any help would be greatly appreciated :)

LuckyPants
  • 123
  • 1
  • 9
  • This post should be useful to you: https://stackoverflow.com/questions/1522564/how-do-i-run-a-python-program – meeza May 08 '18 at 08:56
  • Create softlink to required directory of your script and add that to `PATH` and run – ntshetty May 08 '18 at 09:04

2 Answers2

0

There is no requirement for scripts to switch working directories.

In fact, well-written scripts should, as a rule, not need to switch directories at all.

If the script requires access to a configuration file or something, a common set of arrangements is to allow the user to configure the file's location via an environment variable, then fall back to a system-wide standard location, and then fall back to the current directory, and as a last resort, perhaps examine the script's location via argv[0] (which is $0 in shell scripts).

In concrete terms, something.command should probably look like

exec python3 $HOME/python_script_location/script.py "$@"

... in which case you might as well make $HOME/python_script_location/script.py executable with a proper shebang (in this case usually #!/usr/bin/env python3) and maybe ln -s $HOME/python_script_location/script.py $HOME/bin (assuming $HOME/bin is already on your PATH) or maybe alias something.command=$HOME/python_script_location/script.py if you only need it interactively and don't otherwise have a directory in your PATH for your own personal scripts.

tripleee
  • 175,061
  • 34
  • 275
  • 318
-1

Add a symlink /usr/bin pointing at your script. Check this question how to do it: How to symlink a file in Linux? Use which command to validate if the system can see it.

  • I think I have worded question wrong, I don't want to know how to link file. I want to know to write a python program in the command line, like how git init is able to create a dot directory inside the current terminal directory. For example, let's say I have a python script that copies all files in the current terminal directory into a newly created dot directory. I want to create a terminal command say that in whatever directory will run this script. – LuckyPants May 08 '18 at 09:54
  • 1. create your script in any directory, like /home/user/scripts 2. name it "copy" 3. start it with your python path preceded by shebang, for example #!/usr/bin/python3 to let bash know which interpreter to use 4. write your script 5. cd to /usr/bin 6. create symlink to your scirpt ln -s /home/scripts/copy copy 7. call "copy" from where you want, it will execute in that directory – Bendegúz Szatmári May 09 '18 at 11:17