3

I have an java application.And I call some batch files from app. A batch file get full path of itself by using readlink -f. But I get "command not found error" in Linux.(Linux 2.4.18-3custom #2 Wed Aug 18 03:46:33 EDT 2004). I tried pwd command in batch file.But it gave my app current directory.But I need to find batch directory from itself.So,pwd does not solve my problem. I actually write my batch file on shell script. And I can not install coreutils. Is there any alternative for readlink -f which does not include pwd?

  • 1
    Do you call batch files or shell scripts? If it's a shell script, are you using `Bash`? – konsolebox Aug 19 '14 at 14:34
  • 1
    `readlink` is not part of the Linux kernel, so giving us your kernel version does not help us know which version of readlink (if any) you have. Giving us your distribution name and version, by contrast, might help. – Charles Duffy Aug 19 '14 at 14:34
  • that said -- if your question is how to find the location of your script without `readlink`, see BashFAQ #28: http://mywiki.wooledge.org/BashFAQ/028; after discussing why you shouldn't try to do this to begin with, it then talks about the available techniques (and their limitations). See in particular the example using `BASH_SOURCE`. – Charles Duffy Aug 19 '14 at 14:36
  • possible duplicate of [Can a Bash script tell what directory it's stored in?](http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in) – Charles Duffy Aug 19 '14 at 14:39
  • ...since `readlink -f` isn't widely available on non-Linux platforms, most iterations of this question have answers covering techniques which don't require it. – Charles Duffy Aug 19 '14 at 14:40
  • 1. I actually use shell script and bash. 2. this is my version and name (Red Hat Linux release 7.3 (Valhalla)) 3. I tried pwd but it gave me the application path.I can not see any other way on your link. – fatih emrah durum Aug 19 '14 at 15:16

2 Answers2

0

This is an example forwarded solution from my dormant blog in Linuxquestions.org. You can make your script use this to get the absolute path of a path. This one works with any version of Bash. You can also get other solutions for other shells even old ones. Just check the post.

function getabspath {
    local -a T=()
    local -i I=0
    local IFS=/ A

    case "$1" in
    /*)
        __=$1
        ;;
    *)
        __=/$PWD/$1
        ;;
    esac

    while read -r -d / A; do
        case "$A" in
        ..)
            [[ I -ne 0 ]] && unset T\[--I\]
            continue
            ;;
        .|'')
            continue
            ;;
        esac

        T[I++]=$A
    done << .
$__/
.

    case "$1" in
    */)
        [[ I -ne 0 ]] && __="/${T[*]}/" || __=/
        ;;
    *)
        [[ I -ne 0 ]] && __="/${T[*]}" || __=/.
        ;;
    esac
}

Usage:

getabspath your/path/here
<Do something with "$__">

Consider compiling a simple C solution as well.

konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • Thanks for idea.How can ı use your methods? You say in usage "methodname" and "path".But Also find batch's full path. I am using shell script.I tried your method but it was some errors in linux machine. Exp. ./test.bat: line 10: syntax error near unexpected token `<<<' ./test.bat: line 10: ` read -r -a T1 <<< "$1"' – fatih emrah durum Aug 19 '14 at 15:44
  • @fatihemrahdurum You have to know first what shell you're using. Do you get a message when you run `bash --version`? – konsolebox Aug 19 '14 at 15:50
  • Yes.My shell version is GNU bash, version 2.05a.0(1)-release (i686-pc-linux-gnu) Copyright 2001 Free Software Foundation, Inc. – fatih emrah durum Aug 19 '14 at 16:34
  • There is no error after your update.Thank you. But the reason is the same with pwd command.It gave app path again.I need batch physical path.Have you any idea?Where is the wrong step? – fatih emrah durum Aug 19 '14 at 17:26
  • @fatihemrahdurum Can you reply back the command you use to get the path? Can you also tell me why the solutions in [Can a Bash script tell what directory it's stored in?](http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in), not working for you? – konsolebox Aug 19 '14 at 17:42
  • function getabspath { local -a T=() local -i I=0 local IFS=/ A case "$1" in /*) __=$1 ;; *) __=/$PWD/$1 ;; esac while read -r -d / A; do case "$A" in ..) [[ I -ne 0 ]] && unset T\[--I\] continue ;; .|'') continue ;; esac T[I++]=$A done << . $__/ . case "$1" in */) [[ I -ne 0 ]] && __="/${T[*]}/" || __=/ ;; *) [[ I -ne 0 ]] && __="/${T[*]}" || __=/. ;; esac } getabspath path="$__" – fatih emrah durum Aug 20 '14 at 07:35
  • It's not clear what argument you're passing to `getabspath`. What was you original command that uses `readlink -f`? I'd suggest how to convert it properly.. – konsolebox Aug 20 '14 at 12:17
0

readlink is part of the coreutils package for Linux. So simply install coreutils on your system and you will have readlink available.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85