Sunday, September 20, 2020

FizzBuzz fastest

 Fastest FizzBuzz


I tried python, shell and awk script to check the fastest FizzBuzz program


# Program prints no. 1 to 100

# For multiples of 3: Fizz, 5: Buzz, both 3 & 5 : FizzBuzz


Shell Script

$ time ./fizzbuzz.sh > /dev/null

real    0m0.394s

user    0m0.249s

sys     0m0.146s


Python Script 

$ time ./fizzbuzz.py > /dev/null

real    0m0.072s

user    0m0.065s

sys     0m0.004s

 

AWK Script 

$ time awk -f  ./fizzbuzz.awk > /dev/null

real    0m0.007s

user    0m0.006s

sys     0m0.000s


Sharing fastest script in awk. 

$ cat fizzbuzz.awk

BEGIN {

                for (i=1; i<=100; i++) {

                        db3=i%3;db5=i%5;

                        if (db3==0 && db5==0) {

                                print "FizzBuzz"

                        } else if (db3==0) {

                                print "Fizz"

                        } else if (db5==0) {

                                print "Buzz"

                        } else print i}

        }


Wednesday, November 2, 2016

Quick Connect through Jump host via proxy

When we have hosts shared with ssh-keys, from home network to office network via proxy, then jump host.

This simple ssh/config saves lots of manual work


Host jumphost
        HostName jumphost.my.home.net
        HostKeyAlgorithms ssh-dss
        ForwardAgent yes
        LocalForward 3128 proxyhost.my.home.net:3128
        User sanjeev
        StrictHostKeyChecking no
        ConnectTimeout 30
        KeepAlive no

Host *.my.office.net
        HostName %h
        User sanjeev
        ForwardAgent yes
        ProxyCommand ssh jumphost nc -w 120 %h %p
        StrictHostKeyChecking no
        ConnectTimeout 30
        KeepAlive no

Now from your laptop you can do seamless connection, for any ssh activities


 ssh host1.my.office.net "cat watch.status"


This goes through  proxyhost to connect to office network, login to jumphost and connect to office network host, cat the file watch.status from host1, in office network :)


Friday, October 21, 2016

process start time

For long I had been looking out for process starttime and elapsed time, its important for long stuck process

Refer: http://linuxcommand.org/man_pages/ps1.html

so here I got it..





$ ps wwf -o pid,fuser,lstart,etime,pcpu,pmem,args  -p 2128 

PID  FUSER   STARTED                   ELAPSED       %CPU  %MEM   COMMAND
2128 root    Wed Oct 19 22:00:31 2016  1-06:28:14    0.0   0.2    /sbin/dhclient -1 -v -pf /run/dhclient.enp0s8.pid -lf /var/lib/dhcp/dhclient.enp0s8.leases -I –df /var/lib/dhcp/dhclient6.enp0s8.leasesenp0s8                                                                                                                                        
$ date
Fri Oct 21 04:31:18 UTC 2016

Monday, June 13, 2016

Quickly deleting large file

Deleting larger file takes time, but when we dont want the file, why shout be their waiting time to delete it.

These files i am deleting

-rw-r--r--  1 sanjeev.tripurari  1114871328  24931090000 Jun 13 17:28 words3.txt
-rw-r--r--  1 sanjeev.tripurari  1114871328  24931090000 Jun 13 17:28 words2.txt

Normal deletion

$ time rm -fv words2.txt 
words2.txt

real 0m0.672s
user 0m0.001s
sys 0m0.080s


copy /dev/null will make it zerobyte quickly and then delete 

$ time cp -v /dev/null words3.txt 
/dev/null -> words3.txt

real 0m0.321s
user 0m0.001s
sys 0m0.294s


$ time rm -fv words3.txt 
words3.txt

real 0m0.006s
user 0m0.001s
sys 0m0.003s



Sunday, June 12, 2016

quick share code snippet

We often need to share the code snippet or some text file for review or comments, attaching to chat window or mail take time.

we can run netcat and expose the code on the web and do the quick correction,

$ cat poweroff.sh |nc   -l   0.0.0.0 8088

yes though it just stays for one instance, it saves time of getting on to another application,

the remote user can now use curl/links

function retun value

Have been using bash for long, and never expected to have return value, as the function was written just do the repetative job

But when we need a bash function to return value, be sure that it just has one echo which is for return, any other display will be returned otherwise

here how it is..



$ cat poweroff.sh 


#!/bin/bash

##
## Program to test return value from bash script..
##

function power(){

#
# power(3,2)=9
# power(5,3)=125
#
local fv=$1
local sv=$2

local p=1


for i in ` seq 1 $sv ` ; do
p=` expr $p \* $fv `
done

echo $p
}

v1=${1:-3}
v2=${2:-2}

p1=$(power $v1 $v2)

echo "power($v1,$v2)=$p1"

# Done.




$ ./poweroff.sh 

power(3,2)=9


Tuesday, May 17, 2016

Directory listing with for loop

Often had to check the files between directories if name matches or copy the missing ones, in cases of taking backup.

Most of the sites come up with the while loop, but i found us `for` loop
$ ls -l
total 1392
-rw-------@ 1 sanjeev  users  277935 May 17 18:21 Jenkins_Job_packaging.pdf
-rw-------@ 1 sanjeev  users  227774 May 17 18:21 Pipeline Based Alert and Notification.docx
-rw-r--r--@ 1 sanjeev  users    1324 May 17 18:21 SieveOfAtkin.js
-rw-r--r--  1 sanjeev  users    1301 May 17 18:21 VPN (Cisco IPSec).networkConnect
-rw-------@ 1 sanjeev  users  187319 May 17 18:21 algo.docx
-rw-r--r--  1 sanjeev  users    3797 May 17 18:21 bill.feb

-rw-r--r--  1 sanjeev  users      65 May 17 18:22 i

With for loop it was bad output
j=0
for i in `ls`; do
let j=j+1
echo "$j: $i"
done 

$ bash i

1: Jenkins_Job_packaging.pdf
2: Pipeline
3: Based
4: Alert
5: and
6: Notification.docx
7: SieveOfAtkin.js
8: VPN
9: (Cisco
10: IPSec).networkConnect
11: algo.docx
12: bill.feb

13: i

The spaces in filename is used as separators

$IFS
internal field separator
This variable determines how Bash recognizes fields, or word boundaries, when it interprets character strings.

Improved for loop, using IFS
IFS=$'\n'
j=0
for i in `ls`; do
let j=j+1
echo "$j: $i"

done 


And finally we get the correct filenames

$ bash i

1: Jenkins_Job_packaging.pdf
2: Pipeline Based Alert and Notification.docx
3: SieveOfAtkin.js
4: VPN (Cisco IPSec).networkConnect
5: algo.docx
6: bill.feb
7: i



Well IFS is well documented, but not popularly used .