how to extract path location from below given string.
/opt/oracle/app/oracle/product/12.1.0/bin/tnslsnr
expected output.
/opt/oracle/app/oracle/product/12.1.0/bin
(or)
/opt/oracle/app/oracle/product/12.1.0/bin/
how to extract path location from below given string.
/opt/oracle/app/oracle/product/12.1.0/bin/tnslsnr
expected output.
/opt/oracle/app/oracle/product/12.1.0/bin
(or)
/opt/oracle/app/oracle/product/12.1.0/bin/
str=/opt/oracle/app/oracle/product/12.1.0/bin/tnslsnr
path=${str%/*}
echo "$path"
In general, ${parameter%word} removes word from the end of parameter. In our case, we want to remove the final slash and all characters which follow: /*.
The above produces:
/opt/oracle/app/oracle/product/12.1.0/bin
dirname can be used to strip the last component from a path:
$ dirname -- "$str"
/opt/oracle/app/oracle/product/12.1.0/bin
start cmd:> dirname "/opt/oracle/app/oracle/product/12.1.0/bin/tnslsnr"
/opt/oracle/app/oracle/product/12.1.0/bin
file_path="/opt/oracle/app/oracle/product/12.1.0/bin/tnslsnr"
dir_path_woslash="${file_path%/*}"
echo "$dir_path_woslash"
/opt/oracle/app/oracle/product/12.1.0/bin
shopt -s extglob
dir_path_wslash="${file_path%%+([^/])}"
echo "$dir_path_wslash"
/opt/oracle/app/oracle/product/12.1.0/bin/