Saturday, June 3, 2023

my-alpine and docker-compose.yml

 ```

version: '1'

services:
    man:
      build: .
      image: my-alpine:latest
 

``` 


Dockerfile:

```

FROM alpine:latest
ENV PYTHONUNBUFFERED=1
RUN apk add --update --no-cache python3 && ln -sf python3 /usr/bin/python
RUN python3 -m ensurepip
RUN pip3 install --no-cache --upgrade pip setuptools
RUN apk add git
```

Tuesday, May 23, 2023

A Kotlin recursive function

 import kotlin.reflect.full.memberProperties


data class Abc(

    val a: String,

    val b: String,

    val c: String,

    val d: D,

    val e: E

) {

    data class D(

        val e: String,

        val f: String,

        val g: String

    )

    data class E(

        val h: List<String>

    )

}


data class Xyz(

    val a: String,

    val b: String,

    val c: String,

    val d: D,

    val e: E

) {

    data class D(

        val e: String,

        val f: String,

        val g: G

    ){

        data class G(

            val h: String,

            val i: String

        )

    }

    data class E(

        val h: List<String>

    )

}



fun hasContent(inVal: Any): Boolean {

    if (inVal::class.simpleName == "String") {

        if (inVal != "") {

            return true

        }

    } else {

        inVal::class.memberProperties.forEach { ele ->

            if (ele.returnType.classifier == String::class) {

                if (ele.call(inVal) != "") {

                    return true

                }

            }

            else if (ele.returnType.classifier == List::class) {

                val list = ele.call(inVal) as List<*>

                val listCheck = list.map { hasContent(it!!) }

                if (listCheck.contains(true)) {

                    return true

                }

            }

            else {

                if (hasContent(ele.call(inVal)!!)) {

                        return true

                    }

                }

        }

        return false

    }

    return false

}



fun main() {

    // aa has content, should print true

    val aa = Abc("a1", "b2", "c3", Abc.D("e4", "f5", "g6"), Abc.E(listOf("h7", "i8", "j9")))

    println(" aa has content -> ${hasContent(aa)}")


    // bb has no content, should print false

    val bb = Abc("", "", "", Abc.D("", "", ""), Abc.E(listOf("", "", "")))

    println(" bb has content -> ${hasContent(bb)}")


    // cc has content, should print true

    val cc = Abc("", "", "", Abc.D("e4", "", ""), Abc.E(listOf("", "", "j9")))

    println(" cc has content -> ${hasContent(cc)}")


    // dd has content, should print true

    val dd = Abc("", "", "", Abc.D("", "", ""), Abc.E(listOf("", "", "j9")))

    println(" dd has content -> ${hasContent(dd)}")


    // xx has no content, should print false.

    val xx = Xyz("", "", "", Xyz.D("", "", Xyz.D.G("", "")), Xyz.E(listOf("", "", "")))

    println(" xx has content -> ${hasContent(xx)}")


    // yy has content, should print true.

    val yy = Xyz("", "", "", Xyz.D("", "", Xyz.D.G("", "")), Xyz.E(listOf("", "", "blahblah")))

    println(" yy has content -> ${hasContent(yy)}")


    // zz has content, should print true.

    val zz = Xyz("", "", "", Xyz.D("", "", Xyz.D.G("", "blahblah")), Xyz.E(listOf("", "", "")))

    println(" zz has content -> ${hasContent(zz)}")

}


Wednesday, June 29, 2022

A nice alias to check the resources used be each user on a Linux server

 Sometimes on a shared Linux server or AWS EC2, we found out the resources are limited because some other persons are using the machine, and we want to rank order the users by resources used from high to low. This alias does exactly this.


```

alias wtf="ps hax -o rss,user | awk '{a[\$2]+=\$1;}END{for(i in a)print i\"  \"int(a[i]/1024+0.5);}' | sort -rnk2"

```

Friday, October 22, 2021

AWK: count which customer has the most unique accounts

Content of the count_uniq.awk script:


BEGIN {
    FS=",";
}
{
    if ($0 in seen == 0){arr[$2]++} else {seen[$0]=1}
    seen[$0]=1;
}
END {
    for (a in arr) print a, arr[a]
}


What it does?

count how many accounts does each customer has, ignoring duplicated rows. Details as in image below:


Customer "a" has 3 different accounts: [1, 2, 3]

Customer "b" has 1 account: [2]

Customer "c" has 2 accounts: [1, 2]


Thursday, September 30, 2021

Concatenate multiple files with same headers (and only keep one header line in the output file) - Version2

 Put this into an AWK script and call it merge.awk:


BEGIN{

    h=ARGV[1];

    ARGV[1]="";

}



{

    if(FNR==1){print}

    else if($1!~h){print}

}


and use an argument:

cat *.csv | awk -f merge.awk "<header>" > output.csv

Wednesday, September 8, 2021

git: find string in a certain branch

 So, today I cloned a repo and it has many branches. I know one of the branches has a file that is updated and contains the word "token". This is how I find out which branch it is:

git branch -a | cut -c3- | cut -d' ' -f 1 | xargs git grep "token" 

Thursday, August 12, 2021

cron.allow

If a user on Linux can't run "crontab -l" or "corntab -e", add the user name to /etc/cron.allow, and then it should work. 

my-alpine and docker-compose.yml

 ``` version: '1' services:     man:       build: .       image: my-alpine:latest   ```  Dockerfile: ``` FROM alpine:latest ENV PYTH...