Tag Archives: shell

Specific bash and tool features for working with quotes

Magic dollar

Try to make
user$ x='\n' && echo $x
\n
the same if you use double-quotes, how to output new line ?

user$ x=$'\n' && echo $x

and you get new line! — magic $''

Hex in commands

Try to replace «asd to 'dsa in file
user$ sed -i 's/\»asd/\'dsa/g' file
> ^C
but shell will try to open quotes and it get 3 qoutes, of course it ask you to close one of quotes:
use hex!
user$ sed -i 's/\x22asd/\x27dsa/g' file

Script for downloading repositories from redmine

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
This script may be like a [b]template[/b] for you.
I've used it for downloading git projects from redmine's storage.
Before starting that script you have to authorize on the resource and save cookies in the file.


#!/bin/bash
URLBASE='https://domaint.tld/projects/1/repository/sql/revisions/master/raw/'
debug=0

# Function parses page and greps files names for next downloading
# This function get 1 parameter as a name of folder in which it'll download files
# Btw, this folder will be in the parent folder of that
function getfiles {
    for file in `egrep -o 'changes/.*\?rev=master\" ' index.html|sed -e 's/changes\///g' | sed -e 's/\?rev=master\"\ //g'`
    do
        echo $file
        echo "$URLBASE/$file"
        wget --no-check-certificate --load-cookies cookies-redmine.txt $URLBASE/$file -P ../$1
    done
}

# Function parses page and greps folders names for next downloading files in them.
function getfolders {
    for folder in `egrep -o 'show.*/?depth=1' index.html|sed -e 's/show\///g'|sed -e 's/\?depth=1//g'`
    do
        echo $folder
        mkdir -p ../$folder
        rm -rf index.html
        URL="$URLBASE"
        URL="$URL/$folder"
        wget --no-check-certificate --load-cookies cookies-redmine.txt $URL/
        if [ "$debug" == "1" ]; then
            exit
        fi
                #change this option to 1 for exit on the next turn, but before removing index.html
        debug=0
        getfiles $folder
        getfolders
    done
}

#Get listing of '/'
rm -rf index.html
mkdir -p ../$1
wget --no-check-certificate --load-cookies cookies-redmine.txt $URLBASE/$1/
getfiles $1
getfolders

Fedora: runscript (example: openvpn client)

/etc/init.d/opevpn-home-client

#!/bin/bash
#
# Run-level Startup script for the OpenVPN client
#
# chkconfig: 345 91 19
# description: Startup/Shutdown OpenVPN client 
OVPN_HOME="/home/alter/ovpn/"
case "$1" in
    start)
        echo -n "Starting OpenVPN home client: "
        cd $OVPN_HOME && /usr/sbin/openvpn ovpn.conf
        echo "OK"
        ;;
    stop)
        echo -n "Shutdown OpenVPN home client: "
        killall openvpn
        echo "OK"
        ;;
    reload|restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Usage: $0 start|stop|restart"
        exit 1
esac
exit 0
chmod +x /etc/init.d/opevpn-home-client
chkconfig --add openvpn-home-client 

Script for extracting archives

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash
if [ -f $1 ] ; then
 case $1 in
  *.tar.bz2) tar xvjf $1 ;;
  *.tar.gz) tar xvzf $1 ;;
  *.bz2) bunzip2 $1 ;;
  *.rar) unrar x $1 ;;
  *.gz) gunzip $1 ;;
  *.tar) tar xvf $1 ;;
  *.tbz2) tar xvjf $1 ;;
  *.tgz) tar xvzf $1 ;;
  *.zip) unzip $1 ;;
  *.Z) uncompress $1 ;;
  *.7z) 7z x $1 ;;
  *) echo "'$1' cannot be extracted via script" ;;
 esac
else
 echo "'$1' is not a valid file"
fi

Home automation

Home automation.

Accumulated some information from internet circuitry, controllers, various examples. As a confirmation of my small developing the project, decided to write an article ...
So we have: router dir-320 (with dd-wrt, the installation process described in the previous article about dir-320 + C), the controller ATmega8535, a small control circuit for optodriver, object management (reading lamps, power audio, home water heater), a little wires soldering iron, FREE  time.
What I would like to receive: software power management objects that feed from ~ 220 V.
Proper management of the facility as follows:
Mobile phone-> mobile browser-> wifi (mobile) -> wifi (router dir-320) -> set lightpd-> handler to perl-> uart (router) -> uart (controller mega8535) -> control signal (+5 controller) -> control signal (+5 optodrayvera) -> control signal (~ 220 optodriver) -> object management.
In this scheme can be replaced with «Mobile phone-> mobile browser-> wifi (mobile) ->» on a laptop, work PC and TP.
Also has the ability to manage through the wan port connected to the Inet Network, again by reference to lightpd.
What + this scheme:
  • No need to get out of bed for the inclusion of blackouts, TC management with a laptop or a mobile.
  • There is a possibility to saving by switch off / on the water heater, even if you're not at home — through the Net.
  • Integrated on / off devices (coming to the house, you can pre-enable such light wherever necessary. Including teapot before joining)
  • Often forget to disconnect the device from the network before leaving. This is possible at any time.
What — this scheme:
  • Maybe not quite convenient to get or keep a hand every once in a mobile phone for on/off light. But this is  solved by parallel turning optodriver with switch light.But, again, will not turn off remotely if it enabled.
  • Each device is an additional wire and a device for optodraiver. The wires need to get by secured to the wall or hide in the wall, which entails a change of wallpaper ))))) Optodriver also need to get by hiding in the socket, God forbid, if there is enough room)
  • Exception when MOBILE, laptop does not turn on the computer broke. Solved only parallel connection of a physical means of control.

Continue reading Home automation