Terminal.app/bash: Creating An Executable Shell Script

Joined
Apr 16, 2008
Messages
36
Reaction score
0
Points
6
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?
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's 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$
 
OP
E
Joined
Apr 16, 2008
Messages
36
Reaction score
0
Points
6
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?
 
Joined
Apr 2, 2006
Messages
125
Reaction score
5
Points
18
Location
Edmonton, Canada
Your Mac's Specs
15" MacBook Pro, 12" iBook G4, 14" iBook G4 running Ubuntu 7.10
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. :D

Cheers
 
OP
E
Joined
Apr 16, 2008
Messages
36
Reaction score
0
Points
6
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?
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's Specs
Way... way too many specs to list.
To view your current path type
echo $PATH

in your terminal
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's Specs
Way... way too many specs to list.
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. :)
 
Joined
Apr 2, 2006
Messages
125
Reaction score
5
Points
18
Location
Edmonton, Canada
Your Mac's Specs
15" MacBook Pro, 12" iBook G4, 14" iBook G4 running Ubuntu 7.10
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... :D

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

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top