Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Лаба_9_Операционные_Системы_ПДФ-1

.docx
Скачиваний:
0
Добавлен:
14.12.2022
Размер:
34.03 Кб
Скачать

Министерство цифрового развития, связи и массовых коммуникаций Российской Федерации

Ордена Трудового Красного Знамени федеральное государственное бюджетное образовательное учреждение высшего образования

«Московский технический университет связи и информатики»

(МТУСИ)

Кафедра «Математическая кибернетика и информационные технологии»

Лабораторная работа №9

на тему

«Создание и выполнение командных файлов в ОС UNIX»

Выполнил:

Студент 1 курса магистратуры

Группы М092201(75)

Юсифов Э.С.

Проверил:

Симонов Сергей Евгеньевич

Москва 2022

  1. Вариант №2

Вариант 2

1

3

8

15

18

  1. Задача 1

```bash

#!/bin/bash

# Check if any arguments were provided

if [ $# -eq 0 ]; then

echo "Error: no arguments provided"

exit 1

fi

# Print the arguments, along with their corresponding argument number

for ((i = 1; i <= $#; i++)); do

echo "$i: $1"

shift

done

  1. Задача 3

```bash

#!/bin/bash

# Create a list of files in the home directory, sorted by creation time

ls -rt $HOME > home_files.txt

# Print the list of files

echo "List of files in home directory:"

cat home_files.txt

# Print the total number of files

num_files=$(wc -l < home_files.txt)

echo "Total number of files: $num_files"

```

  1. Задача 8

```bash

#!/bin/bash

# Prompt the user for a username

read -p "Enter username: " user_name

# Check if the entered username matches the current user's username

if [ "$user_name" = "$USER" ]; then

echo "Correct username"

else

echo "Incorrect username"

fi

```

  1. Задача 15

```bash

#!/bin/bash

# Define the list of directories to search

dir_list=(/usr/local /usr/local/bin /usr/local/sbin)

# Prompt the user for the name to search for

read -p "Enter name to search for: " search_name

# Iterate through the list of directories and search for the specified name

for dir in "${dir_list[@]}"; do

if ls $dir | grep -q $search_name; then

echo "Name found in $dir"

fi

done

```

  1. Задача 18

```bash

# Parse the command line arguments

if [ $# -ne 2 ]; then

echo "Usage: $0 <key> <value>"

exit 1

fi

key=$1

value=$2

# Read the phone book file and split each line into its fields

while IFS=$'\t' read -r last_name address phone_number; do

# Search for a record that matches the given search key and value

case $key in

last_name)

if [ $last_name == $value ]; then

# Print the values of the other two fields

echo "address: $address"

echo "phone_number: $phone_number"

break

fi

;;

address)

if [ $address == $value ]; then

# Print the values of the other two fields

echo "last_name: $last_name"

echo "phone_number: $phone_number"

break

fi

;;

phone_number)

if [ $phone_number == $value ]; then

# Print the values of the other two fields

echo "last_name: $last_name"

echo "address: $address"

break

fi

;;

esac

done <phonebook.txt