unix operacinės sistemos simonas kareiva 2013 m. rudens semestras
Post on 14-Dec-2015
221 Views
Preview:
TRANSCRIPT
UNIX operacinės sistemos
Simonas Kareiva
2013 m. rudens semestras
Paskaita #4•awk, sed ir kiti pagalbiniai įrankiai
•Darbas su masyvais
Paskaita #4 – I dalis
UNIX komandinės eilutės įrankių apžvalga: cut diff awk sed expand grep strings tr uniq zcat
cut
> echo aa:bb cc dd:ee | cut -d : -f 2bb cc dd
> echo aa:bb cc dd:ee | cut -d " " -f 2cc
> echo aa:bb cc dd:ee | cut -d " " -f 1,2aa:bb cc
> echo aa:bb cc dd:ee | cut -d " " -f 1,2,3aa:bb cc dd:ee
> cut -d : -f 1,5 /etc/passwd | tail -3_installer:Installer_atsserver:ATS Server_unknown:Unknown User
diff
> echo "pirmas\nantras" > 1.txt
> echo "pirmas\ntrecias" > 2.txt
> diff 1.txt 2.txt 2c2< antras---> trecias
> diff 1.txt 2.txt > diff.txt
> patch -p0 1.txt < diff.txt patching file 1.txt
> cat 1.txt pirmastrecias
expand
> printf "a\tb\n"a b
> printf "a\tb\n" | hexdump0000000 0961 0a62 0000004
> printf "a\tb\n" | expand | hexdump0000000 2061 2020 2020 2020 0a62 000000a
grep
> grep ^a /etc/passwdalias:*:81:81:User &:/var/qmail/alias:/nonexistent
> grep ^c /etc/passwdcyrus:*:60:60:the cyrus mail server:/nonexistent:/sbin/nologincourier:*:465:465:Courier Mail System:/var/spool/courier:/sbin/nologin
> grep -c nologin$ /etc/passwd29
> tail -f httpd-error.log | grep denied[Sun Oct 4 19:18:12 2009] [error] [client 78.60.155.45] (13)Permission denied: cannot read directory for multi: /users/eduardas/www/
> grep -E '(error|failure)' /var/log/console | grep -vc connect_to1945
strings
> strings /var/log/wtmp | grep -A4 simonasksimonask88.223.26.58JttypaJttypcJttypa
> strings /bin/sync | grep error$FreeBSD: src/lib/libc/i386/sys/cerror.S,v 1.10 1999/08/27 23:59:38 peter Exp $ error: Authentication errorExec format errorInput/output errorUndefined error: 0
tr
> cat bug.lt | tr e 3W3 ar3 us3r group of BSD syst3ms.Th3 group was 3stablish3d in th3 summ3r of 2002.It happ3n3d in small city of North Lithuania.
> cat bug.lt | tr '[a-z]' '[A-Z]'WE ARE USER GROUP OF BSD SYSTEMS.THE GROUP WAS ESTABLISHED IN THE SUMMER OF 2002.IT HAPPENED IN SMALL CITY OF NORTH LITHUANIA.
uniq
> grep DNS ppp.log | awk '{ print $10 }' | sort | uniq194.176.32.142194.176.32.163
> grep Illegal cache.log | tail -12009/10/04 19:37:34| urlParse: Illegal character in hostname 'www,youtube.lt’
> grep Illegal cache.log | cut -d " " -f 8 | sort | uniq -c | sort -rn | head 760 '[2a01:138:a001:201:' 57 '2a01%3a138%3aa001%3a201%3a%3a24' 57 '2a01%3a138%3aa001%3a201%3a%3a23' 57 '2a01%3a138%3aa001%3a201%3a%3a22' 57 '2a01%3a138%3aa001%3a201%3a%3a21' 12 '%20ssl.google-analytics.com' 7 'www,youtube,com' 4 'evi ka-pc'� 3 'www,youtube.lt' 3 'vista.%20liedm.lt’
zcat
> cat > test2.sh#!/bin/shecho dateexit 0;
> gzip test2.sh
> cat test2.sh.Z ;??Jtest2.shSV?O???/??JM??WHI,I?J??,Q0???d?I>
> zcat test2.sh.Z#!/bin/shecho dateexit 0;
sed
> echo diena | sed s/diena/naktis/ naktis
> echo "123 abc" | sed 's/[0-9]*/& &/'123 123 abc
> echo kaire desine | sed 's/\([a-z]*\) \([a-z]*\)/\2 \1/'desine kaire
> sed '/start/,/stop/ s/#.*//'?
sed
Palyginkite: sed 's/\([a-zA-Z]*\) \([a-zA-Z]*\) /\1 /' sed 's/[a-zA-Z]* //2'
sed 's/./&:/20' sed 's/^..................../&:/'
sed -n 's/jonas/&/p' <filegrep jonas file
sed 'y/abcdef/ABCDEF/’ tr abcdef ABCDEF
sed
#!/bin/sh
sed -n '/begin/,/end/ { s/#.*// s/[ ^I]*$// /^$/ d p}
’
sed
#!/bin/sh# Tokiu budu isvalome eilutes kurios prasideda # # (komentarus) tarp zodziu ‘begin’ ir ‘end’sed -n '
/begin/,/end/ { s/#.*// s/[ ^I]*$// /^$/ d p}
'
sed (advanced)> cat cat.txt This is my cat my cat's name is bettyThis is my dog my dog's name is frank
> sed 'N;s/\n / /;P;D;' cat.txt This is my cat my cat's name is bettyThis is my dog my dog's name is frank
N - pridedame sekančią eilutę prie darbinio buferios - keitimas/\n / - naujos eilutės ir tarpo atitikimas/ / - keičiamas į vieną tarpąP - atspausdina pirmą buferio eilutęD ištrina pirmą buferio eilutę ir tęsia darbą
awk
> ls -l-rwxrwxrwx@ 1 simonask staff 7497216 Rgs 14 15:59 UNIX_1.pps
> ls -l | awk ' BEGIN { print ”Byla\t\tSavininkas" } { print $9, "\t\t", $3} END { print " - DONE -" } 'Byla SavininkasUNIX_1.pps simonask - DONE -
> dateSekmadienis, 2009 m. spalio 4 d. 20:21:58 EEST
> date | awk '{print $1}'Sekmadienis,
> date | awk '{print $2}'2009
awk
BEGIN { print ”ivesk skaiciu";}{ print ”Skaiciaus ", $1, ” kvadratas yra ", $1*$1; print ”ivesk dar viena skaiciu";}END { print ”Ate"}
awk
awk '{print $2, $1}' skaityk.txt
awk '{print NR "\t" $0}’ skaityk.txt
awk '{l=length();s=int((79-l)/2); printf "%"(s+l)"s\n",$0}'
Palyginkite:head -10awk 'NR < 11’
awk '!/paieska/' skaityk.txtgrep –v paieska skaityk.txt
užduotys
ls -d *.sh | sed 's/\(.*\).sh$/cp "&" \1/' | sh
____________________________________________________________
STR="`sed <<<"$STR" "s/\\\\(.\\\\)/\\\\1('\\\\\\\\s*\\\\\\\\+\\\\\\\\s*')?/g;s/('\\\\\\\\s\\\\*\\\\\\\\+\\\\\\\\s\\\\*')?$//"`"
____________________________________________________________
find ${1-.} -type d -print | env LC_ALL='C' sort -if |sed -e "s,^${1-.},," \
-e "/^$/d" \-e "s,[^/]*/\([^/]*\)$,\`-----\1," \-e "s,[^/]*/,| ,g"
Spec. kintamieji
$0 pačios komandos pavadinimas.
$# pateiktų argumentų skaičius
$? prieš tai dirbusio proceso išėjimo būsena
$$ šio interpretatoriaus proceso numeris
$! paskutinio foninio PID numeris
$- dabartinio interpretatoriaus paleisties parametrai
$* visi parametrai, pradedant nuo $1.
$@ kaip ir $*, tik naudojamas kabutėse
/bin/sh masyvai
area[11]=23
area[13]=37
area[51]=UFOs
echo ${area[11]}
Operacijos su masyvais
area[5]=`expr ${area[11]} + ${area[13]}`
area[6]=`expr ${area[11]} + ${area[51]}`
Kur klaida?
Kiti būdai
area2=( nulis vienas du trys keturi )
echo ${area2[0]}
area3=([17]=septyniolika [24]=dvidesimtketuri)
array1=( `cat "$filename" | tr '\n' ' '`) read -a colors
base64_charset=( {A..Z} {a..z} {0..9} + / = )
Tipo nėra
string=abcABC123ABCabc
echo ${string[@]} # abcABC123ABCabc
echo ${string[*]} # abcABC123ABCabc
echo ${string[0]} # abcABC123ABCabc
echo ${string[1]} #
echo ${#string[@]} # 1
Prisiminkime ir pritaikykime
array= ( nulis vienas du trys keturi penki )# Elementai: 0 1 2 3 4 5
echo ${array[0]} # nulisecho ${array:0} # nulisecho ${array:1} # ulis
echo ${#array[0]} # 5echo ${#array}echo ${#array[1]} # 6
echo ${#array[*]} # 6echo ${#array[@]} # 6
Operacijos su eilutėmis
arrayZ=( one two three four five five )
echo ${arrayZ[@]:0} # one two three four five five
echo ${arrayZ[@]:1} # two three four five five
echo ${arrayZ[@]:1:2} # two three
echo ${arrayZ[@]#f*r} # one two three five five
echo ${arrayZ[@]##t*e} # one two four five five
echo ${arrayZ[@]%h*e} # one two t four five five
echo ${arrayZ[@]%%t*e} # one two four five five
Elementų keitimas (replacement)
echo ${arrayZ[@]%%t*e} # one two four five five
echo ${arrayZ[@]//iv/YY} # one two three four fYYe fYYe
echo ${arrayZ[@]//fi/} # one two three four ve ve
echo ${arrayZ[@]/#fi/XY} # one two three four XYve Xyve
echo ${arrayZ[@]/%ve/ZZ} # one two three four fiZZ fiZZ
echo ${arrayZ[@]/%o/XX} # one twXX three four five five
Skaidrė #5 – naudingi scenarijai
Praktiniai pavyzdžiai kaip išgauti naudos iš scenarijų
Kai kuriuos dažnai naudoja ir dėstytojas
Atkreipkite dėmesį, jog naudojami įvairūs išoriniai įrankiai
singleton#!/bin/bash
echo "argument: $1"
RESULT=`/bin/ps xauwww | /bin/grep -v "$0" | /bin/grep "^$USER.*$1$"`
if [ -z "$RESULT" ] ; thenLC_MESSAGES=en_US $*else echo "Sorry, already running as:" echo " $RESULT” echo "singleton reports that $1 is already started” fi
#end
mysqltcpdump
# tcpdump -i eth0 -s 0 -l -w - dst port 3306 | strings | perl -e 'while(<>) { chomp; next if /^[^ ]+[ ]*$/;if(/^(SELECT|UPDATE|DELETE|INSERT|SET|COMMIT|ROLLBACK|CREATE|DROP|ALTER)/i) {if (defined $q) { print "$qn"; }$q=$_;} else {$_ =~ s/^[ t]+//; $q.=" $_";}}'
wipeout#!/bin/bash
time="2003-04-25 00:00"
reffile=wipeout.ref.$RANDOMtouch -d "$time" $reffileechoecho Deletes all dirs that are older than $timeecho find . -type d -maxdepth 1 -path './*' ! -newer $reffile | while read dir; do echo rm -rf "$dir" rm -rf "$dir"donerm -f $reffile
ts2date
!#/bin/bash#EPOCH=$1
EPOCH=1000000000
DATE=$(perl -e “require ‘ctime.pl’; print &ctime($EPOCH);”)echo $DATEDATE=$(perl -e “print scalar(localtime($EPOCH))”)echo $DATEDATE=$(echo $EPOCH|awk ‘{print strftime(”%c”,$1)}’)echo $DATE
mrename
ls -d *.php3 | \
sed 's/\(.*\).php3$/mv "&" "\1.php"/' | \
sh
#!/usr/bin/perl -w # camel codeuse strict;
$_='ev al("seek\040D ATA,0, 0;");foreach(1..3) {<DATA>;}my @camel1hump;my$camel; my$Camel ;while( <DATA>){$_=sprintf("%-69s",$_);my@dromedary 1=split(//);if(defined($_=<DATA>)){@camel1hum p=split(//);}while(@dromeda ry1){my$camel1hump=0 ;my$CAMEL=3;if(defined($_=shif t(@dromedary1 ))&&/\S/){$camel1hump+=1<<$CAMEL;} $CAMEL--;if(d efined($_=shift(@dromedary1))&&/\S/){ $camel1hump+=1 <<$CAMEL;}$CAMEL--;if(defined($_=shift( @camel1hump))&&/\S/){$camel1hump+=1<<$CAMEL;}$CAMEL--;if( defined($_=shift(@camel1hump))&&/\S/){$camel1hump+=1<<$CAME L;;}$camel.=(split(//,"\040..m`{/J\047\134}L^7FX"))[$camel1h ump];}$camel.="\n";}@camel1hump=split(/\n/,$camel);foreach(@ camel1hump){chomp;$Camel=$_;y/LJF7\173\175`\047/\061\062\063\ 064\065\066\067\070/;y/12345678/JL7F\175\173\047`/;$_=reverse; print"$_\040$Camel\n";}foreach(@camel1hump){chomp;$Camel=$_;y /LJF7\173\175`\047/12345678/;y/12345678/JL7F\175\173\0 47`/; $_=reverse;print"\040$_$Camel\n";}';;s/\s*//g;;eval; eval ("seek\040DATA,0,0;");undef$/;$_=<DATA>;s/\s*//g;( );;s ;^.*_;;;map{eval"print\"$_\"";}/.{4}/g; __DATA__ \124 \1 50\145\040\165\163\145\040\157\1 46\040\1 41\0 40\143\141 \155\145\1 54\040\1 51\155\ 141 \147\145\0 40\151\156 \040\141 \163\16 3\ 157\143\ 151\141\16 4\151\1 57\156 \040\167 \151\164\1 50\040\ 120\1 45\162\ 154\040\15 1\163\ 040\14 1\040\1 64\162\1 41\144 \145\ 155\14 1\162\ 153\04 0\157 \146\ 040\11 7\047\ 122\1 45\15 1\154\1 54\171 \040 \046\ 012\101\16 3\16 3\15 7\143\15 1\14 1\16 4\145\163 \054 \040 \111\156\14 3\056 \040\ 125\163\145\14 4\040\ 167\1 51\164\1 50\0 40\160\ 145\162 \155\151 \163\163 \151\1 57\156\056
top related