Run a command with relative path from symlink directory - bash

I've got a directory structure like this:
bin
drush
build -> vendor/drupal/core
vendor
drupal
core
So as you see, build is a symlink to the core directory.
drush is an executable file. I need to be able to cd into build and call drush from there.
But if I do this:
cd build
../bin/drush
This doesn't work, because .. points to vendor/drupal directory, because when I cd into build, it in fact goes to vendor/drupal/core, of course.
I know I can call it this way: ../../../bin/drush, but is there some kind of workaround to make .. point to the root, not the actual parent?

You can create an another symlink in the folder "drupal" on the executable drush. So with this way, you can call it from "build".
$ cd vendor/drupal/
$ ln -s absolute_path/bin/drush dummy-drush
$ cd build
$ ./dummy-drush
Solution 1 :
If you don't want to create anything in vendor, maybe it's better to create an alias which allows to you to execute drush (be careful to give the good path).
Solution 2 :
If the directory bin in your example is not the folder "/bin" from the root, you can add it in your PATH in order to execute every executable .
$ export PATH=$PATH:/personal_bin/
$ cd bin
$ ln -s drush dummy-drush

Related

build command need to be executed inside a specific named directory "./package" from child folder or parent folder script terminal (cd) loop or regex

how to write cd NAME_FOLDER and recursively search for it by name and then go to it if necessary (see below for more details).
the NAME_OF_FOLDER is unique so no worried about that.
but the challenging things are:
that it needs to search from the PARENT to the CHILD,
and if it didn't find it that way, search from CHILD to PARENT.
(or you can use any other logic if you think my logic is slow)
example folder
here is an example image of my folder:
possible scenarios
here are some scenarios:
if I am inside
./package -> don't run cd
./test -> cd ./package
./src -> cd ../ && cd ./package
./lib -> cd ../../ && cd ./package
and so on for every deep folder structure
docs:
../ means go from child to parent
./ means go from parent to child
why do I need it?
I am a javascript developer,
and I am using the sveltekit framework
to create a svelte package library.
and I need to publish that library to npm.
and this is ok.
but since I write a lot the same CLI codes.
I am changing the package.json's scripts object,
so I write only one time npm run build to run 6+ commands
{
...
"build": "
svelte-kit sync
&& svelte-package
&& npm version patch
&& cd ./package # only this I need to solve this (the others are solved)
&& npm publish
&& git commit
"
}
this is in one line, but for making you read it easily the code in multiple lines
here how it is in my code:
what does the build command should do?
the command generates a ./package folder always on the root of the folder
(where we can find package.json, .gitignore, ./src, etc...)
increase the number of versions automatically when we use the build command,
then... TODO:
do the script I need to access the ./package folder from every folder I am in now. (like cd ./package)
npm publish
my os?
windows 11 (but using bash with vscode) or also powershell will be good but prefer bash
any other details, I will tell you. thanks
For testing, I created this structure:
test/
package/
src/
lib/
routes/
Then I created that script:
#!/bin/bash
topdir="test"
while [[ $(basename "$(pwd)") != "$topdir" ]]
do
if [[ -d package ]]
then
cd package
pwd
else
cd ..
pwd
fi
done
if [[ -d package ]]
then
cd package
pwd
fi
This script "climbs" the directories until it finds a "package" directory. It the cd into it.
To use the script, you have to source it. If you execute it, it will change directories while the script is running, but it will not affect your current terminal.
So, lets assume the script is ~/bin/cd_package.bash
You would call it like this: . ~/bin/cd_package.bash
Note the pwd commands are just so you can follow what is going on and can be removed once you are convinced it works.

problems with git bash on windows. i can't access folders on git bash for windows

i bought a course from codecademy and at beginner was a lesson about git bash. I install it , but if i want to change directories with "cd" i cant't.
enter image description here
Also , when i double-click on shortcut the program doesn't run.
Try changing to you home directory first
$ cd
then
$ pwd
/c/Users/yourname
Try going to you root directory with : cd / (make sure you add a space between cd and /)
Then do cd yourfilename/yourfilename....you can add multiple directories
Make sure your spelling is correct
Few other tips :
To navigate to your home directory use "cd ~".
To navigate to the previous directory use "cd -"

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.

Unable to install modman, .profile is missing

https://github.com/colinmollenhour/modman/blob/master/README.md
I am trying to install modman.
First I install via:
bash < <(curl -s -L https://raw.github.com/colinmollenhour/modman/master/modman-installer)
modman is created at User/Username/bin/modman
I got lost on the next part:
source ~/.profile
I don't have .profile in my directory, so i created one in my user root and I added
export PATH=$PATH:/Users/Username/bin/modman
I am not sure if that is correct, when I cd to my project directory and
do modman init it returns modman: command not found
Why am I getting this message?
You add directories to PATH, not individual binaries.
export PATH=$PATH:/Users/Username/bin
Note the line from the installer that would have updated .profile for you:
echo -e '\nPATH="$HOME/bin:$PATH"' >> $HOME/.profile
(Typically, you might add $HOME/bin to the beginning of the path so that you can override system binaries, but in your case, it doesn't matter since you don't have modman installed outside your home directory.)

Adding shortcut(soft-link) of a directory to global PATH so that it can be accessed from everywhere

I'm trying to put all my symlinks in one directory (I'll call this symlinks directory). I've exported the path of that directory and put it in my .bashrc file. The symlinks to executable applications are running fine but I'm having hard time making symlinks for my directory. This is what I tried.
ln -s ~/mydir/ m
where m is supposed to be my symlink to mydir directory.
This works only when I'm inside symlinks directory. Trying cd m or even just m didn't work from outside that directory. I get:-
bash: cd: m: No such file or directory
Okay, so I thought maybe the PATH doesn't recognize directory paths. So I tried creating a bash script.
#!/bin/sh
cd ~/mydir/
Tried this, m...permission denied. Okay I thought and did chmod +x m to that file. And when I run that script like m then nothing. I tried ./m, still nothing.
I'm close to losing my mind to do such a simple task.
PATH is used to look for commands and I think a command ought to be a file or a symlink to a file.
So, cd m doesn't work as here the command is "cd" (not m). The lookup for "m" does not happen in PATH.
Just m does not work as the "m" found in PATH is a link to a directory and not a file. You can try creating another "m" which points to a file and put it in a directory later in the PATH and it will be recognized when you run just m.
The script you created does work, except that the cd now happens in a new shell and is lost at the end of the script. You can check this by putting an ls after the cd in your script.
There are a few ways to achieve what you want to do.
One option is to use the CDPATH variable. This is the search path for the cd command. Check man bash for details but basically, all you need to do is add your symlinks directory to CDPATH.
export CDPATH=~/symlinks:$CDPATH
cd m ## will cd to linked directory
Alternatively, you can create aliases or functions and put it in your .bashrc or another file which you then source.
alias m="cd ~/mydir"
m() {
cd ~/mydir
}
And now you can just type m to cd to mydir

Resources