This script copies all .txt files from a specified directory to a backup subdirectory, then prints a success message. It checks that the given directory exists and lists the .txt files before creating the backup directory and copying the files.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
16 views
Bash Script Test A
This script copies all .txt files from a specified directory to a backup subdirectory, then prints a success message. It checks that the given directory exists and lists the .txt files before creating the backup directory and copying the files.
names_file="data.txt" if [ ! -f "$names_file" ]; then echo "File not found: $names_file" exit 1 fi
# Count the total number of names
total_names=$(wc -l < "$names_file")
# Display unique names in alphabetical order
echo "Unique names in alphabetical order:" sort -u "$names_file"
# Prompt user to enter a name
read -p "Enter a name to check: " user_input # Check if the entered name is in the file if grep -q "$user_input" "$names_file"; then echo "Name '$user_input' is in the file." else echo "Name '$user_input' is not in the file." fi
Exercise 3: System Monitoring
#!/bin/bash
# Display current date and time
echo "Current date and time: $(date)"
# Show total number of CPU cores
cpu_cores=$(grep -c '^processor' /proc/cpuinfo) echo "Total number of CPU cores: $cpu_cores"