Well actually this isn’t the first Bash script that I write, but it sure is the first one that actually contains more than just one process and that also has options that the user can give it.
A friend of mine asked me to write a bash script that basically obtained some information from the OS, Linux in this case, and that it would store it on a file. The file would need to check if the file already exists and then create a new file if that’s the case. So let’s get started.
Armed with my Bash Cookbook I’ll begin to write this bash script on my ArchLinux box running the latest version of bash that is found on their repositories and using vim to actually write the script itself. Just in case you were wondering.
The script is not that complex actually, I guess the most complex part of the script is the part where you actually need to cut the data that is provided by other programs or commands, but besides that it’s just a basic script that takes some parameters which specify what the output of the program is.
Amongst the information that needs to be extracted by the scrip we have some system information:
- Devices attached to the system and if possible the device file that is associated with that specific device
- Display the amount of free space for each of the partitions and storage devices that are attached to the system
- The amount of free and used RAM and SWAP
- Obtain the information from the uname command
- The 10 biggest processes that are running on the system and the list of the current users that are connected to the system as well as the processes that they are running
- The program must be able to display the information on the STDOUT or, if the user wants, to a file who’s filename is obtained through parameters
The first thing that will be written is the main structure of the script. I’ll be using two methods of obtaining user commands, one will be through the usage of parameters and the other will be through the usage of an interactive menu, for the time being I’ll start with the interactive menu. Using the select command I will create the user menu and the actual options will be stored on a separate file called menulist. The user will have the option to use the number next to the option or the actual name of the option that they want executed.
Since there is an option to save the data to a file or to display the data on the STDOUT then, during the data gathering operation, all the data will be stored on a temp file and then either displayed on the STDOUT or saved to another specified file, depending on the user’s configurations.
The temp file is overwritten every time a command is executed, depending if the user specified a filename or not the data of the temp file will be sent to STDOUT or directly written to the user specified file, the user also has an option to disable the writing to a file and send the data back to STDOUT once more this is done by selecting the save command once more and typing “disable” instead of a file name.
The script doesn’t use functions and it came out being really big, something that can actually be optimized later on, due to this if one would like to modify the action of one command it would need to be done in the two sections of the script otherwise typing either the option number or the command would actually cause the script to have different output amongst the two. In order to optimize this the usage of specified functions would be recommended.
Currently the script consists of two files, one is the actual script and the other is the menu that gets displayed at runtime, a third file would be created at runtime. The script is interactive and cannot be backgrounded or demonized because of this, again the usage of functions would actually allow for this to occur.
Also the script doesn’t check for more advanced stuff and just does the basic checks, in other words it has not been debuged to ensure it’s stable, but it should be stable enough for the basic data.
Writing this script has been a great experience and I’ll try my luck at writing many more scripts and they shall include those features that weren’t included in this script. The script I just wrote will be posted later on.












can we see the script?
[quote comment=""]can we see the script?[/quote]
Sure, I will upload it as soon as I get back home…
[quote comment=""]can we see the script?[/quote]
OK, I have uploaded the script…finally
You can download it from http://files.codebeta.net/sysdata.zip
I must warn that the script is in spanish, well the messages and all, also the script is not the most efficient implementation but was kinda in a hurry to complete it.