I have a simple script to demonstrate this question called getpath.sh:
#!/bin/bash
# getpath.sh
path="$1"
if [ -z "$path" ]; then
"details of [$PWD] :"
else
echo "details of [$path] : "
fi
Let's say I'm running this script in this directory: /home/root/mypath1/mypath2
example when I run getpath.sh without argument:
./getpath.sh
output: details of /home/root/mypath1/mypath2
Then, when I run getpath.sh script with an argument:
./getpath.sh /etc/nginx
output: details of /etc/nginx
The above results are ok but I want to get full path of the directory n-level up using '..'. Example when I run this script with '..' argument:
./getpath.sh ..
output: details of ..
the expected output should be: details of /home/root/mypath1
Another one
./getpath.sh ../..
output: details of ..
the expected output should be: details of /home/root/
and the last example:
.getpath.sh ../../..
output: details of ..
the expected output should be: details of /home
What can I modify to make it work?