0% found this document useful (0 votes)
10 views

Assessment 2 - Linux

Uploaded by

727823tuam009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Assessment 2 - Linux

Uploaded by

727823tuam009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1

ADP - Assignment 2

Linux Shell Scripting:

Q 1. You have a directory with multiple text files. Write a shell script that renames
all files in the directory by adding a timestamp to their names.

bash
#!/bin/bash

# Get the current timestamp


timestamp=$(date +"%Y%m%d%H%M%S")

# Iterate over each file in the directory


for file in *; do
# Check if the file is a regular file
if [ -f "$file" ]; then
# Get the file extension
extension="${file##*.}"

# Generate the new file name with the timestamp


new_name="${file%.*}_${timestamp}.${extension}"

# Rename the file


mv "$file" "$new_name"

# Display the renamed file


echo "Renamed $file to $new_name"
fi
done

To use this script, save it in a file with a `.sh` extension (e.g., `rename_files.sh`),
make it executable (`chmod +x rename_files.sh`), and then run it
(`./rename_files.sh`). The script will add a timestamp to the names of all files in the
directory and display the renamed files.

Q 2. Create a shell script that takes a number as input and checks if it's even or
odd. If it's even, use a loop to print the numbers from 2 to the input number.

bash
#!/bin/bash

# Prompt the user to enter a number


read -p "Enter a number: " number

# Check if the number is even or odd


if ((number % 2 == 0)); then
echo "The number is even."
echo "Numbers from 2 to $number:"

# Loop to print numbers from 2 to the input number


for ((i = 2; i <= number; i += 2)); do
echo "$i"
done
else
echo "The number is odd."
fi
2

Q 3. Write a script that takes two filenames as command line arguments. The script
should compare the contents of the two files and display a message indicating
whether they are identical or not.

bash
#!/bin/bash

# Check if two filenames are provided as command line arguments


if [ $# -ne 2 ]; then
echo "Usage: ./compare_files.sh <file1> <file2>"
exit 1
fi

# Assign the filenames to variables


file1=$1
file2=$2

# Check if both files exist


if [ ! -f "$file1" ] || [ ! -f "$file2" ]; then
echo "One or both files do not exist."
exit 1
fi

# Compare the contents of the two files


if cmp -s "$file1" "$file2"; then
echo "The contents of $file1 and $file2 are identical."
else
echo "The contents of $file1 and $file2 are different."
fi

Q 4. Develop a script that defines a function to calculate the factorial of a given


number. The script should take user input for a number and use the function to
display the factorial.

bash
#!/bin/bash

# Define the factorial function


factorial() {
if [ $1 -eq 0 ]; then
echo 1
else
local result=1
for ((i=1; i<=$1; i++)); do
result=$((result * i))
done
echo $result
fi
}

# Prompt the user to enter a number


read -p "Enter a number: " number

# Call the factorial function with the user input


result=$(factorial $number)

# Display the factorial


echo "The factorial of $number is: $result"
3

Q 5. Create a script that runs a time-consuming task in the background. Then,


schedule this script to run every day at a specific time using the `cron` utility.

bash
#!/bin/bash

# Run the time-consuming task in the background


(time-consuming-task &) > /dev/null 2>&1

# Display a message indicating that the task has started


echo "Time-consuming task has started."

# Schedule the script to run every day at a specific time using cron
# Replace "0 0 * * *" with your desired schedule (e.g., "30 9 * * *" for 9:30 AM every
day)
echo "0 0 * * * $(pwd)/$(basename "$0")" | crontab -

# Display a message indicating that the scheduling is complete


echo "Script has been scheduled to run every day at a specific time."

To use this script, save it in a file with a `.sh` extension (e.g., `schedule_task.sh`),
make it executable (`chmod +x schedule_task.sh`), and then run it
(`./schedule_task.sh`). The script will run the time-consuming task in the background,
display a message indicating that the task has started, schedule the script to run
every day at a specific time using `cron`, and display a message indicating that the
scheduling is complete.

Make sure to replace the `time-consuming-task` placeholder with the actual


command or script that represents your time-consuming task. Additionally, modify the
`0 0 * * *` schedule in the `echo` command to match your desired time (in cron
format).

Linux Basic Commands:

Q 1. You are a system administrator. Explain how to change the permissions of a


file so that only the owner can read and write to it, but others cannot access it.
Provide the necessary commands and permissions settings.

To change the permissions of a file so that only the owner can read and write to it,
while others cannot access it, you can use the `chmod` command in Linux. Here's
how you can do it:

1. Open a terminal and navigate to the directory where the file is located.

2. Use the `ls -l` command to list the files in the directory and view their current
permissions. Identify the file for which you want to change the permissions.

3. To change the permissions, use the `chmod` command followed by the


permission settings and the filename. In this case, you want to give read and write
permissions to the owner, and remove all permissions for others. The permission
settings are represented by three digits:

- The first digit represents the owner's permissions.


- The second digit represents the group's permissions.
- The third digit represents others' permissions.

The permission settings are as follows:


4

- 4: Read permission
- 2: Write permission
- 1: Execute permission
- 0: No permission

To give read and write permissions to the owner and remove all permissions for
others, you can use the following command:

bash
chmod 600 filename

Here, `6` represents read and write permissions for the owner, and `0` represents no
permissions for the group and others.

4. After executing the command, you can verify the changed permissions by using
the `ls -l` command again. The file should now have the desired permissions.

Q 2. Write a script that finds and displays the top 5 processes consuming the most
CPU and memory on your system.

bash
#!/bin/bash

# Get the top 5 processes consuming the most CPU


echo "Top 5 processes consuming the most CPU:"
ps -eo pid,ppid,cmd,%cpu --sort=-%cpu | head -n 6

# Get the top 5 processes consuming the most memory


echo "Top 5 processes consuming the most memory:"
ps -eo pid,ppid,cmd,%mem --sort=-%mem | head -n 6

To use this script, save it in a file with a `.sh` extension (e.g., `top_processes.sh`),
make it executable (`chmod +x top_processes.sh`), and then run it
(`./top_processes.sh`). The script will display the top 5 processes consuming the
most CPU and memory on your system.

Q 3. Create a script that compresses a directory and its contents into a tarball. Set it
up to be run as a scheduled task using `cron` every Sunday night.

bash
#!/bin/bash

# Directory to be compressed
directory="/path/to/directory"

# Destination directory for the tarball


destination="/path/to/destination"

# Create a timestamp for the tarball filename


timestamp=$(date +%Y%m%d%H%M%S)

# Compress the directory into a tarball


tar -czf "$destination/archive_$timestamp.tar.gz" "$directory"

# Display a success message


echo "Directory compressed successfully."
5

To set up this script as a scheduled task using `cron` to run every Sunday night,
follow these steps:

1. Open the crontab file for editing by running the command `crontab -e`.

2. Add the following line to the crontab file to schedule the script to run
every Sunday at 11:59 PM:

59 23 * * 0 /path/to/script.sh

This line specifies that the script should be executed at 11:59 PM


(23:59) every Sunday (0).

3. Save the crontab file and exit the editor.

Now, the script will run automatically every Sunday night at the specified time,
compressing the specified directory into a tarball and saving it to the destination
directory.

Introduction to Git:

Q 1. Imagine you are starting a new software project. Describe the steps you would
take to create a new Git repository for your project and link it to a remote repository
on GitHub.

1. Create a new repository on GitHub:


- Go to GitHub (https://github.com) and sign in to your account.
- Click on the "+" button in the top-right corner and select "New repository".
- Provide a name for your repository, choose any additional settings you
need, and click "Create repository".

2. Set up Git on your local machine:


- Install Git on your computer if you haven't already (https://git-
scm.com/downloads).
- Open a terminal or command prompt and navigate to the directory where
you want to create your project.

3. Initialize a new Git repository:


- Run the following command to initialize a new Git repository in your project
directory:

git init

4. Link the local repository to the remote repository:


- Copy the URL of your remote repository from the GitHub page (e.g.,
`https://github.com/your-username/your-repo.git`).
- Run the following command to add the remote repository:

git remote add origin <remote-repository-url>

Replace `<remote-repository-url>` with the URL you copied.

5. Add and commit your project files:


- Add your project files to the Git repository using the following command:

git add .
6

This will add all the files in the current directory to the staging area.
- Commit the changes with a meaningful message using the following
command:

git commit -m "Initial commit"

6. Push the local repository to the remote repository:


- Run the following command to push your local repository to the remote
repository:

git push -u origin master

This will push the changes to the `master` branch of the remote repository.

Q 2. You are working on a feature for your project. Create a Git branch for this
feature, make several commits, and then merge the feature branch back into the main
branch (usually 'master').

1. Start by ensuring you are on the main branch:

git checkout master

2. Create a new branch for your feature:

git branch feature-branch

3. Switch to the feature branch:

git checkout feature-branch

4. Make your desired changes to the project and commit them:

git add .
git commit -m "Commit message 1"

Repeat the above two commands as needed to make additional commits.

5. Once you have made all the necessary commits on the feature branch, switch
back to the main branch:

git checkout master

6. Merge the feature branch into the main branch:

git merge feature-branch

If there are no conflicts, Git will automatically perform the merge. If there are
conflicts, you will need to resolve them manually.

7. Push the changes to the remote repository:

git push origin master

This will update the main branch on the remote repository with the changes
from the feature branch.
7

Q 3. You are collaborating with a team on GitHub. Explain how you would clone the
repository, make changes, and push those changes to the remote repository.

1. Clone the repository:


- Go to the GitHub repository page.
- Click on the "Code" button.
- Copy the repository URL.
- Open your terminal or command prompt.
- Navigate to the directory where you want to clone the repository.
- Run the following command, replacing `<repository-url>` with the URL you
copied:

git clone <repository-url>

- This will create a local copy of the repository on your machine.

2. Make changes:
- Navigate into the cloned repository directory using the `cd` command.
- Use a text editor or any other tool to make the desired changes to the files in
the repository.

3. Stage and commit your changes:


- Run the following command to stage the changes you made:

git add .

- This will stage all the modified files for commit. If you only want to stage
specific files, replace `.` with the file names.
- Run the following command to commit the changes:

git commit -m "Your commit message"

- Replace `"Your commit message"` with a descriptive message summarizing


the changes you made.

4. Push the changes to the remote repository:


- Run the following command to push your changes to the remote repository:

git push

- If it's your first time pushing to the repository, you may need to specify the
remote branch using:

git push -u origin <branch-name>

Replace `<branch-name>` with the name of the branch you want to push to.

5. Enter your GitHub credentials if prompted.

After successfully pushing your changes, they will be reflected in the remote
repository, and your team members will be able to see and review them.
8

Software Development Models and Agile:

Q 1. Your company is about to start a new software development project. Discuss


the advantages and disadvantages of using the Waterfall model versus the Agile
model, and recommend which one to use for your project.

Waterfall Model:

Advantages:

1. Clear and well-defined project scope: The Waterfall model follows a


sequential approach, with each phase building upon the previous one. This allows for
a clear understanding of the project scope from the beginning.

2. Predictability: The linear nature of the Waterfall model makes it easier to


estimate project timelines and costs.

3. Documentation: The Waterfall model emphasizes comprehensive


documentation, which can be beneficial for projects with strict regulatory or
compliance requirements.

Disadvantages:

1. Limited flexibility: The Waterfall model does not easily accommodate changes
or iterations once a phase is completed. This can be problematic if requirements
change or if feedback from stakeholders is received later in the project.

2. Late feedback: Since testing and user feedback typically occur towards the
end of the project, any issues or necessary changes may be identified late in the
development cycle, leading to potential delays and increased costs.

3. Lack of stakeholder involvement: The Waterfall model often limits stakeholder


involvement to the initial requirements gathering phase, potentially resulting in a final
product that does not fully meet their needs.

Agile Model:

Advantages:

1. Flexibility and adaptability: The Agile model allows for iterative development,
enabling changes and adjustments to be made throughout the project based on
feedback and evolving requirements.

2. Continuous stakeholder involvement: Agile methodologies emphasize regular


collaboration and feedback from stakeholders, ensuring that the final product aligns
with their expectations.

3. Early detection of issues: Frequent testing and iterations in Agile allow for
early detection and resolution of issues, reducing the risk of major problems later in
the development process.

Disadvantages:

1. Increased complexity: Agile methodologies can be more complex to manage,


requiring dedicated team members and effective communication to ensure smooth
collaboration and coordination.

2. Potential for scope creep: The flexibility of Agile can lead to scope creep if
requirements are not properly managed and controlled.
9

3. Uncertain timelines and costs: Agile projects may have less predictable
timelines and costs due to the iterative nature of development.

Based on the provided context of the project being a software development project,
the Agile model may be more suitable. The digital marketing landscape is constantly
evolving, and an Agile approach can provide the necessary flexibility to adapt to changing
requirements and market conditions. Additionally, the involvement of stakeholders
throughout the development process can help ensure that the final product meets their
needs effectively. However, it is important to consider the specific requirements and
constraints of the project before making a final recommendation.

Q 2. You are part of a Scrum team. Explain how the Scrum framework and its core
practices like sprint planning, daily stand-up, and retrospectives help in managing
and delivering a software project.

As part of a Scrum team, the Scrum framework and its core practices play a crucial
role in managing and delivering a software project effectively. Here's how each of
these practices contributes to the overall success of the project:

1. Sprint Planning:
Sprint planning is a collaborative meeting where the team determines what
work will be done during the upcoming sprint. It involves two main questions:
"What will we do this sprint?" and "How will we do it?". This practice helps in
setting clear goals and priorities for the team, ensuring that everyone is
aligned on what needs to be accomplished. By breaking down the work into
manageable tasks, the team can estimate effort and plan accordingly.

2. Daily Stand-up (Scrum):


The daily stand-up, also known as the daily Scrum, is a short and focused
meeting held every day. Each team member shares three things: what they
did yesterday, what they plan to do today, and any obstacles they are facing.
This practice promotes transparency, collaboration, and accountability within
the team. It helps identify and address any issues or roadblocks early on,
allowing the team to adapt and make necessary adjustments to stay on track.

3. Retrospectives:
Retrospectives are held at the end of each sprint to reflect on the team's
performance and identify areas for improvement. During this meeting, the
team discusses what went well, what could have been done better, and any
potential changes to enhance future sprints. Retrospectives foster a culture of
continuous improvement, enabling the team to learn from their experiences
and make adjustments to their processes, communication, and collaboration.
This practice helps in refining the team's approach and increasing their
efficiency and effectiveness over time.

By following these core practices of the Scrum framework, a software project can
benefit in several ways:

1. Improved Collaboration: Scrum promotes collaboration and cross-functional


teamwork. By having regular meetings and open communication channels, team
members can share knowledge, coordinate efforts, and work together towards a
common goal.

2. Increased Transparency: The Scrum practices provide transparency into the


progress of the project. Sprint planning, daily stand-ups, and retrospectives ensure
that everyone is aware of the work being done, any challenges faced, and the overall
project status. This transparency helps in identifying and addressing issues promptly.
10

3. Adaptability and Flexibility: Scrum embraces change and allows for


flexibility in project management. The iterative nature of sprints enables the team to
adapt to evolving requirements, customer feedback, and market dynamics. The
regular feedback loops provided by the core practices help in making timely
adjustments and delivering value incrementally.

4. Continuous Improvement: The retrospective practice encourages the team


to reflect on their performance and identify areas for improvement. By continuously
learning from their experiences and making small, incremental changes, the team
can enhance their processes, productivity, and overall project outcomes.

Overall, the Scrum framework and its core practices provide a structured approach to
managing software projects, fostering collaboration, adaptability, and continuous
improvement. By following these practices, teams can effectively plan, execute, and
deliver high-quality software products.

Q 3. Compare and contrast Extreme Programming (XP) and Lean Software


Development. Describe the key principles and practices of each and when they might
be suitable for different projects.

Extreme Programming (XP) and Lean Software Development are both agile software
development methodologies that aim to improve the efficiency and effectiveness of software
development projects. However, they have different principles and practices. Let's compare
and contrast them:

Extreme Programming (XP):

Principles:

1. Communication: Emphasizes frequent and open communication between


team members, stakeholders, and customers.
2. Simplicity: Focuses on delivering the simplest solution that meets the
requirements.
3. Feedback: Encourages continuous feedback through regular testing,
reviews, and customer involvement.
4. Courage: Promotes taking risks, embracing change, and making necessary
adjustments to deliver high-quality software.
5. Respect: Values the expertise and contributions of all team members.

Key Practices:

1. Test-Driven Development (TDD): Developers write tests before writing


code, ensuring that the code meets the desired functionality.
2. Continuous Integration (CI): Developers integrate their code frequently,
allowing for early detection and resolution of integration issues.
3. Pair Programming: Two developers work together on the same code,
promoting knowledge sharing, code quality, and collaboration.
4. Iterative Development: Software is developed in short iterations, with regular
releases and feedback loops.
5. On-site Customer: A representative from the customer's side is actively
involved in the development process.

Suitability:

Extreme Programming is suitable for projects where requirements are likely to


change frequently, and there is a need for rapid feedback and continuous
improvement. It works well for small to medium-sized teams that value collaboration,
communication, and flexibility.
11

Lean Software Development:

Principles:

1. Eliminate Waste: Focuses on reducing any activities or processes that do


not add value to the final product.

2. Amplify Learning: Encourages continuous learning and improvement


through experimentation, feedback, and reflection.

3. Decide as Late as Possible: Defers decisions until the last responsible


moment to gather more information and make informed choices.

4. Deliver Fast: Strives to deliver value quickly to customers, enabling early


feedback and validation.

5. Empower the Team: Empowers team members to make decisions, take


ownership, and continuously improve the process.

Key Practices:

1. Value Stream Mapping: Identifies and eliminates non-value-added activities


in the software development process.

2. Kanban: Visualizes the workflow, limits work in progress, and optimizes the
flow of work.

3. Just-in-Time (JIT): Delivers work just in time to avoid unnecessary delays or


inventory buildup.

4. Continuous Improvement: Encourages regular reflection, learning, and


process optimization.

5. Cross-Functional Teams: Teams consist of members with diverse skills to


promote collaboration and reduce dependencies.

Suitability:

Lean Software Development is suitable for projects where efficiency, waste


reduction, and continuous improvement are critical. It works well for projects with
stable requirements and a focus on delivering value quickly. Lean principles can be
applied to both small and large teams.

In summary, Extreme Programming (XP) emphasizes communication, simplicity, and


feedback, while Lean Software Development focuses on waste reduction, learning, and
delivering value. XP is suitable for projects with changing requirements and a need for
flexibility, while Lean is suitable for projects that prioritize efficiency, continuous
improvement, and value delivery. The choice between the two methodologies depends on
the specific project context and goals.

*******************

You might also like