Results 1 to 8 of 8
  1. #1


    Member Since
    Apr 16, 2008
    Posts
    36
    Terminal.app/bash: Creating An Executable Shell Script
    I'm wondering what I'm missing here. I'm trying to create a little script that executes in a terminal session. I'm using bash (the default terminal shell). Here's what I did in a terminal session to try to make one--

    $ ed mmm
    mmm: No such file or directory
    i
    #!/bin/bash
    echo "Hello World!"
    .
    w
    32
    q
    $ ls -l mmm
    -rw-r--r-- 1 markhenri staff 32 Apr 28 10:24 mmm
    $ chmod +x mmm
    $ ls -l mmm
    -rwxr-xr-x 1 markhenri staff 32 Apr 28 10:24 mmm
    $ mmm
    -bash: mmm: command not found
    $ bash mmm
    Hello World!
    $ sh mmm
    Hello World!
    $



    Seems like this should be an executable file? On line docs say this is how you do it. Is it something with my rights/privileges? I tried mmm.sh also as well as #!/bin/sh in the first line. The script runs fine if you invoke the shell command before it. Odd?

  2. #2

    Dysfunction's Avatar
    Member Since
    Mar 17, 2008
    Location
    Tucson, AZ
    Posts
    6,866
    Specs:
    Way... way too many specs to list.
    try ./mmm

    mikeMbp:~ mike$ ls -l mmm
    -rw-r--r-- 1 mike staff 31 Apr 28 10:37 mmm
    mikeMbp:~ mike$ chmod 755 mmm
    mikeMbp:~ mike$ cat mmm
    #!/bin/bash
    echo "Hello World"
    mikeMbp:~ mike$ ./mmm
    Hello World


    it's not in the path.

    mikeMbp:~ mike$ sudo cp mmm /usr/local/bin
    mikeMbp:~ mike$ mmm
    Hello World
    mikeMbp:~ mike$
    mike
    This machine kills fascists
    Got # ? phear the command line!

  3. #3


    Member Since
    Apr 16, 2008
    Posts
    36
    That worked.

    I didn't have a /usr/local/bin directory so I created one.

    It was in my path though?

    I guess my question is about scripts in general. On Win XP, I used to write batch files for doing ftp uploads and just leave them laying around whatever directory I was working in. When I'd finish working on the file, I'd just execute the batch file and up it would go. It was convenient because I didn't have to go into my FTP client or start issuing change dir commands all all that.

    Is there a Mac equivalent way to do this? It sort of appears that this would go against the security model on Unix and it's probably why PC's are notoriously vulnerable to malicious code. Am I getting this?

  4. #4

    GroovyLinuxGuy's Avatar
    Member Since
    Apr 02, 2006
    Location
    Edmonton, Canada
    Posts
    125
    Specs:
    15" MacBook Pro, 12" iBook G4, 14" iBook G4 running Ubuntu 7.10
    Quote Originally Posted by ebay View Post
    That worked.

    I didn't have a /usr/local/bin directory so I created one.

    It was in my path though?

    I guess my question is about scripts in general. On Win XP, I used to write batch files for doing ftp uploads and just leave them laying around whatever directory I was working in. When I'd finish working on the file, I'd just execute the batch file and up it would go. It was convenient because I didn't have to go into my FTP client or start issuing change dir commands all all that.

    Is there a Mac equivalent way to do this? It sort of appears that this would go against the security model on Unix and it's probably why PC's are notoriously vulnerable to malicious code. Am I getting this?
    You are not using Windows anymore...remember that you are doing things the *nix way now. You either prefix it with ./ or call it from another directory or have it in your path.

    Hmm...what I wrote sound kinda snippy...I don't mean it to sound that way. No offence meant if any taken.

    Cheers

  5. #5


    Member Since
    Apr 16, 2008
    Posts
    36
    Ha ha, no offense taken! Very grateful for your comments.

    OT: I'm still detoxing from Windows. I'm on day 7. It's going pretty good. A prospective client suggested that I load Windows on my Mac and I just about had a hissy-fit. He said, and I quote, "you could always slap Windows on..." I felt cheap and dirty afterward.

    I did notice that I could suffix the file with .mybatch (whatever) and open it from finder where it asked for an association with that suffix. If I chose terminal.app, it ran it. Am I straying here?

  6. #6

    Dysfunction's Avatar
    Member Since
    Mar 17, 2008
    Location
    Tucson, AZ
    Posts
    6,866
    Specs:
    Way... way too many specs to list.
    To view your current path type
    echo $PATH

    in your terminal
    mike
    This machine kills fascists
    Got # ? phear the command line!

  7. #7

    Dysfunction's Avatar
    Member Since
    Mar 17, 2008
    Location
    Tucson, AZ
    Posts
    6,866
    Specs:
    Way... way too many specs to list.
    Quote Originally Posted by ebay View Post
    Ha ha, no offense taken! Very grateful for your comments.

    OT: I'm still detoxing from Windows. I'm on day 7. It's going pretty good. A prospective client suggested that I load Windows on my Mac and I just about had a hissy-fit. He said, and I quote, "you could always slap Windows on..." I felt cheap and dirty afterward.

    I did notice that I could suffix the file with .mybatch (whatever) and open it from finder where it asked for an association with that suffix. If I chose terminal.app, it ran it. Am I straying here?
    you should be able to run it that way from finder. Personally most of my scripts are run via cron.
    mike
    This machine kills fascists
    Got # ? phear the command line!

  8. #8

    GroovyLinuxGuy's Avatar
    Member Since
    Apr 02, 2006
    Location
    Edmonton, Canada
    Posts
    125
    Specs:
    15" MacBook Pro, 12" iBook G4, 14" iBook G4 running Ubuntu 7.10
    Quote Originally Posted by ebay View Post
    Ha ha, no offense taken! Very grateful for your comments.

    OT: I'm still detoxing from Windows. I'm on day 7. It's going pretty good. A prospective client suggested that I load Windows on my Mac and I just about had a hissy-fit. He said, and I quote, "you could always slap Windows on..." I felt cheap and dirty afterward.

    I did notice that I could suffix the file with .mybatch (whatever) and open it from finder where it asked for an association with that suffix. If I chose terminal.app, it ran it. Am I straying here?
    Yup...if it works, then it works...

    Most of my scripts are run out of my bin directory in my home...I just added it to my path...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Bash shell scripting in OS X
    By V6Pony in forum OS X - Development and Darwin
    Replies: 18
    Last Post: 02-24-2016, 11:39 AM
  2. Terminal -- shell script -- script will only read two lines of a file
    By Straitsfan in forum OS X - Apps and Games
    Replies: 0
    Last Post: 01-30-2011, 09:04 PM
  3. Making a Shell Script Executable
    By surfwax95 in forum OS X - Development and Darwin
    Replies: 10
    Last Post: 08-25-2010, 04:15 PM
  4. Help with the bash...alias/script...?
    By Antares in forum OS X - Development and Darwin
    Replies: 2
    Last Post: 10-26-2009, 12:32 PM
  5. Bash Script
    By ftjogoh in forum Apple Notebooks
    Replies: 1
    Last Post: 11-04-2004, 01:14 PM

Posting Permissions

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