Bash Shell Scripting

Overview: 

In this guided project from Coursera, I have acquired essential command-line skills by successfully completing a series of tasks, including navigating through directory structures, manipulating files, retrieving information, implementing aliases, writing scripts, and automating processes using cron. Throughout these tasks, I have developed proficiency in using commands to navigate directories, manipulate files, find information, create aliases, write scripts for tasks like file backup, and automate these processes through cron jobs.


Bash Shell Introduction:

Bash, short for "Bourne Again SHell" stands as a powerful and widely-used command-line interpreter for Unix-like operating systems. Serving as the default shell for Linux distributions and macOS, Bash provides a versatile interface between users and the underlying system. Its functionality extends beyond basic command execution, offering features such as scripting, variable manipulation, and conditional statements, making it a fundamental tool for system administrators, developers, and power users. With its scripting capabilities, Bash allows for the automation of repetitive tasks, enhancing efficiency and productivity in the management of files, processes, and system configurations.

Key Terminologies:

User Aliases: Bash user aliases are personalized shortcuts, enabling users to create concise commands for frequently used or complex operations. Configured in files like .bashrc or .bash_profile, these aliases enhance efficiency by reducing the need for repetitive typing, contributing to a more streamlined and user-friendly command-line experience.

Cron: Cron, a time-based job scheduler in Unix-like operating systems, automates the execution of tasks at predetermined intervals. Using a crontab file, users can schedule scripts, commands, or programs to run periodically, offering a powerful tool for task automation. This scheduling utility plays a crucial role in automating routine maintenance, backups, and other periodic processes.

echo: command is used to display text or output to the terminal. It is a simple command that takes arguments (text or variables) and prints them to the standard output (usually the terminal).

Shell Scripts: Enable the automation and customization of packet capture tasks, allowing users to specify conditions, filters, and actions to analyze network traffic efficiently.

Task 1: Navigated file directories

Used following commands

echo $SHELL  to see which shell is running

ls to see all directories


ls -al  to see all the hidden files with their detailed information such as file/directory, permissions, file size, file and directory names.

pwd  to see the path to  directory


ls –al to see what files in this directory

cd to change directory


Task 2: Manipulated Files

Used following commands

 touch myBashScript  to create a new file named myBashScript

mv myBashScript muchBetterName to rename myBashScript file, now its muchBetterName


rm muchBetterName  to delete file named muchBetterName


cat myNewFile.txt  to view the contents of the file named myNewFile


nano myNewFile.txt  to edit the contents of the file named myNewFile. To edit the file we need to open that file in text editor in this case nano is the text editor.



Task 3: Finding information about files, logs, command history, and file paths

Created a new directory newdir using command mkdir newDir



Searched the file using command find  -name “*backup*” 2>/dev/null | grep SUSER

Grep tells the shell to search for a regular expression.




Task 4: Aliases

Created an alias to find all backup files in the home directory alias fbackup=’find ~ -name “backup*”2>/dev/null’



Task 5: Created a Shell Script

Created a shell script to backup dev directory and email it. First I created a backup script file and opened it in nano text editor using command nano myBackup.  Then created a variable

BACKUP_PATH=”/home/rhyme/dev/”Used the date command and assigned it to the variable to backup daily DATE=`date+%d%m%Y` .

Created a backup file with extension.tar BACKUP=”backup_” and ‘EXT=”.TAR”


As the file is not executable and has only read write permissions
 

So, I changed the permissions using command chmod u=rwx myBackup this command gives my read, write and execute permissions.

I don’t want other users to have all these permissions and for this I used the command chmod go=rx myBackup which will allow other users only to read and execute myBackup file and they cannot write.


Used this tar cfz $FILE_NAME $BACKUP_PATH command which will archive and create a tar file of the backup dev directory and compresses it into a zip file.


Task 6: Automated this Shell Script using Cron

Used command If test –f”$FILE_NAME”; then echo mail –A $FILE_NAME –s “Today’s Backup”rhymedevkl@gmail.com

else echo ‘There is a problem creating backup file’ >> $ HOME_PATH/error.log

fi  

Which wil send backup in the tar zipped file to rhymedevkl@gmail.com email address with the subject Today’s Backup, and if the backup up file does not exist it will give this ‘There is a problem creating backup file’ >> $ HOME_PATH/error.log’ message and then redirect to HOME_PATH/error.log


Email received of backup file


To automate this job everyday at 2am I opened cron file using crontab  -e command and created a cron tab using  0 2 * * * /home/rhyme/myBackup command.

Lessons Learned:

·      Careful attention to command syntax and avoiding typos is essential for error-free command execution.

·       Understanding and managing file permissions is critical for successful file manipulation tasks.

·       Verifying the correct execution of scheduled tasks ensures the reliability of automated processes.

Conclusion:

In summary, this project has been a journey in mastering fundamental command-line operations. Navigating directories, manipulating files, and scripting for automation were key components. Challenges like syntax errors and file permissions were overcome through attention to detail and systematic problem-solving. The creation of a backup script and its automation using cron showcased practical application. Lessons in effective navigation, script development, and scheduled task validation were integral. This project not only provided valuable technical skills but also instilled a strategic and efficient mindset for command-line tasks in Unix-like environments.

CERTIFICATE OF COMPLETION



Comments