I have a path that I retrieve with the pwd command, I have to modify that path runtime in my shell script. For istance let’s say I have /var/www/cgi-script, I have to modify it in /var/www/etc. What I am trying to do is retrieve the last index of “/”, take a substring and then add etc. I tryed with awk several times but I can’t seem to find a solution. Is there a way to use a reverse index?
path=`pwd`;
char="/"
pathindex=`awk -v a="$path" -v b="$char" 'BEGIN { print index(a,b)}'`
Check out dirname: