Bash script basics
When you are using Linux at the command line you are working in a shell environment. By default in Linux it would be bash. You can select from a number of shells, but bash is definitely the most popular. What is really nice when working in a shell environment is not only can you do just regular work there, you can also write programs to automate tasks. And shell programming can get quite complex and powerful. There are some definite differences in the different shells in Linux and Unix. We will focus on bash here.
Let's cover the most basic parts of a bash shell script. At the most basic you are just running one Linux command after another. The first line though will state what shell to run under. Technically if you are starting the script from bash you don't need it because it will run in bash. But it is always good to put it in there just to make sure. Then you will put in the lines you want.
This example will get the disk free space and put it into a text file then email it out to a person. It includes two lines to tell you when the script starts and when it ends.
Let's cover the most basic parts of a bash shell script. At the most basic you are just running one Linux command after another. The first line though will state what shell to run under. Technically if you are starting the script from bash you don't need it because it will run in bash. But it is always good to put it in there just to make sure. Then you will put in the lines you want.
This example will get the disk free space and put it into a text file then email it out to a person. It includes two lines to tell you when the script starts and when it ends.
Basic script
So this would be what the script looks like. You would save it to a file of course. Linux does not really care much about extensions like Windows. But I typically will end my file name either .sh or .bash so I can find scripts easy. So this one might be named mydiskstatus.sh or something.
The other thing you will need to do before it will run is make it executable of course. To make it executable just by you you can run the chmod command to add the execute permission for yourself.
The other thing you will need to do before it will run is make it executable of course. To make it executable just by you you can run the chmod command to add the execute permission for yourself.
chmod u+x mydiskstatus.sh
Now all you need to do is to run the command. By default on Linux executable files that are not in the environment path won't run by specifying just the filename. This is actually a security feature. So you have to give the path to the file when you run it. You can do a full path from root, or a relative path from where you are at. Thus if this file is in your home directory you could run it one of these ways. (The first assumes your in your home drive when you run it)
./mydiskstatus.sh
~/mydiskstatus.sh
/home/asg/mydiskstatus.sh
This is a very very basic example on how to create a simple script and get it working for you. This example is just barely looking at the surface of everything you can do. You can add a lot of different programming features to the shell script to make it much more intelligent. Those are covered in other topics on this site.
Home |
About |
Services |
Copyright © 2016