Unix Command Line Current Directory - bash

Let's say I have a folder structure like this:
mygame
play.command
otherstuff
code.py
I want play.command to cd to mygame no matter where mygame is and run code.py from there. How do I do that?

Put that code.py in /bin folder. Then you can access it from anywhere.

From your question it seems you want to go to the parent directory. You do this via
cd ..
otherstuff/code.py
(with the space after 'cd')
or call code.py more directly with
../otherstuff/code.py

I think you are trying something like this,
You have two ways to run scripts:
absolute path:
python /path/to/script.py
none-absolute path:
cd ../to/your/directory && python script.py

First cd to the directory where the script is stored in, and then execute the python script
#!/bin/bash
cd "$(dirname "${BASH_SOURCE[0]}")"
otherstuff/code.py

Related

executing a python code located in a subfolder

I have a directory called NHOME. In this directory there is a folder named subFolder. It's like this:
/NHOME/subFolder
In NHOME directory I'm executing a bash script which intends to execute a python code located in subFolder. I have problem doing this. I tried something like this:
./subFolder/file.py
but it didn't work. Is this possible?
Use pwd to get the script's location instead of the relative ./
pythonScript="$PWD/subFolder/file.py"
To run:
#!/bin/bash
"$PWD"/subFolder/file.py
Note: The " around $PWD are needed if the path contains spaces ()

Run make file in subdirectory using shell script

I need to run make file_name command in a subdirectory using shell
script and then cd .. out of that subdirectory and run the remaining commands in the main directory.
You can run make from the parent directory without switching to the subdirectory first:
make -C subdir file_name
Assuming the script is initially running in "the main directory", just cd to the subdir in a subshell. eg
#!/bin/sh
# do stuff in main directory
( cd subdir; make file_name ) # use a subshell
# now run more commands in the main directory
You can also use pushd and popd, or try storing $(pwd), or just use cd .., but the subshell is usually the cleanest solution.

Change directory in bash script in windows

How can I change my working directory in bash script in windows. I have
~dp0 = C:\test\docker\windows and I want to change my directory to C:\test\build
So it means 2 levels up and then int o build folder
Thanks
Since C:\ is mounted by default into /mnt/c this will work.
Create a .bashrc in your home path by following command:
echo "BUILDDIR=/mnt/c/test/build" >> ~/.bashrc;source ~/.bashrc
cd $BUILDDIR
# Do your work below for example ./configure.
./configure
On my system in Git bash the C:\ root is just /c/ and other dirs from there are whatever they are, so it would be cd /c/test/build/.
You could also still say cd ../../build/.
Good luck.
To change the directory using a bash script is just like you would using normal bash.
cd "C:/test/build"
echo "You're now in the folder, do what you will."
Save the file as .sh and it can be used as such.
Note, when navigation your folders using a bash script remember the directory you'll be starting from is always the home directory.

How to run an executable file from root on Ubuntu 16.04?

My problem is fairly simple.
I have a file that I want to run, say x.sh.
From my root location I run something like.
$ cd Desktop/./x.sh -args
It prompts me to go to the directory and then run the file.
And as prompted the following works
$ cd Desktop
$ ./x.sh -args
Now, Am I missing something here or is this just the way Ubuntu is designed to work?
You don't use cd while executing a file. Directly execute it on the prompt: Desktop/x.sh -args
You're missing something. You can either cd to the directory and run the file from that directory or you can run the file from the absolute path or relative path, provided the user you're trying to run it with has access to that directory. The following should work at the level you were trying to do cd Desktop/./x.sh -args:
./Desktop/x.sh -args

Run .command–file in the folder it's located?

I have a file called execute.command. When I execute it by opening it. It runs from my users folder. I would like it to run in the folder where it is located. is that possible?
You must change the script to change working directory to script-location directory:
#!/bin/bash
cd "$(dirname "$0")"
More answers here : Bash script: set current working directory to the directory of the script
I added cd dirname $0 to the top of the command line. This will cd you into the correct folder. Here are more details.
http://hints.macworld.com/article.php?story=20041217111834902

Resources