Thursday, May 31, 2018

Gracefully Exit Firefox using shell script and xdotool,Linux Teacher Sourav,Kolkata 09748184075

sudo apt-get install xdotool

if [[ -n `pidof firefox` ]];then
  WID=`xdotool search "Mozilla Firefox" | head -1`
  xdotool windowactivate --sync $WID
  xdotool key --clearmodifiers ctrl+q
fi



Source:http://how-to.wikia.com/wiki/How_to_gracefully_kill_(close)_programs_and_processes_via_command_line

Sending notifications alert to the User if battery status getting less than a threshold and shutdown the pc using shell script,Linux Teacher Sourav,Kolkata 09748184075

#!/bin/bash
#01/06/2018


while true
do
    battery_level=`acpi -b | acpi -b | cut -f4 -d ' '|tr -d ',%'`
    if [ $battery_level -le 5 ]; then
       notify-send "Battery is less than 5%!" "Charging: ${battery_level}%" " Please connect the charger ,the system will shutdown after 2 minute"
shutdown -P +2 "Shutdown script started"
  
    elif [ $battery_level -le 20 ]; then
       notify-send "Battery is lower than 20%!" "Please connect the charger: ${battery_level}%"
      

 

    fi

    sleep 10 # run this script in every 10 seconds
done


Source:https://askubuntu.com/questions/518928/how-to-write-a-script-to-listen-to-battery-status-and-alert-me-when-its-above?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

Saturday, May 26, 2018

Shell script to play Constant sound until user press a button when a new user logs on,Linux Teacher Sourav,Kolkata 09748184075

#!/bin/sh
#
# 27/05/2018
#
export BEEP=/usr/share/sounds/ubuntu/ringtones/Harmonics.ogg
alias beep='paplay $BEEP'
beepsound()
{
while [ 1 ]
do
beep
 read -t 0.25 -N 1 input
    if [[ $input = "q" ]] || [[ $input = "Q" ]]; then
# The following line is for the prompt to appear on a new line.
        echo
        exit

    fi
done
}

echo "New User Login Alert,Press Q to stop the beep"
numusers=`who | wc -l`
while [ true ]
do
currusers=`who | wc -l`
# get current number of users
if [ $currusers -gt $numusers ] ; then
echo "Someone new has logged on!!!!!!!!!!!"
date
who

beepsound
numusers=$currusers
elif [ $currusers -lt $numusers ] ; then
echo "Someone logged off."
date
numusers=$currusers
fi
sleep 1 # sleep 1 second
done



Friday, May 25, 2018

Do a task if your partition usage is greater than a given percentage using shell script,Linux Teacher Sourav,Kolkata 09748184075

a=$(df -h | grep -E 'root' | awk ' { print $5 " " $1}' | cut -f1 -d " " )
echo $a

b=${a::-1}
echo $b
if [ $b -ge 90 ] ; then
echo "hello"
else
echo "nope"
fi


in terminal you do it like this

df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | awk '{ print $1}' | cut -d '%' -f1 

Check a string if it is palindrome in shell script using rev command and without the use of rev command,Linux Teacher Sourav,Kolkata 09748184075

#/bin/sh
#25/05/2018

#in our class there is a very common task to see a string is palindrome or not

#for example aba is a string that will be same when altered from the opposite direction

#another example is 1991
#do you understand the assignment
#yes

#so let us ask the user to give us a string

echo "Please enter a string to be checked whether it is a palindrom"

read answer

echo "the string you entered is stored in the answer variable and it is $answer"

#there is an utility called rev by which we can do it just by a command

reverse=`echo $answer | rev`
#echo $reverse
#if [ $answer == $reverse ] ; then

#echo "the entered string is palindrome"

#else

#echo "the entered string is not palindrome"

#fi


#so it's working,however if we like we can do it without the use of rev command

#this backtick is used for evaluation ,like whatever inside the backtick will be evaluated

#and then be assigned to the left side

#wso to perform the same task without using rev

#we need to find the length of the string(for example the length of hello is 5

lenofword=$(echo -n $answer | wc -m)

echo the length of the word is $lenofword


#i am using shell command wc -m to get the character count,what could the problem
#for character we use -c, right?


#yes my mistake
#for some reason it's not working right now ,let me search a solution and will get back to you
#ok. leave it here. now can we work on the script i shared.


#yes sure

#i will come back to this later
#sometimes in shell script i forget the syntax,will check surely
reverse=""
for(( i=lenofword;i>=0;i--)) ; do
reverse+=${answer:i:1}



done

#echo $reverse

if [ $answer == $reverse ] ; then

echo "the entered string is palindrome"

else

echo "the entered string is not palindrome"

fi