Home

The Bash Shell

Overview

I worked with:

Task 2: Create an alias for a backup operation

I needed to create an alias that gives me the ability to back up whatever path I provide it.

I had to use sudo to complete this task since I was not root.

First, I had to create an alias that uses the tar to back up the second parameter provided into the first parameter. Here's the command line example:

Usage example: backup "fileToSaveTo.tar.gz" "pathToBackUp"

To validate that I was in the home folder in the terminal, I entered the following command:

pwd

Expected output:

[ec2-user@ ~]$ pwd
/home/ec2-user/

To create an alias called backup, I entered the following command:

alias backup='tar -cvzf '

I remembered that tar is a command that I use to create or extract an archive that contains files and folders.

tar -cf would work perfectly but would not display what is inside the archive and would not compress it.

To use the backup alias to back up the CompanyA folder, I entered the following command:

backup backup_companyA.tar.gz CompanyA

Expected output:

*The command backup backup_companyA.targz CompanyA shows the entire directory of Company A. The contents include folders such as Management, Employees, Finance, HR, IA, SharedFolders, and bin. Files include the following: Sections.csv, Promotions.csv, Schdules.csv, Salary.csv, Managers.csv, Assessments.csv, and hello.sh.*

[ec2-user@ ~]$backup backup_companyA.tar.gz CompanyA
CompanyA/
CompanyA/Management/
CompanyA/Management/Sections.csv
CompanyA/Management/Promotions.csv
CompanyA/Employees/
CompanyA/Employees/Schedules.csv
CompanyA/Finance/
CompanyA/Finance/Salary.csv
CompanyA/HR/
CompanyA/HR/Managers.csv
CompanyA/HR/Assessments.csv
CompanyA/IA/
CompanyA/SharedFolders/
CompanyA/bin/hello.sh

To verify that the archive was created, I entered the ls command:

[ec2-user@ ~]$ ls
backup_companyA.tar.gz CompanyA

Task 3: Explore and update the PATH environment variable

In this task, I displayed the PATH environment variable. I then updated the variable and added a new directory, in which I can place executables.

To navigate to the bin folder in the home CompanyA directory, I entered the following command:

cd /home/ec2-user/CompanyA/bin
I can also use the pwd command to verify that I am in the home folder, /home/ec2-user, and use cd CompanyA/bin to enter the /home/ec2-user/bin folder.

To run the hello.sh script, I entered the following command:

./hello.sh

Expected Output:

[ec2-user@ bin]$ hello.sh
hello ec2-user

To navigate to the parent folder, I entered the following command:

cd ..

Expected Output:

[ec2-user@ bin]$ cd ..
[ec2-user@ CompanyA]$

To run the hello.sh script again, I entered the following command:

./bin/hello.sh

Expected Output:

[ec2-user@ CompanyA]$ ./bin/hello.sh
hello ec2-user

To run the hello.sh script again, I entered the following command:

hello.sh

Expected Output:

[ec2-user@ CompanyA]$ hello.sh
bash: hello.sh: command not found
I analyzed the three different ways I tried to run the hello.sh script. In the next step, I figured out why the third run failed and how to solve this issue.

To display the value of the PATH variable, I entered the following command:

echo $PATH

Expected Output:

[ec2-user@ CompanyA]$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/.local/bin:/home/ec2-user/bin
I remembered that the PATH variable is a list of folders where the system looks for executables and libraries. If I enter a command such as hello.sh, Linux will look for the script in the current folder and then in all the folders contained in the PATH variable. I could see that /home/ec2-user/bin is not listed. There were currently only two ways to run the script:
- Navigate to the /home/ec2-user/CompanyA/bin folder, and enter hello.sh to run the script.
- Enter /home/ec2-user/CompanyA/bin/hello.sh from any folder.

To add the /home/ec2-user/CompanyA/bin folder to the PATH variable, I entered the following command:

PATH=$PATH:/home/ec2-user/CompanyA/bin

Expected Output:

[ec2-user@ CompanyA]$ PATH=$PATH:/home/ec2-user/CompanyA/bin

To try to run the hello.sh script again, I entered hello.sh:

[ec2-user@ CompanyA]$ hello.sh
hello ec2-user

Related Topics