Question: Wrtie in BASH - VIM (response is supposed to be a BASH script) All successful applications should have some form of logging. This keeps track
Wrtie in BASH - VIM (response is supposed to be a BASH script) All successful applications should have some form of logging. This keeps track of application states, errors, warnings, and etc. Logs can be kept for security, for debugging, performance monitoring, or any number of reasons. Log files are often split into multiple files. One scheme of managing this is to have a log file for each day. In a sample application, assume that logs are kept as follows (note, this may not be best practice): log/application.0.log (this is todays general application log) log/application.1.log (this is yesterdays general application log) log/application.2.log (this is the general application log from 2 days ago) ... log/security.0.log (this is todays security log) log/security.1.log (this is yesterdays security log) log/security.2.log (this is the security log from 2 days ago) ... In order to keep these files accurate, the engineers will schedule a nightly process to rotate the logs. This rotation will increment the number in each file, and archive (or in our case, delete) any files older than a specified number of days. A new, empty, log file should also be created. Logs can have any name, but will always follow the pattern logtype.number.log where the number is the current age of the file. Make sure that your application will work for any number of logtypes. Log files are the ONLY things kept in the log folder, so don't worry about items not matching this pattern. Remember that no log files are required to exist, and the program should handle this condition gracefully. Remember that logrot may have been run with a larger number of logs kept before, and you will need to delete all logs numbered greater than your input parameter. In a file called logrot, implement log rotation in bash. logrot takes a required integer parameter, and an optional string parameter. The integer is the number of previous logs to keep, while the string is the path to the directory the logs are kept in (defaults to log). In this program you should validate that the integer parameter is provided and is a number. If a second parameter is provided, make sure the path given is to a directory and that you have write access to it. Do not worry about individual log files having write permissions. Here are several sample runs of the program. Lines starting with '>' are responses from the program. Lines starting with '#' are not printed, but a description of what action the program takes. ./logrot > number of days of logs to keep is required to be a non-negative integer ./logrot -3 > number of days of logs to keep is required to be a non-negative integer ./logrot 5 Not_A_Writeable_Directory > Not_A_Writeable_Directory must be a path to a log directory with write access for $USER ./logrot 1 # moves log/*.0.log to log/*.1.log # creates new log/*.0.log # deletes all other .log files ./logrot 2 otherfolder # moves otherfolder/*.1.log to otherfolder/*.2.log # moves otherfolder/*.0.log to otherfolder/*.1.log # creates new otherfolder/*.0.log # deletes all other .log files
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
