Tag Archives: bash

useful functions for .bashrc/.zshrc

ssh domain.name will open tmux terminal on the remote side or reconnect to the old one.
Also it reconnects when get issue with connection.

1
2
3
4
5
6
7
8
9
10
11
12
function ssh () {
    if ! command -v autossh &> /dev/null; then echo "Install autossh"; fi
    AUTOSSH_POLL=20
    export AUTOSSH_POLL
    if [ $# -eq 1 ]; then
        autossh -M 0 $@ -t "tmux attach -t alter || tmux new -s alter";
    elif [ $# -gt 1 ]; then
        /usr/bin/ssh -A $@
    else
        /usr/bin/ssh -h
    fi
}

Insert to pastebin service your message:
$ echo 'test1' | pb
https://pb.idone.su/view/8f60b2b5
$ curl https://pb.idone.su/view/raw/8f60b2b5
test1

$ pb «test2»
https://pb.idone.su/view/32974896
$ curl https://pb.idone.su/view/raw/32974896
test2

1
2
3
4
5
6
7
8
function pb(){
    if [ -n "$1" ]; then
        echo "$1" | curl -d expire=1440 -d private=1 --data-urlencode text@- https://pb.idone.su/index.php/api/create
    else
        stdin="$(cat)"
        echo "$stdin" | curl -d expire=1440 -d private=1 --data-urlencode text@- https://pb.idone.su/index.php/api/create
    fi
}

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

Bash: regexp

example:

I have the following files and i want to change the day of the date:

totalacc.US.2012-10-09.txt
totalacc.US.2012-10-10.txt
totalacc.US.2012-10-11.txt
totalacc.US.2012-10-12.txt
totalacc.US.2012-10-13.txt
totalacc.US.2012-10-14.txt
totalacc.US.2012-10-15.txt
totalacc.US.2012-10-16.txt
totalacc.US.2012-10-17.txt
totalacc.US.2012-10-18.txt
totalacc.US.2012-10-19.txt
totalacc.US.2012-10-20.txt
totalacc.US.2012-10-21.txt
totalacc.US.2012-10-22.txt
totalacc.US.2012-10-23.txt

script:

#!/bin/bash
regex="((2012-[019]{2}-)([0-9]{1,2}))"
for name in `ls -1|sort |egrep -e «totalacc.US.2012-[019]{2}- .*»`
do
[[ $name =~ $regex ]]
day=«${BASH_REMATCH[3]}»
echo $day
done

Bash: IFS delimiter

echo > test << EOF
a b c d
aa bb cc dd
a_b_c_d
EOF

for i in `cat test`; do echo $i; done    
a
b
c
d
aa
bb
cc
dd
a_b_c_d

IFS=$'_'
for i in `cat test`; do echo $i; done
a b c d
aa bb cc dd
a
b
c
d

IFS=$'\n'                                                                                                                                                              
for i in `cat test`; do echo $i;done   
a b c d
aa bb cc dd
a_b_c_d

IFS=$' '
for i in `cat test`; do echo $i; done
a
b
c
d
aa
bb
cc
dd
a_b_c_d