On Tue, Nov 13, 2007 at 03:25:36PM +0000, jennifer.jcarr wrote:
> I am very new to linux. I was trying to write the little Hello World
> script just to play with the script. I was unable to figure out how to
> make this work. Can some one show my set by step how to save and run
> this script?
> Thanks
> Jennifer
Very short answer.
#!/bin/bash
echo "Hello World"
Suppose you name it hello.sh
Now type
bash hello.sh
It should work, unless you're doing something special in emacs, as I
note that's the subject line. If you're asking how to write it using
emacs, I can't help, I'm afraid.
Now, for the VERY long explanation below. :)
Ok, step by step. This is my way of doing it. (Also, since you're new,
let me give the quick netiquette lecture--note how I am answering your
question below the question as if it were a conversation. As most
mailers start a reply ABOVE the part they're replying to--and, in a
business context, it's sometimes helpful, let me point out that on
technical type mailing lists, it's considered preferable to post in
line--that is, answer paragraph A below paragraph A, paragraph B below
paragraph B, etc. (You haven't done this, but it's a common habit of
newcomers, so I figured I should mention it.)
Another thing you'll learn as you get more experienced is to give enough
information. It makes it easier for us to help you. I'm guessing,
(again, because you say you're new) that you're using bash, and trying
to write a shell script. A script could be in perl, python, or other
language too. You should have said, I'm running (for example) Fedora 8
and trying to do the following shell script, but it doesn't work--I get
an error message of... (or just say nothing happened or whatever.) As
it is, I have to try to guess.
Please don't take any of this as criticism, we understand that you're
new, and we've all made similar mistakes. (And, as I said, you haven't
yet made the top posting one.) :)
Ok, now on to your question. As I said, I have to sort of guess. I
imagine your script looks a little like
#!/bin/bash
echo "hello world"
It might have some other things. Anyway, let's take that one step by
step.
The first line #!/bin/bash
That will tell the script to use the bash shell, which is in the
directory known as bin. In Linux most programs are in /bin or in
/usr/bin. (If you want to make your scripts more portable, especially
in the early stages, you can use /bin/sh. In most Linux systems, it's a
link to /bin/bash, but the good thing is that if you then go to, for
example, FreeBSD, it will still work. However, if you put the script
starting with #!/bin/bash in FreeBSD, it won't work, because bash is
stored in a different directory. (/usr/local/bin/bash). That's just
something to keep in mind if you find yourself really getting into
scripting.
That first line is the only one where a # is not treated as a comment.
In every line but the first line, a # in front of the line means it's a
comment, not read by the shell. So you could do
#!/bin/bash
#this is a comment, it does nothing.
echo "hello world"
The next line is echo and then some words in quotes. In this case, you
can use either single quotes or double quotes. They do different things
in some cases, but in a simple two word thing, it won't matter. Typing
the echo command, and then some words simply prints the words on your
screen--it echoes them, so the name of the command makes sense.
Now, this is probably where you had a problem. In Linux, files will
have different sets of permissions. There is 4, for read, 3 for write
and 1 for execute. A typical set of permissions might be 755. This
means, the owner of the program can read, write and execute, the group
that owns the program can read and execute and everyone else can read
and execute. If you didn't change permissions on the script, and then
tried to run it, you get something like permission denied. So, after
writing your script, let's change the permissions on it. You can use a
command called chmod, as in change mode.
Let's assume you called your script hello.sh (The .sh is a common
suffix for shell scripts.)
chmod 755 hello.sh
Now, if you're in the directory where the script is, you can just run it
by going
./hello.sh
It should print hello world on your screen.
(The . in this case, refers to your current directory. You're probably
learning about moving around the system, into different directories.
Then, the /. This just indicates that it's something in the directory.
Another way to do it, without doing the chmod, would be to simply write
bash hi.sh
This calls the bash command, that is, your shell, and then runs the
script.
Hope this long winded explanation is of some use and didn't confuse you
further.
--
Scott Robbins
PGP keyID EB3467D6
( 1B48 077D 66F6 9DB0 FDC2 A409 FA54 EB34 67D6 )
gpg --keyserver pgp.mit.edu --recv-keys EB3467D6
Giles: I'll have you know that I have very, um, many
relaxing hobbies.
Buffy: Such as?
Giles: Well, um...I enjoy cross-referencing.
Buffy: Do you stuff your own shirts or do you send 'em out?
To unsubscribe from this list, please email [EMAIL PROTECTED] & you will be
removed.
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/LINUX_Newbies/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/LINUX_Newbies/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:[EMAIL PROTECTED]
mailto:[EMAIL PROTECTED]
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/