Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Bash / Shell Scripting (http://www.programmingforums.org/forum26.html)
-   -   Starting to use Unix (http://www.programmingforums.org/showthread.php?t=11919)

Eric the Red Nov 17th, 2006 3:31 PM

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?

Arevos Nov 17th, 2006 3:46 PM

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?

Eric the Red Nov 17th, 2006 3:56 PM

Yes, I've been told to write the 'bash' command then create my script in that shell.

Eoin Nov 17th, 2006 6:22 PM

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.

free-zombie Nov 18th, 2006 3:01 AM

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" /

Arevos Nov 18th, 2006 4:02 AM

Quote:

Originally Posted by Eric the Red (Post 119170)
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.

Jimbo Nov 18th, 2006 6:24 AM

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.

a thing Nov 18th, 2006 4:16 PM

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).

sons_of_bitch Jan 31st, 2008 12:35 AM

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

opa6x57 Apr 7th, 2008 12:14 AM

Re: Starting to use Unix
 
Quote:

Originally Posted by sons_of_bitch (Post 140558)
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.


All times are GMT -5. The time now is 4:47 PM.

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