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