#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    29
    Rep Power
    0

    Question how to execute shell script?


    hi,

    I would like to know how I can execute a shell script without having to sorce it?
    Im on Unix solaris.

    rite now i do . ./myscript.sh

    shouldnt it be just ./myscript.sh .please explain and help.
    is .sh necessary?...
    thanks in advance.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    You run a script like this:
    ./myscript.sh

    Just make sure you have execute permissions on it.

    >> is .sh necessary?...
    No, you can rename your file to anything you want (myscript, myscript.foo, myscript.bat etc.).
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Location
    USA
    Posts
    334
    Rep Power
    11
    As well as the correct permissions make sure that you
    have something like the following as the first line of
    the script so that the correct shell interpreter is
    used to execute the script.

    #!/usr/bin/ksh for the Korn shell,
    #!/usr/bin/sh for the POSIX shell,

    and so on.
  6. #4
  7. funky munky
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2001
    Location
    UK
    Posts
    1,446
    Rep Power
    16
    In addition to the above comments, if you place the script in your PATH you can just execute the file without being calling it with it's full path-name.

    For example I have a lot of scripts I use on a regular basis in ~/bin (in fact many of these are actually symlinks to other scripts, but that's another story:P). In my ~/.cshrc I make sure I have '~/bin' in my PATH env variable:

    set path = ($HOME/bin ... other/paths

    (note this is different for bash/ksh/etc).

    This way when I want to execute a script that resides in ~/bin I can do so from any directory just as:

    myscript.sh

    (without the leading path to the script).

    HTH.
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    Originally Posted by fpmurphy
    As well as the correct permissions make sure that you
    have something like the following as the first line of
    the script so that the correct shell interpreter is
    used to execute the script.

    #!/usr/bin/ksh for the Korn shell,
    #!/usr/bin/sh for the POSIX shell,

    and so on.
    Erm, I think you mean /bin/ksh and /bin/sh, not /usr/bin/ksh and /usr/bin/sh. On most BSD and Linux systems (RedHat, NetBSD, OpenBSD and FreeBSD), your default shells are usually in /bin, not /usr/bin.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2008
    Posts
    1
    Rep Power
    0

    #!bin/sh


    Originally Posted by munkfish
    In addition to the above comments, if you place the script in your PATH you can just execute the file without being calling it with it's full path-name.

    For example I have a lot of scripts I use on a regular basis in ~/bin (in fact many of these are actually symlinks to other scripts, but that's another story:P). In my ~/.cshrc I make sure I have '~/bin' in my PATH env variable:

    set path = ($HOME/bin ... other/paths

    (note this is different for bash/ksh/etc).

    This way when I want to execute a script that resides in ~/bin I can do so from any directory just as:

    myscript.sh

    (without the leading path to the script).

    HTH.

    Have a similar question

    I have a script called "run.sh"

    I have another script called "start_run.pl"

    I used these scripts 1000 times and they worked perfectly

    But now I get an error

    In the start_run.pl I have a command--------print SCRIPT "/home/programs/run.sh


    when I run the "run.sh" script on the command line in linux it says bash: run.sh: command not found
    so I assume that is why the start_run.pl script is not working

    I know the ./run.sh works (with the ./).

    But I would like to know how I can change it so that when I type run.sh without ./ it would work becuase it worked before like that

    In the script its
    /home/programs/run.sh (I cannot go /home/programs/./run.sh)

    These scripts used to work before so i do not understand why its a problem now?

    does it have to do with the /bin/sh (I have the right permissions so its not that)
    does it have to be configured again so that I can run a command without the ./

    Do I have to set the path of bin/sh?
    do you have any suggestions?

IMN logo majestic logo threadwatch logo seochat tools logo