Friday, May 18, 2018

Shell script to watch a process and inform when it ends ,Linux Teacher Sourav,Kolkata 09748184075

#!/bin/sh
#
# 18/5/2018
#
echo "this shell script will watch when a process ends"
if [ $# -ne 1 ] ; then
echo "Usage: processwatcher process-directory"
echo " For example: processwatcher /proc/14856 where 14856 is the PID of the  target process"
exit 255
fi
FN=$1
# process directory i.e. /proc/14856
rc=1
while [ $rc -eq 1 ]
do
if [ ! -d $FN ] ; then
# if directory is not there
echo "Process $FN is not running or has been terminated."
let rc=0
else
sleep 1
fi
done
echo "End of processwatcher script"
exit 0

I have given the name of this script as processwatcher.sh,suppose another job or process is going on simultaneously and i want to know when it ends ,my script will serve that purpose when we run it with the Process ID of the target process or job as an argument to this shell script

For example we created this shell script to show the usage of break and continue in shell scripting ,it will run for a certain amount of time

#!/bin/sh
#
# 18/5/2018
#
echo "Break Continue Example"
FN1=/tmp/break.txt
FN2=/tmp/continue.txt
x=1
while [ $x -le 1000000 ]
do
echo "x:$x"
if [ -f $FN1 ] ; then
echo "Running the break command"
rm -f $FN1
break
fi
if [ -f $FN2 ] ; then
echo "Running the continue command"
rm -f $FN2
continue
fi
let x++
sleep 2
done
echo "x:$x"
echo "End"
exit 0


we first run it from the terminal

sh ./breakandcontunue.sh


Now to see what process ID it has while running we need to use the grep command


ps auxw | grep break* | grep -v grep

the last piped section was to filter the grep search which itself acts as a process

the output is like this

sourav   14856  0.0  0.0  12876  2996 pts/1    S+   20:05   0:00 sh ./breakandcontunue.sh

So the PID as we can see is 14856


so in the /proc/folder we should find a folder named 14856 and it will continue to exist while this script/process/job runs

our shell script processwatcher is basically checking the existence of this folder and thus if the process is running with an interval of 1 second with the help of sleep command



 
      

Usage of break and continue statement in a shell script,Linux Teacher Sourav,Kolkata 09748184075

#!/bin/sh
#
# 18/5/2018
#
echo "Break Continue Example"
FN1=/tmp/break.txt
FN2=/tmp/continue.txt
x=1
while [ $x -le 1000000 ]
do
echo "x:$x"
if [ -f $FN1 ] ; then
echo "Running the break command"
rm -f $FN1
break
fi
if [ -f $FN2 ] ; then
echo "Running the continue command"
rm -f $FN2
continue
fi
let x++
sleep 1
done
echo "x:$x"
echo "End"
exit 0

You can test the script after creating the necessary files such as continue.txt and break.txt in the /tmp folder


touch /tmp/continue.txt

and the script will continue till x gets 1000000

to stop this use the tab feature in terminal and in a different tab use
touch /tmp/break.txt

The shell script will stop using the break statement

Using Let command in shell script error command not found solved,Linux Teacher Sourav,Kolkata 09748184075

If anyone is trying to learn shell script in ubuntu (in my case ubuntu 16.04), he or she might found out that bash built in commands such as let giving you command not found error ,to solve this you need to understand even if you put the line #!/bin/sh at the top of your script /bin/sh on ubuntu is dash not bash

So to solve this you need to reconfigure dash to not be the default shell

The command to accomplish this is

sudo dpkg-reconfigure dash


press no when asked for whether you like dash to be the default

and then everything should work fine


Source:https://ubuntuforums.org/showthread.php?t=1377218

Thursday, May 10, 2018

Send a message in your phone using VBA and TextLocal API.VBA Teacher Sourav,Kolkata 09748184075

Private Sub Command1_Click()
  
        Dim username As String
        Dim password As String
        Dim result As String
        Dim myURL As String
        Dim Sender As String
        Dim numbers As String
        Dim Message As String
        Dim postData As String
        Dim winHttpReq As Object
        apikey = Text1.Text
        Sender = Text2.Text
        numbers = Text3.Text
        Message = Text4.Text
    
        Set winHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
    
        myURL = "https://api.textlocal.in/send/?"
        postData = "apikey=" + apikey + "&message=" + Message + "&numbers=" + numbers + "&sender=" + Sender
    
        winHttpReq.Open "POST", myURL, False
        winHttpReq.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        winHttpReq.Send (postData)
    
        sendsms = winHttpReq.responseText
        MsgBox (sendsms)
       
    End Sub
   

**For promotional account which is mine the sender should be blank

**Due to TRI NCCP regulation after 9 pm and before 9 am you can not send message (error 192)
   


I followed this tutorial

https://www.youtube.com/watch?v=fc5ZNMXb4Fo

Tuesday, May 8, 2018

WI-FI getting disconnected/Networks not showing up on Ubuntu 16.04 solved ,Linux Teacher Sourav,Kolkata 09748184075


Install rfkill

sudo apt-get install rfkill
then run this command

rfkill unblock all
check if the wifi is working. If not do this

sudo nano /var/lib/NetworkManager/NetworkManager.state
then you will see some settings. Set everything to "true" reboot your system

give this command in terminal

rfkill list
you'll see that some are softblocked and hardblocked. All of them should be "no".

If it's not "no" then you need to somehow turn them to "no". I did this and it worked for me

sudo modprobe -r acer-wmi
cd /etc/modprobe.d
sudo nano blacklist.conf
Then add blacklist acer-wmi as a new line at the end of the file.

then save the file by pressing Ctrl+O ,close it and reboot the system. It should work

Source:https://askubuntu.com/questions/769521/wifi-networks-are-not-showing-in-ubuntu-16-04?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa