dimanche 7 juin 2020

Perform if selection statement on a variable which can cycle through values?

I wrote the following script to help me study.

The program closes distracting apps if found running while the script is being executed.

I want some way to expand the scope of monitoring for programs i.e close more programs while the script is running. For example, if nautilus, neverball, transmission etc are running too, they should be met with the same fate i.e pkill.

I don't want to write more lines i.e make more if..then statements. I want some way for this same code to cycle through the variable $program for all these values each iteration. Something like an array ? I'm not entirely sure of the bash way to do it so if you can think of a better way, tell me.

#!/bin/bash

bold=$(tput bold)
normal=$(tput sgr0)
echo "Slotmachine has begun. Do not kill this terminal or you\'ll mess up directory permissions." 
echo ...
echo "Type the ending time for slot ${bold}( CAUTION! Time format must be: 05:03 or 11:34 ):- ${normal}"
read endtime

cd /home/user1

for folder in *;
    do 
        if [[   "$folder" != "Documents" ]] #documents has my physics notes.
        then
            chmod u-r $folder 
            echo Permissions taken for $folder.
        fi
    done


sleep 3;



declare -i counter=1 , killtimer=5;

program="firefox"

until [[ $( date +%H:%M ) == $endtime ]]
    do 
        date

        echo "Study slot till $endtime"
        echo "..."
        echo "${bold}Focus".
        counter=$((counter+1))
        sleep 1
        clear
        if pgrep -x $program > /dev/null
        then
            for killtimer in {5..1}
                do
                    echo "${bold}Browsing isn't allowed. If you need to browse, wait till the end. Do not kill this terminal or you'll mess up permissions."
                    echo "${bold} $program will be killed in.. $killtimer seconds."
                    sleep 1
                    clear
                    killtimer=$((killtimer-1))
                done
            pkill $program
        fi

    done

for folder in *;
    do 
        if [[   "$folder" != "Documents" ]]
        then
            chmod u+r $folder 
            echo "${normal}Permissions given for $folder".
        fi
    done

sleep 2
echo ...
echo "${bold} Slotmachine session has ended. ${normal}"```

Aucun commentaire:

Enregistrer un commentaire