how to use a shell script as a linux command

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kalaisuresh
    New Member
    • Oct 2006
    • 17

    how to use a shell script as a linux command

    Hai ,

    I have a shell script in my directory
    filename = c
    -----
    #/bin/bash
    clear
    -----
    root>c
    if i run this script in my directory it is working
    but i have this one to use like linux command
    whereever i am giving c that terminal or console has to be clear
    please help me
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by kalaisuresh
    Hai ,

    I have a shell script in my directory
    filename = c
    -----
    #/bin/bash
    clear
    -----
    root>c
    if i run this script in my directory it is working
    but i have this one to use like linux command
    whereever i am giving c that terminal or console has to be clear
    please help me
    Try this:

    # ./c

    That is a way to execute programs when the directory is not in your path (if it is, you should be able to execute by just typing the name).

    I think (though please someone correct me if I'm wrong) that you can use
    # echo $PATH
    to see what your current path is set at, and I can't remember of the top of my head, but if you Google "add to path" and the OS flavor you're using, you'll see pretty quickly how to edit the path.

    Comment

    • prn
      Recognized Expert Contributor
      • Apr 2007
      • 254

      #3
      kalaisuresh appears to be using bash as his shell, so:

      $ echo $PATH

      should work fine to show the path. Let's suppose the result looks like this:

      Code:
      [prn@deimos ~]$ echo $PATH
      /usr/lib/qt-3.3/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin
      A sensible thing to do (and commonly done) would be to create a "bin" directory for storing personal programs. If so, I might create a directory as /home/prn/bin and put a program, e.g., like his c into that directory. Now I want to make sure that when I type "c[enter]" at my prompt, that my program c is the one that is executed. (In this example, there probably isn't a system equivalent, but just to be sure, it would be wise to check. Use the command
      which c
      to see if the system would find another program of the same name on your PATH).

      Now you you want to make sure that your system will always find your program c for you. Edit your .bash_profile and, toward the end, include a line like:
      PATH=$HOME/bin:$PATH
      this puts the directory /home/bin/prn (after all, this is my example and I know what my login is -- I'm less sure about yours :)) at the beginning of my path. Whatever your login name is, it will put YOUR bin directory, i.e., the one hanging off your $HOME at the beginning of your path.

      Now, after
      Code:
      . .bash_profile
      (to rerun your profile script):
      Code:
      [prn@deimos ~]$ echo $PATH
      /home/prn/bin:/usr/lib/qt-3.3/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin
      /home/prn/bin is now the first place my shell will look for programs.

      Of course, in the case of something so simple, it would probably be a lot easier to put a line like
      Code:
      alias c='clear'
      into your .bashrc file.

      There is one additional comment I feel almost compelled to make and that involves the line
      Originally posted by kalaisuresh
      root>c
      from the original post. I hope that doesn't mean what I think it does. It's a really good idea NOT to be root all the time. Make sure you have a normal user account and generally use that. If you're not sure why, that's probably a good topic for another thread.

      Best of Luck,
      Paul

      Comment

      Working...