Showing posts with label Shell Scripts. Show all posts
Showing posts with label Shell Scripts. Show all posts

Jun 28, 2017

shell script to create a set of each 3 values within a range

I am trying to create a set of each 3 values within a range using Shell script.


Range of values     : 512 -885
Column difference: 12
Column increment: 3

    512 524 536
    515 527 539
    518 530 542
                 ......
    860 872 884
    863 875 513  --> Column 3 Reset
    866 878 516
    869 881 519
    872 884 522
    875 513 525  --> Column 2 Reset
    878 515 528
    881 518 531
    884 521 534
    513 524 537
    ....

Shell Script:

#!/bin/bash

start=512
end=885
incr=12
col1=$start
col2=$(($col1+$incr))
col3=$(($col2+$incr))
colincr=3
for z in {1..50}
do
    if [ $col1 -gt $end ]
    then
        col1=$(($(($col1 % $end))+$start-1))
    fi
    if [ $col2 -gt $end ]
    then
        col2=$(($(($col2 % $end))+$start-1))
    fi
    if [ $col3 -gt $end ]
    then
        col3=$(($(($col3 % $end))+$start-1))
    fi
    printf '%-4s%-4s%-4s\n' $col1 $col2 $col3
    col1=`expr $col1 + $colincr`
    col2=`expr $col2 + $colincr`
    col3=`expr $col3 + $colincr`
    
done





Aug 11, 2015

rsh Permission denied error fix?

RSH command execution is throwing permission denied error as shown below.


host1> /usr/bin/rsh -l user1 host2 "ls /user1"
Permission denied.

Reason: host2 machine user1 home directory missing .rhosts file

FIX: Connect host2 machine, goto user1 home directory and create .rhosts file.
Provide the file permission as 0600 to file .rhosts and try RSH command now.. :)



May 15, 2015

How to get the unused or free IPs on linux machine?

 Inline format of the script:
lsof -nl >/tmp/lsof.log;rm -rf ~/freeIPs.log; for ip in `ip add list|grep -v "127.0.0\|::1\|0.0.0.0\|00:00:"|cut -d" " -f6|cut -d"/" -f1|grep -v qdisc|awk 'NF'`;do grep $ip /tmp/lsof.log > /dev/null; if [ $? != 0 ]; then echo $ip >> ~/freeIPs.log; fi; done; rm -rf /tmp/lsof.log;echo "Total IPs:`ip add list|grep -v "127.0.0\|::1\|0.0.0.0\|00:00:"|cut -d" " -f6|cut -d"/" -f1|grep -v qdisc|awk 'NF'|wc -l`"; echo "Free IPs:`wc -l ~/freeIPs.log`";

Script:

 lsof -nl >/tmp/lsof.log
 rm -rf ~/freeIPs.log
 for ip in `ip add list|grep -v "127.0.0\|::1\|0.0.0.0\|00:00:"|cut -d" " -f6|cut -d"/" -f1|grep -v qdisc|awk 'NF'`
 do 
     grep $ip /tmp/lsof.log > /dev/null
     if [ $? != 0 ]
    then 
          echo $ip >> ~/freeIPs.log
    fi
 done
 rm -rf  /tmp/lsof.log
 echo "Total IPs:`ip add list|grep -v "127.0.0\|::1\|0.0.0.0\|00:00:"|cut -d" " -f6|cut -d"/" -f1|grep -v qdisc|awk 'NF'|wc -l`";
 echo "Free IPs:`wc   -l ~/freeIPs.log`";

Sep 5, 2014

Sample in-line unix "for" loop command eaxmples.

home>for i in {307,306,311} ; do echo $i; done
output:
307
306
311

home> for i in {307,306,311} {374..376} ; do echo $i; done
output:
307
306
311
374
375
376

May 5, 2014

How to setup password free SSH to hostname/localhost?


cd ~
echo "Removing the OLD .ssh dir"
rm -rf .ssh
ssh-keygen -t rsa
mkdir -p ~/.ssh
cat .ssh/id_rsa.pub >> .ssh/authorized_keys
chmod 700 .ssh; chmod 640 .ssh/authorized_keys
ssh-add
ssh `hostname`

Sep 18, 2013

How to write inline or single line shell script?

for i in {1..10}; do echo $i; done

for i in {1..10}; do echo $i:"$(ls |wc -l)"; done

Jul 18, 2013

How to change the `hostname` in the Linux?

>hostname
localhost

>cat /etc/HOSTNAME
localhost


Change the /etc/HOSTNAME file content and restart the machine.                                             

                                            

Jun 9, 2012

Uploading flat files into Postgres Database for specific tables


#!/bin/sh
if [ ! $# -eq 2 ] ; then
echo "Invalid arguments! Usage : DATTFilesToDB.sh  inputdir(fullpath) tablelistfilename";
echo "ex:DATFilesToDB.sh  usr/1L_joblogs joblogtables.txt";
exit;
else
echo "Executing database  uploading from DAT flies ..." ;
exec<$2
echo "----------- FLAT FILES (.dat) to DATABSE Tables------------------">>/usr/FFtoDB.log
echo "---Start Date:"$(date)>>/usr/FFtoDB.log
echo "-------------------------------------------------">>/usr/FFtoDB.log
echo "FlatFiles Loc:$1">>/usr/FFtoDB.log
cd $1
value=0;
while read line
 do
   value=`expr $value + 1`;
   echo "Currently running File/Table:$line">>/usr/FFtoDB.log
   query="COPY "$line" FROM '"$1"/public_"$line".dat' WITH DELIMITER ' ';"
   echo $query
   psql -h localhost -d webacc -U postgres -c "$query">> /usr/FFtoDB.log 2>&1
 done
echo "****$value file got created..";
echo "****$value file got created..">>/usr/FFtoDB.log
echo "---End Date:"$(date)>>/usr/FFtoDB.log
fi
echo "---------------------------------End of Script-----------------------";


.txt file should contain the table names.Script will upload flat files data into selected tables from the input dir. Errors and return values will be saved in  /usr/FFtoDB.log file.



Jun 7, 2012

sed command to identify date pattern yyyy-mm-dd hh:mm:ss.mse and replace with current date

sed -e "s/[0-9][0-9]\([0-9][0-9]\)-\([0-9][0-9]\)-\([0-9][0-9]\) \([0-9][0-9]\):\([0-9][0-9]\):\([0-9][0-9]\).\([0-9][0-9][0-9]\)/$(date +"%F %T.")$(($(date +%N)/1000000))/g" inputfile > newfile

Creating postgres database tables into Flat files (.copy) using shell script


#!/bin/sh
if [ ! $# -eq 2 ] ; then
echo "Invalid arguments! Usage : creatflatfiles.sh  outpurdir tablelistfilename";
echo "ex:creatflatfiles.sh  1L_joblogs tableslist.txt";
exit;
else
echo "Executing database  downloading to faltflies ..." ;
exec<$2
/bin/mkdir -p $1
cd $1
value=0;
while read line
 do
   value=`expr $value + 1`;  
   touch $line.copy;
   chmod 666 $line.copy;
   query="COPY "$line" TO '/usr/"$1"/"$line".copy';"
   echo $query
  #psql -h localhost -d databasename -U username -c command
   psql -h localhost -d webacc -U postgres -c "$query" >> /usr/FFtoDB.log 2>&1
 done
echo "****$value file got created..";
fi
#------------------------------------------

tableslist.txt should contain the table names.Script will generate tables data into flat files (tablename.copy) in the outputdir. Errors and return values will be saved in  /usr/FFtoDB.log file.