Enter your email address:

Delivered by FeedBurner

Simple shell script


StumbleUpon Toolbar

Maybe most of the readers of this blog do already know about shell scripts, and maybe they know even more than me (which is not difficult), but for those who does not know about it, I will write a little about shell scripts.

A shell script is a sequence of commands -For those coming from DOS is like a .bat file- this command will execute in the sequence they are entered, unless loops, if, do, for, or any other commands like those are used.

Writing the script

Lets go with the classic example, of "Hello World"

First just use your favorite text editor, (do not use a word processor), and write this inside.

#!/bin/sh
clear
echo "Hello World"

lets say you named this file, myfirstscript.sh, you can name it almost anything you want, but try to use a comprehensive name, and use the .sh extension, for better human reading.

Making it executable

Now we need to tell Linux that this is an executable file, so the operating system may know how to treat with it, to do that execute

chmod +x myfirstscript.sh

Explaining the script a little

The first like tells Linux which interpreter to use, there are a lot of different interpreters, some are:

  • #!/bin/bash — Execute using the Bourne-again shell
  • #!/bin/bash -c '/bin/bash' — Execute using bash in the /bin/ directory, and calls bash inside the /bin/
  • #!/usr/bin/cowsay - Execute using cowsay
  • #!/bin/csh — Execute using csh, the C shell
  • #!/bin/ksh — Execute using the Korn shell
  • #!/bin/awk — Execute using awk program in the /bin/ directory
  • #!/bin/sh — On some systems, such as Solaris, this is the Bourne shell. On Linux systems there is usually no Bourne shell and this is a link to another shell, such as bash. According to the Single UNIX Specification's requirements for /bin/sh, such a shell will usually mimic the Bourne shell's behaviour, but be aware that using

you can learn more on Wikipedia

Running the script

To run it just go to the directory where it is saved and run:

./myfirstscript.sh

Remember to use ./ when you are on the same directory or it will not execute, also you can save it in a directory which is included in you path, or include its directory in you path variable.

I will continue with this topic later.

Trackback URL for this post:

http://www.go2linux.org/trackback/447
StumbleUpon Toolbar

 If you like this article, subscribe to our full rss

If this article was somehow useful for you, you can leave something in the tip's jar

Please post your question in our forum and use comments only to leave your comments about the article, thanks.

I prefer using a real

I prefer using a real language like Perl, Python etc. to create my shell scripts if its involves even one control flow statement(stuff like if, for etc.).

“Wow,” it is really good

“Wow,” it is really good to say a post from someone that knows a topic well and is able to get their point across. I am really looking forward to your following post.

I accepted to take a instant and thank you for the post you wrote.

Thanks and best of luck!

Thank you very much for your

Thank you very much for your kind words!

Guillermo Garron

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

Captcha
This question is for testing whether you are a human visitor and to prevent automated spam submissions.

This site is proudly hosted at Bluefur Hosting