Thursday, November 17, 2016

Summing numbers in a column

To sum several values in column from file we can use paste command and bash arithmetic expressions

$ echo $(($(paste -s -d'+' nums)))

I've always used tr to glue the values into one line, but the remaining '+' sign must be removed:

$ echo $(($(cat nums | tr '\n' '+' |sed 's/.$//')))

Or with awk:

$ awk '{s+=$1}; END { print s}' nums




Saturday, July 16, 2016

wget and line delimiters CR LF

If you download file with wget and the filename comes from windows file, the output filename will always have %0D at the end. To get rid of this, instead of:

wget $filename

use

wget $(echo "$filename" | tr -d '\r')

This will do the trick

Sunday, March 6, 2016

Raspberry Pi - booting into console mode

By default Raspbian boots into GUI mode. I don't need that. I wanted to change it to boot into command line. In Slackware or RedHat distros I always did it by editing runlevel in /etc/inittab.
Raspbian is different. I did it with raspi-config utility:

$ sudo raspi-config

And then I chose "Boot options"=>"B2 Console Autologin Text console, automatically logged in as 'pi' user"

Sunday, February 21, 2016

InsecureMag - downloading all issues

I bumped into InsecureMag (https://www.helpnetsecurity.com/insecuremag-archive/) - the free magazine on security. As I'm interested in it I decided to check it out. It's totally free. But being lazy I wanted to have all the issues on my harddisk. As usual bash comes with help:

#!/bin/bash
for i in {1..49}; do
  link="https://www.helpnetsecurity.com/dl/insecure/INSECURE-Mag-$i.pdf"
  wget -c "$link"
done


Have a nice reading :)





Raspberry Pi - Setting Static IP address

For Christmas I bought for myself :) Raspberry Pi and installed Raspbian and OpenELEC on this. For now I usually use it as low-energy  uploader/downloader or photo batch processor.
I want to have remote access to it also from outside my home network. By default Raspbian uses DHCP to obtain IP address from my home DHCP server. As I need to forward ports from my ISP router to my Raspberry PI, I need to give it always static IP address. I've been messing around with it a bit until I figured it out. Here is the recipe:

1. Turn off dhcp client which will always override things

$ sudo update-rc.d dhcpcd disable

You can also do it old school way by removing execute right:

$ sudo chmod a-x /etc/init.d/dhcpcd

2. Add the following lines to /etc/network/interfaces (the bold ones are new)

auto lo
auto eth0

iface lo inet loopback

#iface eth0 inet manuala
iface eth0 inet static
address 192.168.1.100
gateway 192.168.1.254
netmask 255.255.255.0


3. We need /etc/resolv.conf with static dns entry. This file is always rewritten by resolvconf utility, so we need to change it. In /etc/resolvconf.conf we add one line:

name_servers=192.168.1.254

My nameserver is my gateway at the same time. 

4. Restart networking services by:

 $ sudo /etc/init.d/networking restart

Works for me. We can check if everything is ok running:

$ /sbin/ifconfig