Shell Scripting Flashcards
All bash scripts must start with ______________
!/bin/bash (there is a hash tag in front of the ! symbol)
In shell scripting, this #!/bin/bash is known as _____________
shebang
Describe what the following crontab entry does: 0 5,17 * * * /scripts/script.sh
Runs script.sh at 5am and 5pm daily.
Assess what the following bash script does:#!/bin/bash
disk_usage=$(df -h / | awk ‘NR==2{print $5}’ | cut -d’%’ -f1)
if [ $disk_usage -gt 80 ]; then
echo “Warning: Disk usage is over 80%!” | mail -s “Disk usage alert” admin@example.com
fi
It is a simple bash script that checks for current disk usage, and if disk % is over 80, sends an email to the admin.
Provide a bash script that batch adds new users from a text file as input. The users should be given a default passwords. The default password for each user are the first three letters of their name followed by _321. The batch script should also display instructions on what the script does.
!/bin/bash
display instructions
echo “This script will batch add new users from a text file.”
echo “Each user will be given a default password based on their name.”
read input file
while read line
do
# extract username
username=$(echo $line | cut -d ‘,’ -f 1)
# extract first three letters of name
password=$(echo $username | cut -c 1-3)
# set default password
default_password=”$password_321”
# create user with default password
sudo useradd -m -p $(openssl passwd -1 $default_password) $username
# display message
echo “User $username has been created with the default password: $default_password”
done < input.txt
What does the following bash script do:#!/bin/bash
uptime
top -bn1 | grep load | awk ‘{printf “CPU Load: %.2f\n”, $(NF-2)}’
free -m | awk ‘NR==2{printf “Memory Usage: %s/%sMB (%.2f%%)\n”, $3,$2,$3*100/$2 }’
df -h | awk ‘$NF==”/”{printf “Disk Usage: %d/%dGB (%s)\n”, $3,$2,$5}’
ping -q -c5 google.com > /dev/null
if [ $? -eq 0 ]
then
echo “Internet Connectivity: Connected”
else
echo “Internet Connectivity: Disconnected”
fi
This script checks the system uptime, CPU usage, available memory, disk usage, and network connectivity.