BashLecture2

From KBase

Jump to: navigation, search

Contents

[edit] Bash Conditional Expressions

[edit] Operators

|*op*|*true if file...*| |-a file|exists| |-e file|exists| |-d file|is a directory| |-f file|is a regular file| |-h file|is a simbolic link| |-r file|is readable| |-w file|is writable| |-x file|is executable| |-s file|has a size > 0|

|*op*|*meaning*| |file1 -nt file2|true if file1 is newer than file2| |file1 -ot file2|true if file1 is older than file2| |-z str|true if strlen(str) == 0| |-n str|true if strlen(str) <nop>!= 0| |str1 == str2|true if two strings are equal| |string op|<nop>==, <nop>!=, <, >| |numeric op|-eq, -ne, -lt, -le, -gt, -ge|

[edit] Usage

if test -e babo.cpp; then 
  echo "babo"
  echo "cpp"
fi

test -e babo.cpp && echo "babo";
$ a=babo
$ test -z $a && echo "babo"
$ a=
$ test -z $a && echo "babo"
babo

$ a=2;b=1
$ test $a -gt $b && echo "babo"
babo

[edit] Aliases

$ alias
alias als='alias | sort > ~/.aliases'
alias cda='cd /cygdrive/d/WS/w200506/alpha'
alias cdc='cd /cygdrive/d/WS/w200506/cima20'
alias cdt='cd /cygdrive/d/WS/w200603/taf'
alias dir='ls -o'
alias grep='grep --color'
alias ls='/bin/ls --color=auto -F'
alias maru='ssh scgyong@scgyong.net'
alias reverse='perl -e '\''while(<>){unshift(@arr,$_);}for(@arr){print;}'\'''
alias rgrep='grep -r'
alias vcvars='source ~/bin/vcvars.sh'
alias wmake='make -f WIPIModule.mak'
$ cat ~/.bashrc
source .aliases

* For almost every purpose, shell functions are preferred over aliases.

[edit] Directory Stack

$ dirs
~
$ popd
bash: popd: directory stack empty
$ pushd a
~/a ~
$ pushd b
~/a/b ~/a ~
$ pushd c
~/a/b/c ~/a/b ~/a ~
$ pushd d
~/a/b/c/d ~/a/b/c ~/a/b ~/a ~
$ pushd e
~/a/b/c/d/e ~/a/b/c/d ~/a/b/c ~/a/b ~/a ~
$ pushd f
~/a/b/c/d/e/f ~/a/b/c/d/e ~/a/b/c/d ~/a/b/c ~/a/b ~/a ~
$ popd
~/a/b/c/d/e ~/a/b/c/d ~/a/b/c ~/a/b ~/a ~
$ pushd +2
~/a/b/c ~/a/b ~/a ~ ~/a/b/c/d/e ~/a/b/c/d
$ pushd -2
~ ~/a/b/c/d/e ~/a/b/c/d ~/a/b/c ~/a/b ~/a
$ popd +1
~ ~/a/b/c/d ~/a/b/c ~/a/b ~/a
$ popd +0
~/a/b/c/d ~/a/b/c ~/a/b ~/a
$ popd -1
~/a/b/c/d ~/a/b/c ~/a
$ dirs
~/a/b/c/d ~/a/b/c ~/a
$

[edit] Job Control

$ ls -R > ls-R.txt&
[1] 3020
$ jobs
[1]+ Running /bin/ls --color=auto -F -R >ls-R.txt &
$ ls -R > ls-R2.txt&
[2] 1112
[1] Done /bin/ls --color=auto -F -R >ls-R.txt
$ jobs
[2]+ Done /bin/ls --color=auto -F -R >ls-R2.txt

* Commands * bg, fg, jobs, kill, wait, disown, suspend

$ kill -SIGINT 13325
$ kill -SIGCONT %1

---+ Regular Expressions

[edit] Links

* [[1][Regular Expression by GyonG]] * [[2][WikiPedia/Regular expression]] ---+ Useful Shell Tools

* __Do One Thing Well in a time !!__

[edit] grep

* NAME * grep, egrep, fgrep - print lines matching a pattern

* SYNOPSIS * grep [options] PATTERN [FILE...] * grep [options] [-e PATTERN | -f FILE] [FILE...]

원하는 문자열이 포함된 파일을 찾는다. 보통의 경우 해당 라인을 보여준다. |*options*|*meaning*| |--color|match 된 string 을 다른 색으로 표현해줌| |-c|몇 번 match 되었는지 보여줌| |-h|파일 이름은 보여주지 않음| |-i|ignore case| |-l|match되는지 여부만 보여줌| |-n|line number 와 함께 보여줌| |-o|라인 전체를 보여주지 않고 match 된 부분만 보여줌| |-q|아무것도 찍지 않음. exit status 만 가짐| |-r|recursive search. --include, --exclude 쓸 수 있음| |-v|match 되지 않는 라인을 보여줌|

[edit] find

* NAME * find - search for files in a directory hierarchy * SYNOPSIS * find [-H] [-L] [-P] [path...] [expression]

원하는 파일을 찾는다

|*test options*|*meaning*| |-name pattern|이름이 pattern 인 파일을 찾는다| |-path pattern|파일 이름 전체가 pattern 인 파일을 찾는다| |-prune|이름으로 찾을 때 디렉토리는 무시한다| |-regex pattern|regular expression 을 사용한다.폴더이름 포함한 패턴매칭을 사용한다| |-empty|빈 파일을 찾는다| |-mmin n|파일의 데이터가 n 분 전에 변경된 파일을 찾음.
-amin 은 access, -cmin 은 create 시간.
+n 은 n 분 초과, -n 은 n 분 미만을 뜻한다.
min 은 분, time 은 *24 시간 을 뜻한다| |-size n[cwbkMG]|해당 크기 파일을 찾는다|

|*test operators*|*meaning*| |(expr)|force precedence| |! expr, -not expr|not| |expr1 expr2, expr1 -a expr2, expr1 -and expr2|and| |expr1 -o expr2, expr1 -or expr2|or|

|*action options*|*meaning*| |-delete|지운다| |-exec command|실행한다| |-ok command|user 의 동의를 얻고 실행한다| |-print|파일이름을 찍는다| |-printf|파일이름을 특정포맷으로 찍는다|

$ find . -name 'Std*' -print
./cpp/wc/StdAfx.cpp
./cpp/wc/StdAfx.h
./cpp/wc/WordCounterE/Debug/StdAfx.obj
./cpp/wc/WordCounterE/StdAfx.cpp
./cpp/wc/WordCounterE/StdAfx.h
$ find . -regex 'Std*' -print
$ find . -regex '*Std*' -print
$ find . -regex '.*Std.*' -print
./cpp/wc/StdAfx.cpp
./cpp/wc/StdAfx.h
./cpp/wc/WordCounterE/Debug/StdAfx.obj
./cpp/wc/WordCounterE/StdAfx.cpp
./cpp/wc/WordCounterE/StdAfx.h
$ touch cpp/wc/StdAfx.h
$ find . -regex '.*Std.*' -mtime 1 -print
$ find . -regex '.*Std.*' -mtime -1 -print
./cpp/wc/StdAfx.h
$ touch cpp/wc/StdAfx.h cpp/exif/read.cpp
$ find . -mtime -5 -print
./cpp/exif/read.cpp
./cpp/wc/StdAfx.h
$ find . \( -regex '.*Std.*' -o -mtime -5 \) -print
./cpp/exif/read.cpp
./cpp/wc/StdAfx.cpp
./cpp/wc/StdAfx.h
./cpp/wc/WordCounterE/Debug/StdAfx.obj
./cpp/wc/WordCounterE/StdAfx.cpp
./cpp/wc/WordCounterE/StdAfx.h

man find 에 있는 예제들중 발췌

* find /tmp -name core -type f -print | xargs /bin/rm -f * Find files named core in or below the directory /tmp and delete them. Note that this will work incorrectly if there are any filenames containing newlines, single or double quotes, or spaces.

* find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f : * Find files named core in or below the directory /tmp and delete them, processing filenames in such a way that file or directory names containing single or double quotes, spaces or newlines are correctly handled. The -name test comes before the -type test in order to avoid having to call stat(2) on every file.

* find . -type f -exec file '{}' \; * Runs `file' on every file in or below the current directory. Notice that the braces are enclosed in single quote marks to protect them from interpretation as shell script punctuation. The semicolon is similarly protected by the use of a backslash, though ';' could have been used in that case also.

* find / \( -perm -4000 -fprintf /root/suid.txt '%#m %u %p\n' \) , \( -size +100M -fprintf /root/big.txt '%-10s %p\n' \) * Traverse the filesystem just once, listing setuid files and directories into /root/suid.txt and large files into /root/big.txt.

* find $HOME -mtime 0 * Search for files in your home directory which have been modified in the last twenty-four hours. This command works this way because the time since each file was last modified is divided by 24 hours and any remainder is discarded. That means that to match -mtime 0, a file will have to have a modification in the past which is less than 24 hours ago.

* find . -perm 664 * Search for files which have read and write permission for their owner, and group, but which other users can read but not write to. Files which meet these criteria but have other permissions bits set (for example if someone can execute the file) will not be matched.

* find . -perm -664 * Search for files which have read and write permission for their owner and group, and which other users can read, without regard to the presence of any extra permis- sion bits (for example the executable bit). This will match a file which has mode 0777, for example.

* find . -perm /222 * Search for files which are writable by somebody (their owner, or their group, or anybody else).

* find . -perm /220 * find . -perm /u+w,g+w * find . -perm /u=w,g=w * All three of these commands do the same thing, but the first one uses the octal representation of the file mode, and the other two use the symbolic form. These commands all search for files which are writable by either their owner or their group. The files don't have to be writable by both the owner and group to be matched; either will do.

* find . -perm -220 * find . -perm -g+w,u+w * Both these commands do the same thing; search for files which are writable by both their owner and their group.

* find . -perm -444 -perm /222 ! -perm /111 * find . -perm -a+r -perm /a+w ! -perm /a+x * These two commands both search for files that are readable for everybody (-perm -444 or -perm -a+r), have at least on write bit set (-perm /222 or -perm /a+w) but are not executable for anybody (! -perm /111 and ! -perm /a+x respectively)

[edit] sed

* NAME * sed - stream editor for filtering and transforming text

* SYNOPSIS * sed [OPTION]... {script-only-if-no-other-script} [input-file]...

대개 간단한 script 를 통해 치환을 하고자 할 때 사용한다.

$ ls
bin/  cpp/  index.html  kkk  perl/
$ ls | sed 's/^/echo /;'
echo bin/
echo cpp/
echo index.html
echo kkk
echo perl/

[edit] awk

$ cat dirout.txt
total 181K
drwx------+ 2 scgyong Users	 0 May 16 22:13 bin/
drwx------+ 4 scgyong Users	 0 Feb 16 02:01 cpp/
-rw-r--r--  1 scgyong 없음	  0 May 24 01:25 dirout.txt
-rw-r--r--  1 scgyong Users 177K May 13 16:11 index.html
-rw-r--r--  1 scgyong Users	 6 Apr 19 01:10 kkk
drwx------+ 6 scgyong Users	 0 Feb 16 02:00 perl/
$ cat dirout.txt | awk '{print $6}'

May
Feb
May
May
Apr
Feb

[edit] cat

[edit] echo

[edit] wc

[edit] seq

* NAME * seq - print a sequence of numbers

* SYNOPSIS * seq [OPTION]... LAST * seq [OPTION]... FIRST LAST * seq [OPTION]... FIRST INCREMENT LAST

$ seq 5 2 10
5
7
9

[edit] wget

* NAME * Wget - The non-interactive network downloader.

* SYNOPSIS * wget [option]... [URL]...

[edit] head, tail

[edit] tee

[edit] cmp, diff

[edit] perl

[edit] 종합

$ wget http://htmlhelp.berlios.de/books/chm.php 
$ grep -oP '\bftp://\S+?\.chm\b' chm.php | sed 's/^/wget /' | sh 
Personal tools
Wiki Help (mediawiki.org)