Wednesday, January 9, 2013

Perl 내부에서 bash script 호출하는 법

다음과 같이 하면 된다.
system('ls -l');
키워드 : call, 안쪽, 시스템, 유닉스 명령어, UNIX command

Saturday, January 5, 2013

Perl 의 array pointer 에서 array 멤버 개수 알아내기

last index 는 $#$pa 이므로 여기에 하나 더하면 멤버 개수가 되고
print($#$pa + 1);

scalar 값으로 받아도 된다.
print(scalar(@$pa));

키워드 : element, elmt, number of, count, pointers, list

Perl 에서 array pointer 와 복사

my @a = @$pa;
위와 같이 하면 $pa 가 가리키는 내용물이 모두 @a 로 카피된다.
reference 만 얻어지는 것이 아님.
키워드 : copy

Perl 에서 주어진 path 를 parsing 해서 directory, extension, filename 을 알아내는 방법

다음과 같이 한다.
         use File::Basename;
         my @a = fileparse("/foo/bar/baz.txt", qr/\.[^.]*/);  
         print "dir : $a[1]\n";  
         print "file : $a[0]\n";  
         print "ext : $a[2]\n";  
다음과 같은 함수를 만들어두면 좀더 직관적으로 쓸 수 있다.
 use File::Basename;
 sub filepathParse  
 {  
      my @a = fileparse(shift, qr/\.[^.]*/);  
      return ($a[1], $a[0], $a[2]);  
 }  
검색어 : perl path manipulation, filename
키워드 : pathname, basename, string, filenameOnly
레퍼런스 : http://perldoc.perl.org/File/Basename.html

Perl 의 file test 문

-r File is readable by effective uid/gid.
-w File is writable by effective uid/gid.
-x File is executable by effective uid/gid.
-o File is owned by effective uid.

-R File is readable by real uid/gid.
-W File is writable by real uid/gid.
-X File is executable by real uid/gid.
-O File is owned by real uid.

-e File exists.
-z File has zero size (is empty).
-s File has nonzero size (returns size in bytes).

-f File is a plain file.
-d File is a directory.
-l File is a symbolic link.
-p File is a named pipe (FIFO), or Filehandle is a pipe.
-S File is a socket.
-b File is a block special file.
-c File is a character special file.
-t Filehandle is opened to a tty.

키워드 : flags
How can I check if a file exists in Perl?

UNIX pipes and redirects

The Unix Command Line: Pipes and Redirects ... tr, sort, uniq, comm 으로 사용한 예
 tr 'A-Z' 'a-z' <fnord.txt | tr -cs 'a-z' '\n' | sort | uniq | comm -23 - /usr/share/dict/words  

Pipes and Filters ... cat, wc, sed, awk, grep 으로 사용한 예
 $ cat apple.txt  
 core  
 worm seed  
 jewel  
 $ cat apple.txt | sed -e "s/e/WWW/"  
 corWWW  
 worm sWWWed  
 jWWWwel  
 $ cat apple.txt | sed -e "s/e/J/g"  
 corJ  
 worm sJJd  
 jJwJl  
 $ cat basket.txt  
 Layer1 = cloth  
 Layer2 = strawberries  
 Layer3 = fish  
 Layer4 = chocolate  
 Layer5 = punch cards  
 $ cat basket.txt | awk -F= '{print $1}'  
 Layer1  
 Layer2  
 Layer3  
 Layer4  
 Layer5            
 $ cat basket.txt | awk -F= '{print "HAS: " $2}'  
 HAS: cloth  
 HAS: strawberries  
 HAS: fish  
 HAS: chocolate  
 HAS: punch cards      

Unix - Pipes and Filters ... ls, grep, sort 로 사용한 예
 $ls -l | grep "Aug"  
 -rw-rw-rw-  1 john doc   11008 Aug 6 14:10 ch02  
 -rw-rw-rw-  1 john doc   8515 Aug 6 15:30 ch07  
 -rw-rw-r--  1 john doc   2488 Aug 15 10:51 intro  
 -rw-rw-r--  1 carol doc   1605 Aug 23 07:35 macros  
 $ls -l | grep "Aug" | sort +4n  
 -rw-rw-r-- 1 carol doc   1605 Aug 23 07:35 macros  
 -rw-rw-r-- 1 john doc   2488 Aug 15 10:51 intro  
 -rw-rw-rw- 1 john doc   8515 Aug 6 15:30 ch07  
 -rw-rw-rw- 1 john doc   11008 Aug 6 14:10 ch02  

Bash Reference Manual

Bash Reference Manual

which, whereis

Linux / UNIX: Command Not Found Error and Solution

bash 에서 while 문으로 command line argument 를 loop 하는 법

 for num  
 do  
   echo $num  
 done  
for num; do ...
for num in "$@"; do ... 는 동일.
How to loop through arguments in Bash ($1 $2 $3 and so forth)?

echo 를 newline 안하고 하는 방법

how do I echo $something >> file.txt without carriage return?
Bash Script, no new line for echo command
검색어 : bash echo without newline

bash script 의 while loop

Bash Guide for Beginners - Chapter 9. Repetitive tasks - The while loop
Bash Guide for Beginners - Chapter 9. Repetitive tasks - The for loop

Thursday, January 3, 2013

bash shell 의 if statement

 if [ $# -eq 0 ]  
 then  
      echo " is Zero"  
 else  
      echo " NOT Zero."  
      echo $#  
 fi  
두줄을 한줄에 쓰려면 다음과 같이 한다.
 if [ $# -eq 0 ]; then  
      echo " is Zero"  
 else  
      echo " NOT Zero."  
      echo $#  
 fi  
if [ $# -eq 0 ] 를 if [ $# -eq 0] 와 같이 쓰면 에러가 난다.

Perl 의 substring 함수

 my $a = "ABCDEFG";  
 my $b = substr($a, 2, 4);  
 print "$b\n";  
결과값은 CDEF 와 같이 나온다.
레퍼런스 : Chop, Chomp, Length, and Substring

Tuesday, January 1, 2013

TextWrangler 에서 특정 라인 넘버로 Go to Line 하는 방법

메뉴 : Search > Go to Line
핫키 : Command + J
Preference 세팅 : Editing > Use ‘Hard’ Line Numbering in Soft-Wrapped Text Views 에 체크.
인용 : The Go To Line command honors the “Use ‘Hard’ Line Numbering in Soft-Wrapped Text Views” option in the Editing preference panel.
레퍼런스 : TextWrangler 4.0 User Manual - Bare Bones Software

대소문자 변환

Converting Between Upper and Lower Case
http://perl.about.com/od/perltutorials/a/Perl-String-Manipulation.htm