Shell Scripting — Exam Drill Pack Unit u06 · 8 real exercises (6 write-a-script + 2 trace) from the 2021–2025 exams · scripts taken from the official solutions

Tip: write your own script first, then compare with the solution.
#!/bin/bash
if [ $# -lt 1 ]; then
   echo "USAGE: $0 <file_list>"
   exit 1
fi
for file in $* ; do
    stats=$(ls -l $file)
    type=$(echo $stats | cut -c1)
    if [ "$type" = "-" ]; then
        dim=$(echo $stats | cut -d" " -f5)
        read=$(echo $stats | cut -c2)
        write=$(echo $stats | cut -c3)
        echo -n "$file $dim read_perm "
        if [ $read = "r" ]; then echo -n "yes write_perm "
        else echo -n "no write_perm x"; fi
        if [ $write = "w" ]; then echo "yes"
        else echo "no"; fi
    fi
    if [ "$type" = "l" ]; then
        user=$(echo $stats | cut -d" " -f3)
        pointed=$(echo $stats | cut -d" " -f11)
        echo "$user $pointed"
    fi
done
exit 0
#!/bin/bash
if [ -z "$1" ]; then
  echo "Usage: $0 <directory path>"; exit 1
fi
directory="$1"
if [ ! -d "${directory}" ]; then
  echo "The provided path is not a directory."; exit 1
fi
# small < 1 MByte (bytes), large > 1 GByte
small_files=$(find "${directory}" -type f -name "*.txt" -size -1048576c)
large_files=$(find "${directory}" -type f -name "*.txt" -size +1G)

small_files_index=1
for file in ${small_files}; do
    cat "${file}" >> "./small_files_${small_files_index}.txt"
    ((small_files_index++))
done

for file in ${large_files}; do
    large_files_index=1
    file_size=$(cat "${file}" | wc -c)
    large_base_name=$(basename "${file}" ".txt")
    chunk_size=$((100 * 1024 * 1024))
    head_size=$((chunk_size * large_files_index))
    while [ $head_size -le $file_size ]; do
        cat "${file}" | head -c "${head_size}" | tail -c "${chunk_size}" > "./${large_base_name}_${large_files_index}.txt"
        ((large_files_index++))
        head_size=$((chunk_size * large_files_index))
    done
    remainder=$((file_size % chunk_size))
    if [ $remainder -ne 0 ]; then
        cat "${file}" | tail -c "${remainder}" > "./${large_base_name}_${large_files_index}.txt"
    fi
done
#!/bin/bash
if [ $# -ne 1 ]; then
  echo "usage $0 file.txt"; exit 1
fi
newfilename=$(basename $1 ".txt")
newfilename=$newfilename".xyx"
cat $1 > $newfilename            # make the copy
nlines=$(cat $1 | wc -l)
rm -f tmp1.txt
while read line
do
  nwords=$(echo $line | wc -w)
  echo $nwords $nlines $line >> tmp1.txt
done < $1
cat tmp1.txt | sort -k 1 -n > $1   # sort by word count
rm tmp1.txt
#!/bin/bash
if [[ $# -lt 1 ]]; then
    echo "Missing file name"; exit 1
fi
cnt=0; days=(); first=""; last=""
declare -A stats
while read -r line; do
    if [ $cnt -eq 0 ]; then
        month=$(echo $line | cut -d" " -f1)
        year=$(echo $line | cut -d" " -f2)
    elif [ $cnt -eq 1 ]; then
        for day in ${line}; do
            days+=($day); stats[$day]=0
        done
    else
        i=0
        for day in ${line}; do
            if [[ $day != "XX" ]]; then
                if [[ $first == "" ]]; then first=${days[$i]}; fi
                last=${days[$i]}
                ((stats[${days[$i]}]++))
            fi
            ((i++))
        done
    fi
    ((cnt++))
done < $1
echo "First day is $first"
echo "Last day is $last"
for day in ${!stats[@]}; do
    echo "$day appears ${stats[$day]} times"
done
#!/bin/bash
if [[ $# -lt 4 ]]; then
    echo "usage $0 elf_path out_path START LEN"; exit 1
fi
elf=$1; out=$2; str=$3; len=$4
if [[ ! -f $elf ]]; then echo "file not found!"; exit 2; fi
let end=$str+$len
readelf -s $elf | tr -s " " | tail -n +4 | while read line; do
    addr=$(echo $line | cut -d " " -f 2)
    size=$(echo $line | cut -d " " -f 3)
    let end_addr=$addr+$size
    if [[ $addr -ge $str ]]; then
        if [[ $end_addr -le $end ]]; then
            name=$(echo $line | cut -d " " -f 8)
            echo "$addr, $size, $name"
        fi
    fi
done > $out
if [[ $(cat $out | wc -l) -gt 0 ]]; then
    sort -o $out -k 2 $out
else
    echo "No symbol found in range [$str, $end]" 1>&2
fi
#!/bin/bash
if [ $# -lt 2 ]; then
  echo "Usage: $0 <start|stop> <list>"; exit 1
fi
if [ ! -f $2 ]; then
    echo "The second parameter must be a valid file"; exit 1
fi
if [ $1 == "start" ]; then
    while read cmd; do
        "$cmd" &
    done < $2
elif [ $1 == "stop" ]; then
    while read cmd; do
        pid=$(ps -aux | grep -e "$cmd" | grep -v grep | tr -s " " | cut -d " " -f 2)
        kill -9 $pid
        echo "Terminated process $pid running $cmd"
    done < $2
else
    echo "First parameter must be 'start' or 'stop'"; exit 1
fi
echo -n $#
for x in $* ; do echo -n $x ; done
echo $#
shift
echo $*
exit 0