Skip to content

Instantly share code, notes, and snippets.

@myclau
Created June 17, 2020 05:21
Show Gist options
  • Save myclau/9632fc83bd7cf2e2f7876f9a62fbd359 to your computer and use it in GitHub Desktop.
Save myclau/9632fc83bd7cf2e2f7876f9a62fbd359 to your computer and use it in GitHub Desktop.
find_relative_path
#powershell
# just put the uri without domain in to this variable
$gitremoteUri="xxxx/xxxxxt/xxx"
$relativeRootPath=""
$gitremoteTreeLevel=$gitremoteUri.split('/').length
for ($i=0; $i -lt $gitremoteTreeLevel ; $i++){
$relativeRootPath+="../"
}
# this result of relativeRootPath will be ../../../
$relativeRootPath
#bash
#!/bin/bash
gitremoteUri="xxxx/xxxxxt/xxx"
delimiter='/'
s=$gitremoteUri$delimiter
array=();
while [[ $s ]]; do
array+=( "${s%%"$delimiter"*}" );
s=${s#*"$delimiter"};
done;
relativeRootPath=""
for (( i=0; i<=${#delimiter[@]}; i++ ))
do
relativeRootPath+="../"
done
# this result of relativeRootPath will be ../../../
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment