Results 1 to 4 of 4

Thread: Use alias names in command

  1. #1
    Join Date
    Jul 2012
    Beans
    35

    Use alias names in command

    I want to use alias names as inputs so as to save time. Is there a way to define aliases in such a way so as to achieve that?

    E.g. if have a lot of python scripts in my /home/pycodes/ folder name as TestCaseXXX where XXX denotes a three digit number.
    Can I define an alias (say like pyrun), that when i type pyrunXXX , the resulting command = python /home/pycodes/Testcasexxx.py ??

  2. #2
    Join Date
    May 2010
    Location
    uk
    Beans
    9,374
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Use alias names in command

    Hi

    I would be tempted to use a function for this type of thing.

    Take a look at this.
    Code:
    matthew-S206:/home/matthew % ls -l tmp[1-3]
    -rwxr-xr-x 1 matthew matthew 13 Apr 11 20:22 tmp1*
    -rwxr-xr-x 1 matthew matthew 13 Apr 11 20:22 tmp2*
    -rwxr-xr-x 1 matthew matthew 13 Apr 11 20:22 tmp3*
    matthew-S206:/home/matthew %
    Code:
    matthew-S206:/home/matthew % cat tmp[1-3]
    echo In tmp1
    echo In tmp2
    echo In tmp3
    matthew-S206:/home/matthew %
    Code:
    matthew-S206:/home/matthew % function c { /home/matthew/tmp${1} }
    matthew-S206:/home/matthew %
    Code:
    matthew-S206:/home/matthew % c 1
    In tmp1
    matthew-S206:/home/matthew % c 2
    In tmp2
    matthew-S206:/home/matthew % c 3
    In tmp3
    matthew-S206:/home/matthew % c 4
    c: no such file or directory: /home/matthew/tmp4
    matthew-S206:/home/matthew %
    It's not an alias and requires a space between the function name and parameter but it is far more extendible than an alias.

    Kind regards
    If you believe everything you read, you better not read. ~ Japanese Proverb

    If you don't read the newspaper, you're uninformed. If you read the newspaper, you're mis-informed. - Mark Twain

    Thinking about becoming an Ubuntu Member?

  3. #3
    Join Date
    Jul 2012
    Beans
    35

    Re: Use alias names in command

    That sounds great. Can you tell me how to use a function (of which I know next to nothing about) to achieve my goal (like running the python codes above)?

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Use alias names in command

    something like
    Code:
    pyrun() { python /home/pycodes/Testcase${1}.py; }
    you can also create a script and put it in $HOME/bin

    Code:
    #!/bin/bash
    python /home/pycodes/Testcase${1}.py
    in both cases pyrun 123 will put 123 in place of $1
    Last edited by Vaphell; April 24th, 2013 at 03:36 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •