View source for bashtips

{{anchor href="top"}}
Hits :  **{{hits}}**

((http://tiswww.case.edu/php/chet/bash/bashref.html Bash Reference Manual Edition))

((http://tldp.org/LDP/abs/html/ Advanced Bash-Scripting Guide))

http://www.linuxconfig.org/Bash_scripting_Tutorial


{{ToC from="h2" to="h3"}}

=== Bash Version ===

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

%%(code)
ebal@ebal:~\¤ echo $BASH_VERSION
3.2.25(1)-release
%%

((#top))
-----------

=== Random Integer Number ===

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

%%(code)
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

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

((#top))
-----------

=== History Output ===

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

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

((#top))
-----------

=== while ===

%%(code)
ls | while read line;do tar jxvf $line ; done
%%

((#top))
-----------

=== for ===

%%(code)
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)' *

%%(code)
$ ps -ef | grep ht[t]pd
%%

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

%%(code)
$ wc -l file
19308615 file

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

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

%%

((#top))
-----------

=== find ===

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

%%(html)
find . -type f -exec chmod 640 {} \;
%%

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

%%(html)
find . -type d -exec chmod 750 {} \;
%%

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

%%
find . -follow
%%

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

%%(html)
find . -type f -mtime +7 -exec ls -l {} \;
%%

==== 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 το οποίο ΔΕΝ θέλω να σταματήσει εάν μου χαθεί η σύνδεσή μου από την κονσόλα:

%%(code)
nohup abcd &
%%

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

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

((#top))
-----------

=== Password Expiration ===

!! BASH VERSION 2 !! 

%%(code)
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 ένα αρχείο:

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

Πως κάνω overwrite την μεταβλητή:
%%(bash)
dmesg >| dmesg.log
%%

Πως αφαιρώ την συγκεκριμένη μεταβλητή:
%%(bash)
set +o noclobber
dmesg > dmesg.log
dmesg > dmesg.log
%%

Some Aliases:

%%(bash)
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 ===

%%(code)
/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))
-----------

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

%%(code)
dmesg | tee dmesg.log
dmesg | sponge dmesg.log
dmesg > dmesg.log

grep -v dmesg.log | sponge dmesg.log
%%

((#top))
-----------

=== Ταξινόμηση IPs ===

%%(code)
alias sip='sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4'
%%

((#top))
-----------

=== xargs ===

%%(code)
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 ===

%%(code)
 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>
%%

%%(bash)
#!/bin/sh
clear

function ff() { 

string=$1

for i in $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 ===

%%(bash)

#!/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 ===

%%(bash)
$ word="lowercase"
$ echo "${word^^}"
LOWERCASE
%%

((#top))
-----------

=== LOWERCASE ===

%%(bash)
$ word="UPPERCASE"
$ echo "${word,,}"
uppercase
%%

((#top))
-----------

=== Bash Sub Shells ===

eg.

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

((#top))
-----------

=== sequence ===

eg.

%%(bash)
for j in `seq 0 255`; do for i in `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))
-----------