'쉘스크립트'에 해당되는 글 23건

  1. 2009.03.31 7. sed 란? 2
  2. 2009.03.05 5. 날짜표시 (date)
  3. 2009.03.04 4. 기본 루프문 (while)
2009. 3. 31. 17:01

7. sed 란?


첫번째 라인 출력

sed -n '1p' filename

마지막 라인 출력

sed -n '$p' filename

두번째 라인 출력

sed -n '2p' filename

첫번째부터 세번째 라인까지 출력 ( 1번 라인 부터 3번 라인까지 포함 )

sed -n '1,3p' filename

첫번째라인 부터 세번째 라인까지 삭제하고 출력
sed '1,3d' filename

특수한 문자열(예를들어 root)을 검색해서 그 라인만 출력

sed -n '/root/p' filename

love가 나오는 행과 peace가 나오는 행 사이의 모든 행들이 출력된다. love가 peace 다음에 나오면 love가 나오는 행부터 마지막 행까지 출력된다.

sed -n ‘/love/,/peace/p’ datafile


파일 안의 빈줄들만 모조리 삭제하고자 할때 .

sed '/^$d' 파일명 > 새파일명

특정 단어를 찾아서 변환하고자 할때
sed 's/TEST/test/g' > 새파일명

특정 단어를 찾아서 지우고자 할때
sed 's/TEST//g' > 새파일명



sed 에 관한 도움말

# sed --help
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
  -e script, --expression=script
                 add the script to the commands to be executed
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if extension supplied)
  -c, --copy
                 use copy instead of rename when shuffling files in -i mode
                 (avoids change of input file ownership)
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -r, --regexp-extended
                 use extended regular expressions in the script.
  -s, --separate
                 consider files as separate rather than as a single continuous
                 long stream.
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
      --help     display this help and exit
      --version  output version information and exit

2009. 3. 5. 15:18

5. 날짜표시 (date)

#!/bin/bash
date= date +%Y-%m-%d_%H:%M:%S
echo $date

이것은 날짜를 표시해주는 스크립트이고
date 명령어를 통해 원하는 방식으로 날짜와 시간등등을 표현해줄 수 있다.

실행시키면 다음과 같은 결과를 얻는다.

[root@localhost ~]# ./date.sh
2009-03-05_15:18:07


다른 방식으로도 가능하다 today 라는 변수를 지정해서 그 안에 날짜 정보를 넣으면
#!/bin/bash
today=`date +%Y%m%d`
echo $today
[root@localhost ~]# ./date2.sh
20090305

이렇게 나오게 된다.
2009. 3. 4. 15:42

4. 기본 루프문 (while)

#!/bin/bash
A=1
while [ $A -lt 5 ]
do
        echo $A
        A=`expr $A + 1`
done

이 쉡 스크립트를 보면 While 문을 사용해서
A 를 1부터 차례로 5보다 작은 수 즉 4까지 증가시키면서
화면에 A 값을 출력하는

기본 루프문 이다.

첫줄은 bash 쉡을 이용한다는 뜻
두번째 A=1 은  변수 A 의 초기값이 1 이라는 뜻
세번재 while 문에 조건  즉 변수A ($A) 가  5보다 작을때까지  다음에 오는 do 와 done 사이의 명령을 반복
echo $A 는 화면에 A의 값을 출력
A 에 1씩 더하는 것

무한 루프문
#!/bin/bash
while [ : ]
do
echo -n "test "
done

while [ : ] 에서 : 은  실행결과가 참이므로 do 와 done 사이의 명령을 계속 수행한다.
중지하려면 Ctrl + C 를 눌러 중지한다.