Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > shell programming in OS X

shell programming in OS X
Thread Tools
Fresh-Faced Recruit
Join Date: Aug 2003
Location: London
Status: Offline
Reply With Quote
Aug 12, 2003, 07:07 PM
 
hi guys
i run Jaguar on an iMacDV400
I want to learn shell programming and picked up a book on Linux programming from local library.

I changed my shell to BASH (as it is used in the book) and tried the first example (echo "hello world") but didn't work.
the script is like this:

#!/bin/bash
echo "hello world"

I saved it as tryme.sh and changed its permissions, so it is executable, but if I try to run it from the terminal, this is what i get:
bash-2.05a$ tryme
bash: tryme: command not found
bash-2.05a$ tryme.sh
bash: tryme.sh: command not found
bash-2.05a$ sh tryme
tryme: tryme: No such file or directory
bash-2.05a$ sh tryme.sh
bash-2.05a$

on linux it would be supposed to run just by typing "tryme";
the icon of the file is still the one from BBEdit and requesting info gives "BBEdit text file" rather than "shell script"


...what am i missing?
is there a way to do this kind of stuff in OS X?

any help is VEEERY APPRECIATED!
cheers!
     
Senior User
Join Date: Mar 2001
Location: Sitting in front of computer
Status: Offline
Reply With Quote
Aug 12, 2003, 07:12 PM
 
Just a guess but don't you need to specify the path to the file "tryme".
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Aug 12, 2003, 07:15 PM
 
What he said.

./tryme.sh

and it's the same in linux.
     
Fresh-Faced Recruit
Join Date: Aug 2003
Location: London
Status: Offline
Reply With Quote
Aug 12, 2003, 07:27 PM
 
sorry, i forgot to mention: I changed working directory, so that doing an "ls" command I can see the tryme.sh file;

i tried
./tryme.sh

but this is what i get:

bash-2.05a$ ./tryme.sh
echo: bad interpreter: No such file or directory

also:
bash-2.05a$ tryme.sh
bash: tryme.sh: command not found



still confused
     
Senior User
Join Date: Mar 2001
Location: Sitting in front of computer
Status: Offline
Reply With Quote
Aug 12, 2003, 08:33 PM
 
Drag and drop your file into the terminal window, this will display the path to it.
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Aug 12, 2003, 08:36 PM
 
Did you save the file with UNIX linefeeds (LF)? Mac linefeeds (CR) won't work and will cause the exact error you mention.
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Aug 12, 2003, 08:40 PM
 
     
Dedicated MacNNer
Join Date: May 2001
Status: Offline
Reply With Quote
Aug 12, 2003, 08:51 PM
 
Originally posted by wise owl:
sorry, i forgot to mention: I changed working directory, so that doing an "ls" command I can see the tryme.sh file;

i tried
./tryme.sh

but this is what i get:

bash-2.05a$ ./tryme.sh
echo: bad interpreter: No such file or directory

also:
bash-2.05a$ tryme.sh
bash: tryme.sh: command not found



still confused
Well, notice that the error is from echo, which means the file is being executed by bash. I think that there must be something wrong with your file's contents. Are you sure there are no weird hidden characters in there in the echo statement or something?

Make sure that you don't have any weird characters and that the file has been saved as a unix file. I just created a test file, test.sh, with the contents of your sample test and it worked fine for me. I did a

chmod u+x test.sh

to set the file's permissions so that it can be executed from the terminal by me.

Did you get it working?
W
     
Senior User
Join Date: Mar 2001
Location: Sitting in front of computer
Status: Offline
Reply With Quote
Aug 12, 2003, 09:10 PM
 
Worked perfect for me too. I used BBEdit to create the file and saved it with UNIX line feeds.

[G4:~] create% chmod u+x /Users/create/Desktop/tryme
[G4:~] create% /Users/create/Desktop/tryme
hello world
[G4:~] create%
     
Dedicated MacNNer
Join Date: Sep 2000
Location: New York, NY
Status: Offline
Reply With Quote
Aug 12, 2003, 10:12 PM
 
What are you using as a text editor? Text edit will work, although you may want to try vi or emacs from within the terminal if you're interested in shell scripting. BBEdit will also do the trick, although it can introduce the addition CR chars, configurable in preferences.

vi exists on almost every unix box. It's the one text editor you can count on. emacs will be on many, and pico will be on some.

your PATH variable is key in finding files. Type 'echo $PATH' at any command prompt to see its current value. If '.' (meaning 'the current directory') isn't in your PATH (and that's probably a good thing, because you don't want to mistake one program for another in whatever directory you happen to be in), than you'll have to specify the current directory with './'

To add a directory to your path in your current shell, type 'export PATH=$PATH:<directory to add>. To add the directory to all future shells (if you're using bash), create a file called '.profile' in your home directory and add the same statement to that file.

To change your default shell to bash, open netinfo (from utilities), and authenticate. Click on users, and then your username. You'll see a default shell, just change it to /bin/bash.

The default shell is normally kept in /etc/passwd on other unix boxes.

Whew!
     
Fresh-Faced Recruit
Join Date: Aug 2003
Location: London
Status: Offline
Reply With Quote
Aug 13, 2003, 02:16 PM
 
THANK YOU ALL GUYS! it works now!
it looks like the main problem was with line breaks - from BBEdit I saved the file as UNIX file.


Quadgrande, with " ... export PATH=$PATH:<directory to add> ..." do you mean all the shell scripts will then be in one directory? so then I can just call them by typing "./<name_of_file>"?




BIIIIIIIIIG THANKS!

PSownloaded WISP... nice one!
PS: do you know good websites for tutorials or information to have a good start with all this stuff (shell programming, unix/OSX...)?


     
Senior User
Join Date: Aug 2003
Location: united states empire
Status: Offline
Reply With Quote
Aug 15, 2003, 11:09 AM
 
Also:

If you use any of os x's included 'unix'-style text editors (i.e. emacs, vi, pico), you won't have to worry about saving the file with unix linefeeds as they do by default.
     
Mac Elite
Join Date: Oct 2000
Location: Seattle
Status: Offline
Reply With Quote
Aug 16, 2003, 05:15 AM
 
Originally posted by wise owl:

PS: do you know good websites for tutorials or information to have a good start with all this stuff (shell programming, unix/OSX...)?

http://www.osxfaq.com/Tutorials/LearningCenter/

cheers
You can take the dude out of So Cal, but you can't take the dude outta the dude, dude!
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 08:16 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2