코락 CoRock
코딩하는 락스타
코락 CoRock
  • 분류 전체보기 (393)
    • frameworks (19)
      • spring (19)
      • spring-boot (0)
      • testing (0)
    • languages (94)
      • java (39)
      • kotlin (0)
      • python (42)
      • r (13)
    • libraries (0)
    • programming (239)
      • android (13)
      • c (17)
      • cpp (22)
      • database (18)
      • design-pattern (4)
      • data-structures (11)
      • git (8)
      • hadoop (6)
      • html-css (7)
      • issue (4)
      • javascript (26)
      • jsp (34)
      • os (29)
      • php (6)
      • preferences (19)
      • etc (15)
    • discography (37)
      • k-pop (18)
      • pop (19)
    • blog (3)

블로그 메뉴

  • Programming
  • Java
  • JavaScript
  • Discography
  • K-Pop Songs
  • Pop Songs
  • Blog
  • Guestbook

공지사항

인기 글

태그

  • Java
  • 자바스크립트
  • python
  • javascript
  • 파이썬
  • Android
  • linux
  • CentOS
  • jsp
  • Spring
  • oracle
  • r

최근 댓글

최근 글

티스토리

반응형
hELLO · Designed By 정상우.
코락 CoRock

코딩하는 락스타

[Linux] shell script programming 2
programming/os

[Linux] shell script programming 2

2018. 10. 3. 17:09
반응형

13. shell script programming 2

shell script programming 1 게시글에 이어서 진행합니다.


13.2 실습예제


13.2.9 and, or 연산자


and 연산자

if [ A ] && [ B ]

if [ A && B ] → error

if [ A ] -a [ B ]


or 연산자

if [ A ] || [ B ]

if [ A ] -o [ B ]


공백에 주의해서 작성해야 함, if[A] → error


-s : 크기가 0이 아니면

then : 다음 라인에 작성하거나 ; 뒤에 작성


gedit andor.sh


#!/bin/sh

echo "input file name:"

read fname

if [ -f $fname ] && [ -s $fname ] ; then

head -5 $fname

else

echo "file not found or size 0"

fi

exit 0


chmod 755 andor.sh


./andor.sh




13.2.10 for 반복문

for 변수 in 값1 값2 값3 ...

do

반복할 문장

done


gedit forin1.sh 


#!/bin/sh

hap=0

for i in 1 2 3 4 5 6 7 8 9 10

do

hap=`expr $hap + $i`

done

echo "sum =" $hap

exit 0


chmod 755 forin1.sh


./forin1.sh




13.2.11 while 반복문

while [ 조건식 ]

do

반복할 문장

done


gedit while1.sh


#!/bin/sh

while [ 1 ]

do

echo "centos 7"

done

exit 0


chmod 755 while1.sh


./while1.sh


종료하는 방법 : Ctrl + C

다른 터미널에서 종료(-9 강제종ㄹ)

프로세스ID를 찾음


ps -ef | grep while1.sh


위에서 찾은 프로세스ID를 입력

kill -9 프로세스ID




gedit bce.sh


#!/bin/sh

echo "input(b:break, c:continue, e:exit)"

while [ 1 ] ; do

read input

case $input in

b | B)

break;;

c | C)

echo "continue..."

continue;;

e | E)

echo "exit..."

exit 1;;

esac

done

echo "end..."

exit 0


chmod 755 bce.sh



반응형
저작자표시 비영리 변경금지 (새창열림)
    'programming/os' 카테고리의 다른 글
    • [Linux] 네임서버 구축
    • [Linux] 텔넷 서버 설치
    • [Linux] shell script programming 1
    • [CentOS] 프로세스
    코락 CoRock
    코락 CoRock
    A COder dreaming of being a ROCKstar

    티스토리툴바