Shell Scripting Flashcards

1
Q

All bash scripts must start with ______________

A

!/bin/bash (there is a hash tag in front of the ! symbol)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

In shell scripting, this #!/bin/bash is known as _____________

A

shebang

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Describe what the following crontab entry does: 0 5,17 * * * /scripts/script.sh

A

Runs script.sh at 5am and 5pm daily.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

A

It is a simple bash script that checks for current disk usage, and if disk % is over 80, sends an email to the admin.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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.

A

!/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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A

This script checks the system uptime, CPU usage, available memory, disk usage, and network connectivity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly