Script
Script
#!/bin/bash
backup_dir="$HOME/backup"
mkdir -p "$backup_dir"
# Step 2: Get the current date and time in the format YYYY-MM-DD_HH-MM-SS
current_datetime=$(date +"%Y-%m-%d_%H-%M-%S")
# Append the current date and time to the filename and copy to the backup directory
cp "$file" "$backup_dir/${filename}_$current_datetime.txt"
fi
done
Report if the CPU usage is above 80% or if the available memory is below 20%
#!/bin/bash
log_file="system_health.log"
cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}')
else
fi
else
fi
Generates a random password for each user and saves the username and password
to a file named credentials.txt
#!/bin/bash
user_list="user_list.txt"
credentials_file="credentials.txt"
generate_password() {
> "$credentials_file"
else
# Create a new user without a home directory (-M) and without a password (-s
/usr/sbin/nologin)
password=$(generate_password)
fi
Saves the compressed file with a name that includes the current date (e.g ,
backup_2023-08-20.tar.gz)
#!/bin/bash
read -p "Enter the directory path you want to back up: " directory_path
exit 1
fi
current_date=$(date +"%Y-%m-%d")
# Get the name of the directory (basename)
directory_name=$(basename "$directory_path")
backup_file="backup_${directory_name}_$current_date.tar.gz"
Allows the user to add tasks , view tasks and remove tasks.
#!/bin/bash
todo_file="todo.txt"
add_task() {
else
fi
view_tasks() {
else
cat -n "$todo_file"
fi
remove_task() {
return
fi
view_tasks
else
fi
}
# Main menu loop
while true; do
echo
case $option in
1) add_task ;;
2) view_tasks ;;
3) remove_task ;;
esac
done
Installs each package using the appropriate package manager (apt, yum, etc).
#!/bin/bash
package_file="packages.txt"
# Determine the package manager (apt for Debian-based, yum for RedHat-based)
package_manager="apt"
package_manager="yum"
else
echo "No supported package manager found (apt or yum)." >> "$log_file"
exit 1
fi
else
fi
fi
done < "$package_file"
Count and display the number of lines, words and charecters in the file.
#!/bin/bash
exit 1
fi
exit 1
fi
file="$1"
longest_word=$(tr -s '[:space:][:punct:]' '\n' < "$file" | awk '{ if (length > max_length) { max_length = length;
longest_word = $0 }} END { print longest_word }')
else
fi