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.
Related
I'm trying to change the directory name where the BASH script resides in.
I've built following function inside this script:
function rename_dir(){
echo "Gimme new dir name: "
read new_dir_name
mv ../${PWD##*/} ../$new_dir_name
cd ../$new_dir_name
Basically it does the job.
When I run it manually by calling ./script.sh it changes the directory name but since it's running in a subshell - I need to do:
cd ..
cd New_Dir_name
And that's fine and understandable. No biggie.
When I source the script it naturally does the job but when I rename the directory the script loses the 'X' attribute and I need to chmod it again.
Any other approach to overcome this? Anyone has any other ideas how to get it done properly?
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.
I am using Bash on repl.it. I am making my own command line, and I would like to know how to run a script from a folder called commands. Basically, I would like to make a file run from a folder. How can I do this?
Link to my repl
Could you just run it like this?
sh commands/${input}.sh
Also you should be able to push/pop dir to go into/out of directories.
pushd commands
# adds cwd into a stack and goes to <dir> commands/
sh $input.sh
popd
# goes back to last dir in stack.
I've been working on a script but always with a fixed directory (/opt/mw/script).
I need to change that to be able to execute the script from any directory.
I think I will need to add a "." at the beginning of the line to be able to execute the script?
For example ./mw/script
Is this correct?
Thanks
You already can execute that script from any directory with that absolute file path. It's the relative file paths (that start with ./ or ../) that can only be executed from a specific directory.
You need to add the "." if the script is in the current directory (e.g. ./script), but it is optional if the script is already inside another directory (e.g. mw/script).
Also notice that if your script contains relative references to other files and directories, you may need to use this trick to properly refer to them from any directory.
For instance, consider the following script using absolute paths:
#!/bin/bash
# lists all files in this directory
ls /opt/mw
If it is converted to relative paths as follows:
#!/bin/bash
# lists all files in this directory
ls mw
Then this script will only work if you run it from its own directory (e.g. cd /opt/ && ./script).
But then you can generalize it like this:
#!/bin/bash
SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# lists all files in this directory
ls $SCRIPT_DIR/mw
Now the script works even when executed from another directory.
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