Hits : 9436


Bash Reference Manual Edition


Advanced Bash-Scripting Guide


http://www.linuxconfig.org/Bash_scripting_Tutorial


Bash Version


Χρήσιμες μεταβλητές του bash shell


ebal@ebal:~\€ echo $BASH_VERSION
3.2.25(1)-release


top


Random Integer Number


Πως μπορούμε να παράγουμε τυχαίους αριθμούς χρησιμοποιώντας μόνο το Bash !!!


ebal@ebal:~\€ echo $RANDOM
29183
ebal@ebal:~\€ echo $RANDOM
1628
ebal@ebal:~\€ echo $RANDOM
408
ebal@ebal:~\€ echo $RANDOM
14008
ebal@ebal:~\€ echo $RANDOM
16924

Timestamp


Πως μετατρέπουμε ένα timestamp (seconds from 1970)
σε human read date


έστω λοιπόν το


1170384301


> date -d '1970-01-01 1170384301 sec'
Fri Feb  2 02:45:01 EET 2007


top


History Output


Η εντολή history μας εμφανίζει τις προηγούμενες εντολές που πληκτρολογήσαμε
σε ένα τερματικό. Για να έχουμε και την μέρα / ώρα χρειάζεται να προσθέσουμε
στο αρχείο /etc/bash.bashrc το παρακάτω:


export HISTTIMEFORMAT='%d/%m/%Y %H:%M '


top


while


ls | while read line;do tar jxvf $line ; done


top


for 


for i in * ; do tar jxvf $i ; done

for i in `ls -1`; do echo $i; cat $i; done


top


grep


egrep '(text_search_1|text_search_2)' *


$ ps -ef | grep ht[t]pd


Πως ψάχνουμε σε ένα αρχείο με πολλές γραμές ένα λεκτικό και τυπώνουμε επίσης και την προηγούμενη ή την επόμενη γραμμή


$ wc -l file
19308615 file

$ grep -B 1 '0$' file
...
...
...

$ grep -A 1 '0$' file
...
...
...


top


find


Πως αλλάζουμε τα δικαιώματα σε όλα τα αρχεία που βρίσκονται κάτω από έναν κατάλογο;


Formatter "highlight/html" not found


Πως αλλάζουμε τα δικαιώματα σε όλους τους καταλόγους που βρίσκονται κάτω από έναν κατάλογο;


Formatter "highlight/html" not found


Πως τα ψάχνουμε όλα :


find . -follow


Πως βρίσκουμε αρχεία τα οποία τελευταία φορά τροποιήθηκαν πριν από χ ημέρες;


Formatter "highlight/html" not found

recursive search & replace with sed 


We need to change in all php files the string 'ebal' with the string 'Evaggelos Balaskas' under /www directory:


find /www -type f -name "*.php" -exec sed -r -i 's/ebal/Evaggelos Balaskas/' {} \;


Change ebal or ebalaskas to 'Evaggelos Balaskas':


find /www -type f -name "*.php" -exec sed -r -i 's/(ebal|ebalaskas)/Evaggelos Balaskas/' {} \;

exclude directory from find


find / -type f ! -path '*snap*'


Search all files over 1GB size, excluding snapshot or temporary folder:


find / -type f ! -path '*snap*' ! -path '*tmp*' ! -path '/proc'-size +1G


find . -name .snapshot -prune -o -name exports -print


top



cat | tac

Hup Or NoHup


Πως τρέχω κάποιο script το οποίο ΔΕΝ θέλω να σταματήσει εάν μου χαθεί η σύνδεσή μου από την κονσόλα:


nohup abcd &


http://en.wikipedia.org/wiki/Nohup


https://linux.die.net/man/1/nohup


top


Password Expiration


!! BASH VERSION 2 !!


clear; sort -k 3 -t ":" /etc/shadow | awk -F ":" '{ if ( ( $2 != "!!" && $2 != "*" ) && ( $5 != "99999" ) && ( $6 != "" ) ) { "date --date \"1970-1-1 "($3+$5)" days\" "| getline expire; print $1 " " expire } }' | awk '{print $1 " expires on " $4"/"$3"/"$7}'


top


Cdrom


Eject Cdrom : $ eject cdrom
Insert Cdrom : $ eject -t cdrom


top


Dont Delete/Overwrite Files


ΔΕΝ θέλω κατά λάθος να κάνω overwrite ένα αρχείο:


set -o noclobber
dmesg > dmesg.log
dmesg > dmesg.log
bash: dmesg.log: cannot overwrite existing file

Πως κάνω overwrite την μεταβλητή:

dmesg >| dmesg.log

Πως αφαιρώ την συγκεκριμένη μεταβλητή:

set +o noclobber
dmesg > dmesg.log
dmesg > dmesg.log

Some Aliases:


alias s="cd .."
alias mv="mv -iv"
alias md="mkdir -iv"
alias cp="cp -iv"
alias rm="rm -iv"

top


Linux standard I/O


http://tldp.org/LDP/abs/html/io-redirection.html


Handle Name Description
0 stdin Standard input
1 stdout Standard output
2 stderr Standard error
&> stdout & stderr
2>&1 stderr to stdout

top


Rsync Example


/usr/bin/rsync -vrpogtzPh  --stats --delete -e ssh server:/some/dir .

/usr/bin/rsync -vazP --log-file /var/log/rsync.log --stats --delete -e ssh server:/some/dir .


top


Πως να αποθηκεύουμε την έξοδο


dmesg | tee dmesg.log
dmesg | sponge dmesg.log
dmesg > dmesg.log

grep -v dmesg.log | sponge dmesg.log


top


Ταξινόμηση IPs 


alias sip='sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4'


top


xargs


ls -1 | xargs -t -I{} ls -l {}

zenity


zenity --info --text="IP: `curl -s whatismyip.org`"

netcat


http://netcat.sourceforge.net/


Destination box: nc -l -p 2342 | tar -C /target/dir -xzf -
Source box: tar -cz /source/dir | nc Target_Box 2342

grep ip 


ifconfig -a | grep -E -o 'inet addr:([0-9]{1,3}\.){3}[0-9]{1,3}' | awk -F: '{print $2}'


top


print single quote in awk


awk '{print " '\'' "}'


top


notify-send


/usr/bin/notify-send -u critical "<b>Please Hibernate ASAP</b>" "Your battery level is 10% and your computer is out of AC. Plz put your computer to charge" -i battery-caution


top


internal field separator


<space><tab><newline>


#!/bin/sh
clear
 
function ff() { 
 
string=$1
 
forin $string;
do
	echo $i
done
 
}
 
ff "begin this string begin this string has no end has no end" 
 
IFS=.
 
echo "==="
 
ff "begin.this.string.begin.this.string.has.no.end.has.no.end"

top



bash pattern matching


#!/bin/sh
clear
string="begin.this.string.begin.this.string.has.no.end.has.no.end"
word="*begin"
word2="end*"
 
# Print string
echo 0: ${string}
# Print length
echo 0: ${#string}
 
# Remove first match
echo 1: ${string#${word}}
 
# Remove till last match
echo 2: ${string##${word}}
 
# Remove last match 
echo 3: ${string%${word2}}
 
echo 4: ${string% %${word2}}

output


0: begin.this.string.begin.this.string.has.no.end.has.no.end
0: 57
1: .this.string.begin.this.string.has.no.end.has.no.end
2: .this.string.has.no.end.has.no.end
3: begin.this.string.begin.this.string.has.no.end.has.no.
4: begin.this.string.begin.this.string.has.no.


top


uppercase


$ word="lowercase"
$ echo "${word^^}"
LOWERCASE

top


LOWERCASE


$ word="UPPERCASE"
$ echo "${word,,}"
uppercase

top


Bash Sub Shells


eg.


diff -u <(sort /etc/group) <(sort /etc/group.pacnew)

top


sequence


eg.


forin `seq 0 255`; do forin `seq 0 255` ; do  seq -f "10.$j.$i.%g" 0 255 ; done ; done

top



Quote


single quote


\x27

or

\47


double quote


\x22

or

\42


top