Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Lab work 5: Write your own comments to the tasks.

If you need to print


something use your name and surname as an output!
n

1. Additional info from IBM course:


https://docs.google.com/document/d/
1vQApybzgEYUbq3Otoab2lUqVy9bSlSMxE9P9B3RGZfM/edit
https://docs.google.com/document/d/1DPFXInS4qZJ4FJzAot4f76quJCTZPtLZztWBYTdCv4k/
edit
https://docs.google.com/document/d/19YGkD5TJiCsmqfLwybplkA8-lGUxeRcz-ubP9qOJ704/
edit
a) Creation of text files: (a1) Create the labC05 folder in your home folder (test user's home
folder). (a2) Create a text file (Year_Surname) in the labC05 folder. (a3) Add to the file your
Name, Surname, id of your group. (a4) Add to the file listing of files and folders in your home
folder. (a5) How many symbols, bytes, words and lines are in the file? Write a bash script that
takes a filename as an argument and outputs the number of lines in the file.
#!/bin/bash
# Check if a filename is provided as an argument
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <filename>"
exit 1
fi
filename="$1"
# Check if the file exists
if [ ! -e "$filename" ]; then
echo "Error: File not found"
exit 1
fi
# Use wc to count the number of lines
line_count=$(wc -l < "$filename")
echo "Number of lines in $filename: $line_count"
2. (b1) Create a text file in labC05 (yourname.txt), containing six lines:
1. the first
2. the second
3. 3th
4. 4th
5. the next
6. the last
(b2) Show the first two lines of the yourname.txt file. (b3) Show the last two lines of the
yourname.txt file. (b4) Show the 3th & 4th lines of the yourname.txt file. Write a bash script that
takes a filename as an argument and outputs the content of the file.
#!/bin/bash
# Check if a filename is provided as an argument
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <filename>"
exit 1
fi
filename="$1"
# Check if the file exists
if [ ! -e "$filename" ]; then
echo "Error: File not found"
exit 1
fi
# Display the content of the file
cat "$filename"
3. (c) Cutting files
(c1) The /etc/passwd file contains user accounts information. Each record is in a line and consists
of number of fields, divided by the ":" symbol. Show the first five records from /etc/passwd (c2)
The 1st filed is user login, the 3th is user id. Show sorted list of the first five logins and their IDs
from /etc/passwd. (c3) The last field of user record contains a shell name. How many users have
nologin as a shell? (create pipeline) (c4) Create a pipeline, that shows a list of user logins, having
the/bin/bash shell. (c5) Create list of the shells, that are used in the /etc/passwd file.

4. Write a script that uses the "lshw" command to generate a report of the system hardware. The
script should output the report to a file and include information on the CPU, memory, storage
devices, and other hardware components.

#!/bin/bash
# Define the output file path and name
output_file="system_hardware_report.txt"
# Run lshw and redirect the output to the specified file
sudo lshw > "$output_file"
# Check if lshw was executed successfully
if [ $? -eq 0 ]; then
echo "System hardware report generated and saved to $output_file"
else
echo "Error: Failed to generate the system hardware report"
fi

Create a simple script that prints the current date and time to a file. Schedule script execution
every 5 minutes. Check the task execution using the system operation logs.

#!/bin/bash
# Define the output file
output_file="/labcaCO5/2023Kanat" # Replace with the actual file path
# Get the current date and time and append it to the file
date "+%Y-%m-%d %H:%M:%S" >> "$output_file"

You might also like