bashtips
Hits : 10,562
Bash Reference Manual Edition[link1]
Advanced Bash-Scripting Guide[link2]
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
Πως αλλάζουμε τα δικαιώματα σε όλα τα αρχεία που βρίσκονται κάτω από έναν κατάλογο;
find . -type f -exec chmod 640 {} \;
Πως αλλάζουμε τα δικαιώματα σε όλους τους καταλόγους που βρίσκονται κάτω από έναν κατάλογο;
Πως τα ψάχνουμε όλα :
find . -follow
Πως βρίσκουμε αρχεία τα οποία τελευταία φορά τροποιήθηκαν πριν από χ ημέρες;
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 ένα αρχείο:
Πως κάνω overwrite την μεταβλητή:
Πως αφαιρώ την συγκεκριμένη μεταβλητή:
Some Aliases:
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>
top
bash pattern matching
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
top
LOWERCASE
top
Bash Sub Shells
eg.
top
sequence
eg.
top
Quote
single quote
\x27 or \47
double quote
\x22 or \42
top
- [link1] http://tiswww.case.edu/php/chet/bash/bashref.html
- [link2] http://tldp.org/LDP/abs/html/