Devlog 0 : Bashing assignments

Assignments are not the most enjoyable thing to do, especially when it’s some mindless arbitrary task.

Let’s use some bash scripting to make our life easier

The task ahead #

It’s OS class, the teacher gives you an assignment even tho it’s the first day of the semester, not a fun start.

Looking in google classroom I see this,

First experiment of OS Lab cycle (Refer syllabus) is Basic Linux commands.

We’re supposed to type in basic commands and take screenshots of the output and then compile them into a report with each command having a title, description, example usage and screenshot.

xdotool #

xdotool is the holy grail of automating stuff, it has pretty much anything someone would want to

[rosh@localhost ~]$ xdotool
Usage: xdotool <cmd> <args>
Available commands:
  getactivewindow
  getwindowfocus
  getwindowname
  getwindowpid
  getwindowgeometry
  getdisplaygeometry
  search
  selectwindow
  help
  version
  behave
  behave_screen_edge
  click
  getmouselocation
  key
  keydown
  keyup
  mousedown
  mousemove
  mousemove_relative
  mouseup
  set_window
  type
  windowactivate
  windowfocus
  windowkill
  windowmap
  windowminimize
  windowmove
  windowraise
  windowreparent
  windowsize
  windowunmap
  set_num_desktops
  get_num_desktops
  set_desktop
  get_desktop
  set_desktop_for_window
  get_desktop_for_window
  get_desktop_viewport
  set_desktop_viewport
  exec
  sleep

This is the final script that I came up with, automation is kinda neat huh :wink:

#!/bin/env bash

SLEEP_TIME=2
count=0

take_ss(){
    sleep $SLEEP_TIME
    if [[ -z "$1" ]]; then
        ((count++))
        maim -i $(xdotool getactivewindow) "$2/$count.jpg"
        echo "taking screenshot..."
        xdotool key ctrl+l
    else
        xdotool type "$1"
        xdotool key KP_Enter
        sleep $SLEEP_TIME
    fi;
}

dir_resolution(){
    if [ -d "$1" ];then
        echo -n "do you want to overwrite the directory $2[y/N] : " 
        read n 
        if [ "$n" == "y" ];then
            rm -r "$1"
            mkdir "$1"
        else 
            exit 1
        fi
    else
        mkdir -p "$1"
    fi

}

main(){

    dir_resolution "$2"

    alacritty -e 'exec bash'
    while read -r line; do 
        echo "$line"
        take_ss "$line" "$2"
    done < "$1" 

    xdotool type "# press enter to kill this window"
    xdotool key KP_Enter
    xdotool type exit
    #xdotool windowkill $(xdotool getactivewindow)
}

[ $# -eq 0 ] && usage && exit
[ $# -eq 1 ] && usage && exit
[ $# -eq 2 ] && main "$@" && exit
[ $# -eq 3 ] && SLEEP_TIME="$3" && main "$@" && exit