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

    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Executing sh script ./ style


    I've been looking around the net for help on executing a shell script.

    My basic understanding is that after setting executable permissions and providing a path (#!/bin/sh) in the first line of the script, I can type ./myscript to execute instead of sh myscript.

    This is not working for me. I can run "sh myscript" but not "./myscript" even though I know for sure I have across the board execute permission and my sh path is correct.

    I'm working on a redhat linux station. Can anybody point out what my problem is?
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,749
    Rep Power
    9397
    What shell are you in? Or in other words, where are you trying to run the file from?
  4. #3
  5. No Profile Picture
    Stumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,409
    Rep Power
    4538
    Redhat defaults to using bash not sh
    ======
    Doug G
    ======
    It is a truism of American politics that no man who can win an election deserves to. --Trevanian, from the novel Shibumi
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,400
    Rep Power
    1688
    The shebang line (the #!/bin/sh as the first line in a script) just tells the system which interpreter this file is for. Where the script file you wish to run is found is another matter - that is handled by the $PATH environment variable and by how you invoke the script.
    By using ./myscript you are telling the system you want to execute the file called myscript which is in the current directory. If it's elewhere it won't be found. Using sh myscript tells the system that you want to load the sh interpreter (which it should find!) then have that load and run the file myscript which, I think, will be searched for in the directories specifed in $PATH.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0
    Originally Posted by requinix
    What shell are you in? Or in other words, where are you trying to run the file from?
    I'm in a tcsh environment.

    @simon: Yes I'm in the same directory as the script i'm trying to run. But even so, when I try ./myscript I just get a "./myscript: command not found" error.

    edit: Ok problem solved....turns out it was because I had started writing the script file on a windows machine so it had stupid windows invisible endline characters on it. Got rid of those and everything is good.

    Thanks for feedback guys.

    Comments on this post

    • mikeatvillage agrees : I hate it when that happens :-(
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,400
    Rep Power
    1688
    Ahhh yes, that old chestnut ... a non-executable executable is a classic example of that!
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Location
    Mount Laurel,NJ,USA
    Posts
    2
    Rep Power
    0
    If you want to use your shall file with different user either root . Give permission for your sh file execution according your need.
    like that :
    $ chmod ugo+rx file name

    then open console and type:

    Sh filename.sh OR bash filename.sh

IMN logo majestic logo threadwatch logo seochat tools logo