UNIX guru's, I need some help with the basename utility

Discussion in 'OT Technology' started by SnYpR808, Feb 21, 2004.

  1. SnYpR808

    SnYpR808 Guest

    i'm writing a small program and I need to use basename so that it strips the excess path info off the name of the program, so that I can call it using jrm instead of username.pj.jrm. the program works fine (it takes whatever i tell it to and sends it to the junk directory, if the junk directory doesn't exist it creates it and sends the file there.) I just can't get this stupid basename problem solved. I checked the man pages and everything and it still doesn't help with this problem. thanks in advance

    cat > username.pj.jrm
    #!/bin/sh
    if test ! -d .junk
    then
    mkdir .junk
    fi
    mv $@ .junk
     
  2. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    how are you calling it? it works fine for me :dunno:

    Code:
    [email protected]_home:~> basename /home/geoff/httpd.conf
    httpd.conf
    [email protected]_home:~> basename /home/geoff/httpd.conf .conf
    httpd
    
    
     
  3. col_panic

    col_panic calm like a bomb Moderator

    Joined:
    Sep 19, 2003
    Messages:
    188,160
    Likes Received:
    0
    Location:
    winter haven, fl
    first: badass avatar

    second: i don't understand - you want to cat to a filename by using basename instead of the full filename?
     
  4. SnYpR808

    SnYpR808 Guest

    SLED - I'm not trying to use basename to strip off the rest of the path. I'm trying to use it so that i can call a program using only the last part of the filename i guess. refer to my response to col_panic. Thanks

    col_panic -thanks, for the compliment. What I want basename to do is strip off the "username.pj" from "username.pj.jrm" so that i can call the program using only "jrm." I was told it was possible using something like prog `basename $0` but i have no idea what that's supposed to do. I tried it and it didn't work.
     
  5. GmH

    GmH Guest

    Why don't you just rename it to "jrm" (or make a symbolic link to it called "jrm")?

    If you can't do that for some reason, then (depending on which shell you use) you can probably make an alias.

    For Bourne shells (sh, bash, ksh, etc), stick this in your .profile:
    alias jrm=username.pj.jrm

    For C shells (csh, tcsh, etc) stick this in your .cshrc:
    alias jrm username.pj.jrm


    FYI, the basename command will only strip off any leading path components, and optionally a specified extension:

    basename '/path/to/somefile.ext' => 'somefile.ext'
    basename '/path/to/somefile.ext' .ext => 'somefile'

    It does not strip off the leading components of the filename, since obtaining that part (rather than removing it) is the whole point of the command.
     
    Last edited by a moderator: Feb 21, 2004

Share This Page