Manipolazione file da shell bash

sort
Questo comando ordina un flusso di testo, o un file, in senso crescente o decrescente, o secondo le diverse interpretazioni o posizioni dei caratteri.

uniq
Questo filtro elimina le righe duplicate di un file che è stato ordinato. È spesso usato in una pipe in coppia con sort.
-c premette ad ogni riga del file di input il numero delle sue occorrenze.

grep
è un comando bash dalla sintassi semplicissima che ci permette di ricercare all’interno dei file la presenza o meno di stringhe di testo, passando tali stringhe come variabili ben definite o come espressioni regolari.
possiamo utilizzare il punto come carattere “jolly”
–v, esegue l’operazione inversa mostrando tutte le righe in cui l’espressione non è presente.
–r estende la ricerca delle occorrenze in modo ricorsivo su tutti i files contenuti nelle sottocartelle in cui eseguiamo il comando. ci restituirà il nome del file in cui questa occorrenza esiste.

awk
Awk suddivide ogni riga dell’input che gli è stato passato in campi. Normalmente, un campo è una stringa di caratteri consecutivi separati da spazi, anche se esistono opzioni per modificare il delimitatore. Awk, quindi, analizza e agisce su ciascun singolo campo. Questo lo rende ideale per trattare file di testo strutturati — in particolare le tabelle — e dati organizzati in spezzoni logici, come righe e colonne.
echo uno due | awk ‘{print $1}’
# uno

echo uno due | awk ‘{print $2}’
# due

awk ‘{print $3}’ $nomefile
# Visualizza allo stdout il campo nr.3 del file $nomefile.

awk ‘{print $1 $5 $6}’ $nomefile
# Visualizza i campi nr.1, 5 e 6 del file $nomefile.

estrae gli indirizzi email
gawk -v RS='[^[:space:][:punct:]]+@[^[:space:][:punct:]]+[.][^[:space:][:punct:]]+’ ‘RT{print RT}’

head
Mostra le prime righe di un file di testo
tail
Mostra le ultime righe di un file di testo
cmp
confronta due file
wc
visualizza numero di righe, parole e caratteri di un file

[0-9a-zA-Z_/./-]+[@]{1}[0-9a-zA-Z_/./-]+

Send data from raspberry pi / banana pi to arduino

for Raspberry pi
git clone git://git.drogon.net/wiringPi
cd wiringPi
chmod +x build
./build

for banana pi
git clone https://github.com/LeMaker/WiringBPi.git
cd WiringBPi
chmod +x build
./build

Edit source before build for change pin

both pi
git clone --recursive git://github.com/ninjablocks/433Utils.git
make codesend

Arduino
Download and install library rc-switch from
https://github.com/sui77/rc-switch

usage from shell
./codesend 12345

arduino wrote in serial monitor (sketch ReceiveDemo_Simple from rc-switch example)
Received 12345 / 24bit Protocol: 1

for antenna look this

Make link to ssh server with password on putty

putty.exe -ssh root@somewhere.com -pw mypasswordforsomewherecom

For those using Windows, you can simply create a shortcut and pass in these parameters.
For example:

Create a shortcut on the desktop to putty.exe
Rename the shortcut to PuTTY – server.com
Right-click shortcut and choose Properties
Modify the target similar to:
“C:\Program Files\PuTTY\putty.exe” user@server.com -pw password
Click OK

Install dymo labelwriter on debian / linux & print from shell

apt-get install cups

scarico driver da dymo
cerchi so google dymo linux e esce la pagina, copi il link e…
wget http://download.dymo.com/Software/Linux/dymo-cups-drivers-1.4.0.tar.gz

Uncompress:
tar -zxvf dymo-cups-drivers-1.4.0.tar.gz

install dependencies
sudo apt-get install libcups2-dev
sudo apt-get install libcupsimage2-dev

Complile:
./configure
make
sudo make install

Configuring cups from web interface

for access to web interface from others machine in the lan:

edit /etc/cups/cupsd.conf  and replace:

Listen localhost:631

with

port 631

Add allow machine from lan in cupsd.conf (Allow From 192.168.1.0/255.255.255.0) :

# Limita l'accesso al server....
# Il default permette solo a locahost di accedre
<Location />
   Order allow,deny
   Allow From localhost
   Allow From 192.168.1.0/255.255.255.0
</Location>

# Limita l'accesso alla pagina di amministrazione...
<Location /admin>
   # La cifratura è disbilitata di default
   #Encryption Required
   Order allow,deny
   Allow From localhost
   Allow From 192.168.1.0/255.255.255.0

# Limita l'accesso alla pagina di configurazione...
<Location /admin/conf>
   AuthType Basic
   Require user @SYSTEM
   Order allow,deny
   Allow From localhost
   Allow From 192.168.1.0/255.255.255.0
</Location>

access to web interface with http://<ipmachine>:631

2015-03-26 19_16_25-Amministrazione - CUPS 1.5.3

2015-03-26 19_17_18-Aggiungi stampante - CUPS 1.5.3

2015-03-26 19_18_03-Aggiungi stampante - CUPS 1.5.3

2015-03-26 19_18_57-Aggiungi stampante - CUPS 1.5.3

2015-03-26 19_19_46-Imposta opzioni stampante - CUPS 1.5.3

For print image from shell:
lpr -P DYMO -o ppi=300 targa.bmp

For see the options of printer:
lpoptions -p PRINTER_NAME -l

telegram_logo

Set telegram-cli as daemon in debian based distro

Create a bash script in /etc/init.d with name telegramd

/etc/init.d/telegramd

#! /bin/sh
### BEGIN INIT INFO
# Provides:          telegram
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Commandline interface for Telegram
# Description:       Telegram-cli is a (unofficial) cli version of Telegram to chat from your console.
#                    This is an init script do make it a daemon.
#                    When used as daemon in conjuction  with LUA (scripting) you can use it to send your system
#                    commands to execute via other Telegram apps (PC - Phone - Web or other) while not
#                    logged in to the system.
#                    See: https://github.com/vysheng/tg for more information.
### END INIT INFO

DESC="Telegram Cli Daemon"
NAME=telegramd
PIDFILE=/var/run/$NAME.pid

. /lib/lsb/init-functions

# Carry out specific functions when asked to by the system
case "$1" in

    start)
        echo -n "Starting $DESC ... "
        start-stop-daemon --start --background --make-pidfile $PIDFILE --pidfile $PIDFILE \
            --exec /usr/bin/telegram -- -k /etc/telegram/server.pub -c /etc/telegram/telegram-cli/config -s /etc/telegram/action.lua -d -vvvv -E -R -D -C -P 2301 || true
        echo "Done."
    ;;

    stop)
        echo -n "Stopping $DESC ... "
        start-stop-daemon --stop --retry 2 --pidfile $PIDFILE \
            --exec /usr/bin/telegram || true
    echo "Done."

    ;;
    restart)
        echo -n "Restarting $DESC "
        start-stop-daemon --stop --retry 2 --pidfile $PIDFILE \
            --exec /usr/bin/telegram || true
        start-stop-daemon --start --background --make-pidfile $PIDFILE --pidfile $PIDFILE \
            --exec /usr/bin/telegram -- -k /etc/telegram/server.pub -c /etc/telegram/telegram-cli/config -s /etc/telegram/action.lua -d -vvvv -E -R -D -C -P 2301 || true
        echo "Done."
    ;;

    status)
        status_of_proc -p $PIDFILE telegram $NAME && exit 0 || exit $?
    ;;

    *)
        N=/etc/init.d/$NAME
        echo "Usage $NAME: /etc/init.d/telegramd {start|stop|restart|status}"
        exit 1
    ;;

esac

set correct permission:
chmod 755 telegramd

set the service for automatic start
update-rc.d telegramd default

Use:
service telegramd start
service telegramd stop
service telegramd status

for send message from shell or another script use
echo "msg CONTACT_NAME text" | nc -w 5 localhost 2301

script shell send telegram msg with expect

First read this post for install and configure telegram

Create a new script named sendtg:

#!/usr/bin/expect
set timeout 2
set msg [lindex $argv 1]
set dest [lindex $argv 0]
spawn telegram -k /etc/telegram/server.pub -c /etc/telegram/telegram-cli/config -W
expect "> "
sleep 10
send "\rmsg $dest $msg\r"
expect "> "
expect eof

Config

move the script in /usr/bin:
mv sendtg /usr/bin/sendtg

and set correct permission:
chmod +x /usr/bin/sendtg

Usage

sendtg User “message to send”

Update ip of wordpress in ksweb webserver

If you need a wordpress blog locally on phone,
you could install ksweb on android.

But you phone have a different ip each wireless, then wordpress don’t work because he use absolute path.

you need change ip before using the blog.

You could make a php file that change ip.

<?php
define( 'DB_HOST', 'localhost' );
define( 'DB_USER', 'blog_user' );
define( 'DB_PASSWORD', 'blog_pass');
define( 'DB_NAME', 'blog' );

$i=$_GET['i'];
//connessione al db
$conn_id = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die ('KO|500|Internal Server Error');
@mysql_select_db(DB_NAME, $conn_id) or die ('KO|500|Internal Server Error');
// @mysql_query("SET NAMES '".DB_CHARSET."'", $conn_id) or die ('KO|500|Internal Server Error');
 
$query = "update wp_options set option_value='".$i."' where option_id = 1;";
 mysql_query($query, $conn_id);
 
$query = "update wp_options set option_value='".$i."' where option_id = 2;";
 mysql_query($query, $conn_id);
?>

run script before launch blog with:

http://localhost/script.php?i=192.168.0.100

see next post for launch it with tasker

Install samba service (windows share)

Install with:
apt-get install samba samba-common-bin
backup the original configuration file:
cd /etc/samba
cp smb.conf smb.conf.original
edit the configuration file:
nano smb.conf

uncomment:
# security = user

for each directory that you want add

[download]
comment = Downloads directory
path = /downloads
writeable = yes

insert the authorized users:
smbpasswd -a claudio
Restart the service:
/etc/init.d/samba restart
or
service samba restart