Pokazywanie postów oznaczonych etykietą bash. Pokaż wszystkie posty
Pokazywanie postów oznaczonych etykietą bash. Pokaż wszystkie posty

poniedziałek, 19 stycznia 2015

Cron i ostatni dzień miesiąca

Wprawdzie nie wiadomo który dzień jest ostatnim w miesiącu, ale na pewno po nim będzie dzień z numerem 1. Numer dnia, który będzie jutro można zaś ustalić wykorzystując date:


$ date +%d -d tomorrow
$ 19

W dniach 28--31, o godzinie 23:55 sprawdź czy następny dzień ma numer jeden. Jeżeli tak, wykonaj SKRYPT.sh:


55 23 28-31 * * [ "$(date +%d -d tomorrow)" = "01" ] && SKRYPT.sh

wtorek, 12 lutego 2013

Enabling bash beep command

To enable bash beep command (Fedora Core 15) one have to execute:


yum install beep

Then as root:


chmod 4755 /usr/bin/beep
# remove snd-pcsp module from blacklisted ones
vi /etc/modprobe.d/blacklist.conf

insert # before blacklist snd-pcsp.

Now Emacs beeps on errors as well. To disable this feature one has to add to .emacs


(setq visible-bell t)

The error is signaled by a screen flash now (see also [Emacs] AlarmBell).

poniedziałek, 21 stycznia 2013

Refined bash script to periodically run fswebcam

The script: 1) suspend capturing images at night for obvious reason and 2) writes images to files named as: image_<odd-or-even-week><day-of-week>_<hour><minute>.jpg

Thus only images from the last two weeks are stored and the older ones are deleted ``automatically''.


#!/bin/bash
OUTPUT_DIR=/var/www/cam/fswebcam-output
# Max resolution for C270 webcam is 1280x720
RESOLUTION="1280x720"
QUALITY="85"

MINUTE=`date +%M`
HOUR=`date +%H`
MONTH=`date +%m`
DAY_OF_WEEK=`date +%u`
WEEK=`date +%U`
FILE_OUT=`date +%d%H%M`
TODAY=`date +%Y/%m/%d`

## Suspend capturing pictures at night
DAY_START=`sun_calc.py -date "$TODAY" -city Sopot -param dawn`
DAY_STOP=`sun_calc.py -date "$TODAY" -city Sopot -param dusk`

if [ "$DAY_START" = "False" -o "$DAY_STOP" = "True" ] ; then
NIGHT="YES"; exit;
fi

fswebcam -r $RESOLUTION -S 11 --jpeg $QUALITY \
--title "Sopot/Abrahama Street (PL)" \
--subtitle "View from my window" \
--info "Logitech_Webcam_C270@raspberryPi ($RESOLUTION)" \
--save $OUTPUT_DIR/image.jpg -q

## Rotate photos every two weeks
## Week number modulo 2 (0 or 1)
WEEK_NO=$(($WEEK % 2))
FILE_OUT="${WEEK_NO}${DAY_OF_WEEK}_$HOUR$MINUTE"

## Wundergound expects the file is `image.jpg'.
## To preserve from overwriting rename:
cd $OUTPUT_DIR && cp image.jpg image_$FILE_OUT.jpg

./sun_calc.py is a Python script (I am not Python programmer BTW):


#!/usr/bin/python
import datetime
import argparse
import pytz
from astral import Astral
city_name = 'Sopot'

parser = argparse.ArgumentParser()
parser.add_argument("-date", type=str, help="Date as yyyy/mm/dd")
parser.add_argument("-city", type=str, help="City name")
parser.add_argument("-param", type=str, help="dusk sunrise sunset or dawn")
parser.add_argument("-verbose", help="Icrease verbosity", action="store_true")
args = parser.parse_args()

dat = args.date
city_name = args.city
param = args.param
verb_level = args.verbose

[year, month, day] = dat.split("/");

year = int(year)
month = int(month)
day = int(day)

a = Astral()
city = a[city_name]

a.solar_depression = 6 ## sd can be: 6, 12, 18

timezone = city.timezone

sun = city.sun(date=datetime.date(year, month, day), local=False)
sun_local = city.sun(date=datetime.date(year, month, day), local=True)

time_now = datetime.datetime.utcnow().replace(tzinfo = pytz.utc)

if verb_level > 0:
print('solar_depressions: ' + str(a.solar_depression))

print city_name + " " + str(time_now) + " " + str(sun[param]) \
+ " (" + str(sun_local[param]) + ")"

print time_now > sun[param]

The script based on astral package can compute dusk/dawn time (among other things) and compares it to the current time. If current time is past dusk/dawn (specified as a command line parameter) ./sun_calc.py returns True. Otherwise it returns False.

The astral package does not contains Sopot but it is easy to add it just by editing astral.py (before installation of course):


Sopot,Poland,54°44'N,18°55'E,Europe/Warsaw

A short test demonstrating that for Sopot and Warsow the script returns significantly different results:


./sun_calc.py -date 2013/01/21 -city Sopot -param dawn -verbose
solar_depressions: 6.0
Sopot 2013-01-21 09:17:11.368979+00:00 2013-01-21 06:09:47+00:00 (2013-01-21 07:09:47+01:00)
./sun_calc.py -date 2013/01/21 -city Warsaw -param dawn -verbose
solar_depressions: 6.0
Warsaw 2013-01-21 09:17:46.172157+00:00 2013-01-21 05:53:22+00:00 (2013-01-21 06:53:22+01:00)
True

So there is circa 15 minutes difference between Sopot which is some 300 km to the North from Warsaw.

wtorek, 7 lutego 2012

Using ffmpeg for cutting media files

As simple as:


ffmpeg -ss start-time -t duration -i file-in file-out

Usually start-time/stop-time are given so duration have to be computed. Nothing difficult but the following bash script calculates duration for us:


#!/bin/bash
# cnt_sec function converts string `hh:mm:ss' to seconds, both
# `hh' and `mm' are optional
function cnt_sec () {
echo $1 | awk -F":" '{ if (NF>2) { sec += 60*60 * $(NF-2) } ;
if (NF>1) { sec += 60 * $(NF-1) } ;
if (NF>0) { sec += $NF } ;
print sec }'
}

STRT_TIME=`cnt_sec $1`
STOP_TIME=`cnt_sec $2`

DURATION=$((STOP_TIME-STRT_TIME))
echo $DURATION
# cut from file $3 from $1 to $2 write to $4
# both $1 and $2 are time durations in `hh:mm:ss' format
ffmpeg -ss $1 -t $DURATION -i $3 $4

poniedziałek, 23 stycznia 2012

Bidirectional synchronisation with rsync

I would like to copy Elka's home directory (/home/eros/) from the laptop (asteroid) to the server (jupiter) and back from jupiter to asteroid as well as from/to jupiter to/from PC machine (darkstar). So the problem is to sync bidirectionally /home/eros/ on three machines.

On darkstar I run the following script (and similar one on asteroid):


#!/bin/bash
# http://wiki.archlinux.org/index.php/Full_System_Backup_with_rsync
#
SOURCEDIR=/home/eros/
DESTDIR=root@jupiter:/public/DELL/backup/rootfs/home/eros

INCLUDED=' --include="/.emacs-local/" --include="/.ssh/" --include="/.emacs" --include="/.bash_profile" --include="/.bashrc" '
EXCLUDED=' --exclude="/.*"'

rsync -av -e ssh $INCLUDED $EXCLUDED ${SOURCEDIR} ${DESTDIR}

# the other way round now
# Cf. http://faculty.wiu.edu/CB-Dilger/s07/rsync-howto.shtml
# Extra slash appended to ${DESTDIR}/ is of uttermost importance!

rsync -av -e ssh $INCLUDED $EXCLUDED ${DESTDIR}/ ${SOURCEDIR}

Adding an exclude switch, --exclude="/.*" excludes all top-level hidden files/directories. Those few which should not be excluded are specified with series of --include switches. Order is important---first --include switches then --exclude switches.

piątek, 24 kwietnia 2009

Jak przekazać liczbę do AWK

Z tej racji, że na mojej stronie jest trochę informcji nt. AWK spory odsetek ją odwiedzających szuka informacji właśnie nt. tego języka. Z wpisów w logu serwera WWW dotyczących pola REFERER wynikałoby m.in., że nie jest oczywiste -- dla niektórych -- w jaki sposób można przekazać wartość zmiennej do skryptu AWK (Jak przekazać liczbę do AWK). Jest o tym napisane w Opisie... -- a konkretnie tutaj Argumenty wywołania programu -- może zbyt szczegółowo. Poniżej zatem bardziej łopatologiczny przykład:


gawk -v qq="A ku ku" 'BEGIN { print qq }'

Albo via skrypt shella:


#!/bin/bash
ARG1="$1"
ARG2="$2"
gawk -v A1="ARG1" -v A2=ARG2 'BEGIN { print A1, A2 }'

środa, 5 września 2007

Instalowanie pdftk

W uzupełnieniu informacji o manipulowaniu plikami PDF dziś wypróbowałem program pdftk do dzielenia plików PDF na strony. Działa. Jedyny kłopot był z instalacją. Z pomocą yuma się nie dało. Z pliku *.src.rpm ani kompilując ze źródeł też nie, bo za każdym razem był zgłaszany jakiś błąd. Znalazłem wreszcie działający pdftk-1.12-7.fc5.i386.rpm pod 'egzotycznym' adresem: linuxmirror.museum.state.il.us

Przy okazji zmieniłem swój skrypt do publikowania wpisów na tym blogu. Ułatwić sobie chciałem, żeby mi skróty rozwijał do pełnych adresów URL. Nie wchodząc w banalne szczegóły, problem pojawił się w miejscu, w którym apostrof i cudzysłów miały być częścią perlowego wyrażenia regularnego cytowanego w skrypcie basha. Jakoś tak:


perl -ne '..coś-tam...; s/blog:["'] ...itd... '

Po dłuższym kombinowaniu (głównie wokół dokumentów HERE) wreszcie wpadłem na oczywiste rozwiązanie:


perl -ne 'my $qchars = "\042\047"; ...coś-tam; s/blog:[$qchars] ...itd... '