I’ll Give you a simple introduction to Bash Scripting
Before I talk about scripting, Allow me to tell you a little about bash.
Bash is a CLI (Command Line Interface) Shell , that is a software that provides an interface allowing you to access the services of the kernel “Simply a program that allows you to execute other programs”
If you want to know more about it please check these links :
Moving On ..
I once Ran into a book “Apress Pro Bash Programming Scripting GNU Linux shell” and I’m still reading it, the thing about this book is that the author created a menu driven, user extensible data base system with a report Generator Just using shell scripting. I still don’t have a clue how, but I sure do want to know ^^.
So why am I saying this?
Well, I think it proves how powerful shell scripting could be, this man really got me interested in shell scripting.
>>Now Let’s Start:
The example I’ll work on is a script for creating script files 🙂 You probably know that the main purpose of scripts is to automate .
1- Of course- you’ll create a file: many people tend to create a /bin directory for the user… so move to the home directory and create the /bin
most people use .sh as an extension to distinguish script files, it doesn’t really make a difference for now.
2– Now to edit the file
** use vim , nano , gedit whatever suits u best. If you are not used to CLI stick with gedit for now
and type the following:
and save it.
** #! : tells the system which interpreter will be used to execute a file.
** touch: is a command that creates files.
** $1: a script can receive arguments. Labeled starting $1, so this refers to the first argument it’ll receive.
** chmod +x $1 : change permissions of this file to executable
3- Make the file executable
Now If you are in the directory this file was created ,typing “#: ./cshell file1“ will create a file named file1 and make it executable .
Saying that you want it to be executed like a command, when you type in a command the shell searches for it in directories stored in $PATH variable so you just have to.
store your directory in the PATH variable , to do so :
BUT, this modification will be lost if you log out “On Fly”. If you want to be permanent just add the previous line to “.bashrc“
Move to your home directory
Edit the file
add the line
save and you’re done!
To save all this just create the file in /bin. It’s in the $PATH by default, but you’ll have to be root
Now that you’re done move anywhere and type
and a new executable file named first has been created 🙂
That’s it, it’s your first bash script.