Thursday 7 July 2016

How to read a file line by line using Linux shell scrpit

This the simple and easy code to read a file using linux script.

Here in the code we read all the lines and display each line :)

file="/home/system5/Desktop/negativewords/neagtivewords" #readng neg words
while IFS= read -r line
do
        # display $line or do somthing with $line
    printf '%s\n' "$line"

done <"$file"

Wednesday 24 June 2015

Sending mail using terminal

This scribble (script ) is for sending mail alert to your mail or ad-min mail,when your system running out of disk space & shows few error problems you face in sending the mail after running the scribble(script).


SCRIPT :

#!/bin/bash

MAILTO=e-mail@gmail.com #e-mail id(give your mail id or admin mail)
ALERT=2
df -H | grep -vE 'abc:/xyz/pqr | tmpfs |cdrom|Used' | awk '{ print $5 " " $1 }' | while read output; do
user=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
echo $user
partition=$(echo $output | awk '{ print $2 }' )
if [ $user -ge $ALERT ]; then
  echo  "Alert: Almost out of disk space $user"
  df -H | mailx -s "Alert: out of disk space $user"  $MAILTO
echo "mailsent"
fi
done

screen-cast:


on Running the scribble you may get following error !!
if you get the same error then  install mail Utilities by
instructing following : sudo apt-get install mailutils.

 
you may get another error saying process exited with a non-zero status.



 Then instruct your computer to install the following :

install postfix using sudo apt-get : sudo apt-get install postfix
if error repeats again then give another power full instruction as insatll dpkg-reconfigure postfix don't forget to use sudo.

 after successful installation of power full instructions try to run the scribble  again.Then you will get the correct result with out any error as shown in following screen cast.
you will get a alert message when ever your system runs out of disk-space.

======================================================

if you did not received any mail in your inbox then do the following.,

I’ve used this method with small adjustment, and it works perfectly. 
As we already Installed mailutils ,When the setup wizard launches, choose the unconfigured option. You don’t need to do any special configuration to get this to work.


next Install and configure sstmp as follow:

~ sudo apt-get install ssmtp.

then enter into the directories of etc,ssmtp,and edit the file ssmpt.conf using vi or vim or nano i have done as follow.,

 ~ sudo  /etc/ssmtp
i entered into ssmtp directory now ,i open the file ssmtp.conf using nano.

 nano ssmtp.conf

then you enter into the file for doing the required adjustments as below

  Uncomment FromLineOverride=YES by deleting the #
   
 Add the following to the file:

     AuthUser=<user>@gmail.com   #add ad-min mail-id
    AuthPass=Your-Gmail-Password #password of the ad-min
    mailhub=smtp.gmail.com:587     #don't change it
    UseSTARTTLS=YES
    Save and close the file:
     
   ctrl-x
Press Enter 

screen cast:


If you’re using two-factor authentication do the follow:

Create a new application-specific password to use in the config file above. (If you’re using Gmail, you can manage those passwords here.)

After doing all required adjustments run the scribble,and check your mail or ad-min mail inbox.

 screen-cast shows the successful sending of mail alert with out any error!!!!