Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 17th, 2006, 3:31 PM   #1
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Starting to use Unix

I'm just starting unix and I've learned most of the basics like browsing through directory trees, creating files using 'vi' and such (well way more than that actually.. lol).

So today I decided to move onto scripts. One thing that I don't understand is why use the 'bash' command or (bourne shell) before creating a script. I understand that going into the bourne shell is where you should be scripting. What is the different shell for? Why not just stay in whatever shell you're using. Is there any advantage to using the bourne again shell (the bash command) over the default shell?
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old Nov 17th, 2006, 3:46 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Are you talking about the shebang at the top of the file?
#!/bin/bash
Or are you saying that you've been told to run "bash" before editing the shell scripts?
Arevos is offline   Reply With Quote
Old Nov 17th, 2006, 3:56 PM   #3
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Yes, I've been told to write the 'bash' command then create my script in that shell.
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old Nov 17th, 2006, 6:22 PM   #4
Eoin
Hobbyist Programmer
 
Eoin's Avatar
 
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3 Eoin is on a distinguished road
Well what you've been told is very confusing. If you're familiar with windows the bash is sorta like the command prompt and a script is like a bat file.

Though is practise that's a gross over simplification.
__________________
Visit my website BinaryNotions.
Eoin is offline   Reply With Quote
Old Nov 18th, 2006, 3:01 AM   #5
free-zombie
Programmer
 
free-zombie's Avatar
 
Join Date: May 2006
Location: Bavaria, Germany
Posts: 50
Rep Power: 0 free-zombie is an unknown quantity at this point
Send a message via ICQ to free-zombie Send a message via MSN to free-zombie Send a message via Yahoo to free-zombie
you can create a script from any editor, so you could start an emacs from a csh or run an instance of SciTE on a remote computer and still write a bash script. 'bash' is the most commonly used shell, but 'sh' is the only one you can guarantee to exist - bash is fully compatible with any vanilla bourne sh.

The first line of a shell script, or any script (including python, perl, ruby...) tells the system how to run it if it begins with '#!' this means that
#!/bin/sh
tells the system that './script.sh' (if that's the script...) is equivalent to "/bin/sh ./script.sh" /
free-zombie is offline   Reply With Quote
Old Nov 18th, 2006, 4:02 AM   #6
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Eric the Red View Post
Yes, I've been told to write the 'bash' command then create my script in that shell.
If I understand you correctly, you shouldn't need to do that.

For instance, I could be running dash, and create a shell script that will run using bash:
dash$ cat > shellscript.sh
#!/bin/bash
echo Running using $SHELL

dash$ chmod +x shellscript.sh
dash$ ./shellscript.sh
Running using /bin/bash
Of course, it could be that your teacher wants you to switch to a standard shell before doing anything, so you don't run into any strange commands. However, the shell script itself is just a text file, and can be created using any program or shell with the capability to write text to files.
Arevos is offline   Reply With Quote
Old Nov 18th, 2006, 6:24 AM   #7
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 766
Rep Power: 3 Jimbo is on a distinguished road
To expound on what free-zombie said a bit, I actually just asked about the #! syntax in another thread recently. There were some good replies, and I recommend that you read it if you haven't already.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Nov 18th, 2006, 4:16 PM   #8
a thing
Unverified User
 
a thing's Avatar
 
Join Date: Aug 2005
Location: none
Posts: 146
Rep Power: 0 a thing is on a distinguished road
Kinda off-topic, but you should use sh instead of bash at the beginning of the script, unless the script uses some feature in Bash that standard sh doesn't have for compatiblity with systems that don't have Bash (FreeBSD doesn't come with Bash by default).
__________________
Warning: My posts may change (dramatically) within the first 15 minutes they're posted.
Got 'Nux?—GNU/Linux and other free software support.
It's GNU/Linux, not just Linux.
a thing is offline   Reply With Quote
Old Jan 31st, 2008, 12:35 AM   #9
sons_of_bitch
Newbie
 
Join Date: Jan 2008
Posts: 2
Rep Power: 0 sons_of_bitch is on a distinguished road
Re: Starting to use Unix

why some of the scrip file is without the starting #!/bin/bash or any shell, and can straight away coding? what is the different between with starting #!/bin/bash or without the #!/bin/xxx in the starting script?

sorry i'm new in unix..

Thanks
sons_of_bitch is offline   Reply With Quote
Old Apr 7th, 2008, 12:14 AM   #10
opa6x57
Hmmmm ... Is there more??
 
opa6x57's Avatar
 
Join Date: Apr 2008
Location: Post Falls, ID
Posts: 15
Rep Power: 0 opa6x57 is on a distinguished road
Re: Starting to use Unix

Quote:
Originally Posted by sons_of_bitch View Post
why some of the scrip file is without the starting #!/bin/bash or any shell, and can straight away coding? what is the different between with starting #!/bin/bash or without the #!/bin/xxx in the starting script?

sorry i'm new in unix..

Thanks

If you are writing a script that you know, with 100% certainty, will always be run on a machine where the only available shell is the one you're using to write the script - then this line is completely optional.

If, however, there exists a possibility that some user will use a different shell - then you MUST specify the shell to be used for your script.

For example - if you use a script command that only exists in the /bin/csh shell - and your user happens to be using the /bin/sh shell - it is very likely that your script will fail. (I almost would guarantee failure in this instance...)

But, if you force your script to run in a specific shell using the !shebang then your script will function correctly.
__________________
Ken -
New to PFO ... but been dabbling in various versions of BASIC since highschool - circa 1973.

"Shouldn't the 'Air and Space' museum be empty?" - Dennis Miller
opa6x57 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting 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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Starting a career... start-ups worth it? Jimbo Coder's Corner Lounge 12 Nov 2nd, 2006 12:23 PM
Unix commands compatible with Windows? titaniumdecoy Bash / Shell Scripting 7 Oct 5th, 2006 7:25 AM
Lions' Commentary on UNIX 6th Edition, with source code Mad_guy Book Reviews 0 Sep 24th, 2006 7:06 PM
Best Linux or Unix Desktop? Prm753 Coder's Corner Lounge 23 Apr 8th, 2006 10:17 AM
Importing from DLL's on Unix Kaja Fumei Existing Project Development 5 Jan 9th, 2006 6:35 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:21 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC