Menu

Main Menu
Talk Get Daily Search

Member's Online

    User Name
    Password

    Very handy shellscript to get absolute path

    Reply
    GameboyRMH | # 1 | 2010-05-31, 15:49 | Report

    Check this out, a shellscript to get the absolute path to any file:

    Code:
    #! /bin/sh
    
    ABPATH=$(readlink -f "$1")
    echo $ABPATH
    "Big deal" you say? Well this can do some very handy things. For example if you use Easy Debian apps via the debbie or sudo debian commands, you know what a PITA it is to have to specify an absolute path all the time. So let's say you're in /media/mmc1/Documents/PDFs/school/coursexyz/ and want to open doc1.pdf with evince. You just do:

    debbie evince `~/getabpath doc1.pdf`

    (this example assumes you put the script in ~/ but you could put it in /usr/bin)

    You could even integrate it into your debbie/debian scripts in /usr/bin to make this sort of thing automatic.

    Edit | Forward | Quote | Quick Reply | Thanks

    Last edited by GameboyRMH; 2010-05-31 at 16:13.

     
    Joorin | # 2 | 2010-05-31, 16:02 | Report

    Pardon my ignorance, I don't use Easy Debian, but why won't
    Code:
    debbie evince doc1.pdf
    work?

    Edit | Forward | Quote | Quick Reply | Thanks

     
    x-lette | # 3 | 2010-05-31, 16:03 | Report

    Nice, but ......

    1: why do you use the variable? couldn't you just do a "echo $(readlink -f $1)" ?

    2: why not call from commandline readlink -f FILE ? Or you could set up an alias called readlinkf as `readlink -f` or even shorten the alias to rlf

    Edit | Forward | Quote | Quick Reply | Thanks

     
    x-lette | # 4 | 2010-05-31, 16:05 | Report

    Originally Posted by Joorin View Post
    Pardon my ignorance, I don't use Easy Debian, but why won't
    Code:
    debbie evince doc1.pdf
    work?
    I also don't know exactly how EasyDebian works but I guess all file calls need absolute paths. This happens.

    Edit | Forward | Quote | Quick Reply | Thanks

     
    GameboyRMH | # 5 | 2010-05-31, 16:06 | Report

    Originally Posted by Joorin View Post
    Pardon my ignorance, I don't use Easy Debian, but why won't
    Code:
    debbie evince doc1.pdf
    work?
    Because Easy Debian runs in a chroot, you don't get the same working directory when you use a debbie/sudo debian command, so relative paths are useless.

    And x-lette you're totally right, I could have done both of those things. I thought I'd just lay it out in a more self-explanatory manner to help the noobs understand.

    Edit | Forward | Quote | Quick Reply | Thanks

    Last edited by GameboyRMH; 2010-05-31 at 16:08.

     
    Joorin | # 6 | 2010-05-31, 16:08 | Report

    Originally Posted by x-lette View Post
    I also don't know exactly how EasyDebian works but I guess all file calls need absolute paths. This happens.
    That sounds like a bug in the Easy Debian wrapper script that should be fixed instead of using scripts like the one above.

    CWD is known. What more is needed?

    Edit | Forward | Quote | Quick Reply | Thanks

     
    Joorin | # 7 | 2010-05-31, 16:14 | Report

    Originally Posted by GameboyRMH View Post
    Because Easy Debian runs in a chroot, you don't get the same working directory when you use a debbie/sudo debian command, so relative paths are useless.

    And x-lette you're totally right, I could have done both of those things. I thought I'd just lay it out in a more self-explanatory manner to help the noobs understand.
    But, your script supplies the full path, from the root, down to your file, so Easy Debian must still be able to handle the whole file system. Or is the script run after the chroot has been set up? Then it's a matter of letting the wrapper script know where you started it from and if the file name starts with a "/". CWD is known when you start the wrapper script.

    And, please, this is not me trying to bash () your script or your idea, I'm just trying to understand.

    Edit | Forward | Quote | Quick Reply | Thanks

     
    GameboyRMH | # 8 | 2010-05-31, 17:37 | Report

    Originally Posted by Joorin View Post
    But, your script supplies the full path, from the root, down to your file, so Easy Debian must still be able to handle the whole file system. Or is the script run after the chroot has been set up? Then it's a matter of letting the wrapper script know where you started it from and if the file name starts with a "/". CWD is known when you start the wrapper script.

    And, please, this is not me trying to bash () your script or your idea, I'm just trying to understand.
    When you run a command with debbie/sudo debian, it always starts in /home/user. So if you're in /media/mmc1 and you run a "evince doc.pdf," debbie will look for doc.pdf in /home/user and won't work.

    Also I looked at the debbie / debian scripts and it isn't possible to make the scripts convert paths unless you assume that a certain argument will always be a path.

    Edit | Forward | Quote | Quick Reply | Thanks

     
    stanman | # 9 | 2010-10-09, 22:53 | Report

    If, in /usr/bin/debbie, you replace the line
    Code:
     sudo /sbin/debian su $DEBUSER -c "$*"
    with
    Code:
      sudo /sbin/debian su $DEBUSER -l -c "cd $PWD; $*"
    the script knows from where you issued the debbie command. Then you can issue commands like
    Code:
      cd /home/user
      debbie ls -l dir/file.txt
    and get the appropriate output instead of "No such file".

    Note that for this to work the PWD directory must be mounted in /.debian, for example, the directory /home must be mounted as /.debian/home.

    Note also that you can issue compound commands with debbie by surrounding the commands with double quotes, for example,
    Code:
      cd /home/user
      debbie "ls MyDocs; df .; df /"

    Edit | Forward | Quote | Quick Reply | Thanks
    The Following User Says Thank You to stanman For This Useful Post:
    GameboyRMH

     
vBulletin® Version 3.8.8
Normal Logout