Skip navigation

Tag Archives: bash


Putting this here too (I’m in habit for putting scripts into blog).


Usually before committing a file we need to check modified files against the code convention before committing to the code repository.

Here’s a single line to run modified files in Git against PHPCS.

git ls-files -m | grep '\.php$' | xargs ./vendor/bin/phpcs -s --standard=etc/phpcs/ruleset.xml

Code above assumes you install PHPCS using composer and have your own standard located in the etc/phpcs/ruleset.xml.

UPDATE

To check all uncommited files, run this

git ls-files --others --exclude-standard | grep '\.php$' | xargs ./vendor/bin/phpcs -s --standard=etc/phpcs/ruleset.xml

I just watched The Last: Naruto The Movie a couple of days ago and I recalled that it’s been almost a quarter of decade since I last read the mangascan.

Reading Naruto in Kindle

Reading Naruto in Kindle

So I got a complete 2GB of mangascan pictures from my friend from volume 1 until volume 72. I’ve been a fan of Kindle e-book reader and I want to read the mangascan in my Kindle device. Here’s how I converted the PNG and JPG files to PDF so it can be readable in Kindle.

Basically what I need is imagemagick library to convert the images to PDF. Here is how to use the imagemagick to do the conversion.

As long as the JPG/PNG files is named with proper file naming, doing a batch conversion in zipped files is as simple as this. It shouldn’t take too long to convert all of the images to PDF. In my case it took less than an hour. The quality of the PDF is not that bad, but I think we can configure the imagemagick to increase the quality. I assume it reduces the quality by a bit.

for zipfile in $(find . -type f -iname "*.zip" | sort -n); do
    echo "$zipfile"
    filename=$(basename "$zipfile")
    filename="${filename%.*}"
    echo "$filename"
    unzip $zipfile -d $filename
    echo "Converting...."
    find $filename | sort -n | tr '\n' ' ' | sed "s/$/\ ${filename}.pdf/" | xargs convert -verbose
done

See? Pretty simple, right? (The fact that it’s really simple is why I posted here. Might be useful for me in the future).


So I some times need to run a monthly cron job. And I want to set a parameter for duration spanned from first day and last day in the current month.

This is how I do it.

if [ -z “$STARTTIME” ] || [-z “$ENDTIME”]; then
STARTTIME=`date -d “-$(($(date +%d)-1)) days” +%Y-%m-%dT00:00:00Z`;
ENDTIME=`date -d “+1 month -$(date +%d) days” +%Y-%m-%dT23:59:59Z`;
fi

echo $STARTTIME
echo $ENDTIME


Jadi saya belakangan ini sedang membuat beberapa skrip bash yang mengambil parameter berupa path sebuah file. Perkaranya adalah bagaimana menangani string input untuk path yang absolute maupun relative. Ternyata cukup sederhana, yakni dengan readlink. Perintah ini akan mengambil absolute path dari input yang berupa relative path.

#!/bin/bash
# Check whether a string represents a directory or not
# string can be relative or absolute path

dir=$(readlink -f $1)
if [ -d $dir ]; then
        echo "$dir"
else
        echo "$dir is not a directory"
fi

Contohnya

petra@petra-laptop:~$ l ~/test
mailer1.txt  php54test.php   test/      test3/     test3.php~  test4.php   test5.php   test6.php
mailer2.txt  php54test.php~  test2.php  test3.php  test4/      test4.php~  test5.php~
petra@petra-laptop:~$ ./test.sh ~/test
/home/petra/test
petra@petra-laptop:~$ ./test.sh /home/petra/test/test3
/home/petra/test/test3
petra@petra-laptop:~$ ./test.sh /home/petra/test/test3.php
/home/petra/test/test3.php is not a directory
petra@petra-laptop:~$ ./test.sh ../petra/test/test4.php
/home/petra/test/test4.php is not a directory
petra@petra-laptop:~$ ./test.sh ../petra/test/test3
/home/petra/test/test3
%d blogger menyukai ini: