리눅스

p.419

 

if [ 조건 ]
then
 true일 때의 식
else
 false일 때의 식
fi

 

# gedit if1.sh

 

#!/bin/sh
if [ "woo" = "woo" ]
then
  echo "true"
else
  echo "false"
fi
exit 0

 

# sh if1.sh
또는 권한 부여
# chmod 755 if1.sh
# ./if1.sh

 

p.421
A -eq B : equal
A -ne B : not equal
A -gt B : Greater Than, A > B
A -ge B : Greater or Equal , A >= B
A -lt B : Less Than, A < B
A -le B : Less or Equal, A <= B
!A : not A

 

#gedit if3.sh
#!/bin/sh
if [ 100 -eq 200 ]
then
  echo "equal"
else
  echo "not equal"
fi
exit 0

 

p.422 파일의 옵션
-f 일반파일이면 true
-d 디렉토리이면 true
-e 파일이 존재하면 true
-r 읽기가능한 파일이면 true
-w 쓰기가능한 파일이면 true
-x 실행가능한 파일이면 true
# gedit if4.sh

 

#!/bin/sh
fname=/lib/systemd/system/httpd.service
if [ -f $fname ]
then
  head -5 $fname
else
  echo "not installed"
fi
exit 0

 

p.423 case 문
case ~ esac
케이스)  케이스의 끝(break) 세미콜론 2개 ;;
*) => default 
#gedit case1.sh

 

#!/bin/sh
case "$1" in
  start)
    echo "start...";;
  stop)
    echo "stop...";;
  restart)
    echo "restart...";;
  *)
    echo "unknown...";;
esac
exit 0

 

./case1.sh aaaaa

 


p.425 and, or 연산자
and연산자
if [ A ] && [ B ]
if [ A ] -a [ B ]
or연산자
if [ A ] || [ B ]
if [ A ] -o [ B ]

 

-s : 크기가 0이 아니면
then : 다음 라인에 작성하거나 ; 뒤에 작성

 

사이즈가 0인 파일 생성
# touch andor.sh
파일 편집
# 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

 

p.427 for 반복문

 

for 변수 in 값1 값2 값3...
do
  반복할 문장
done

 

# touch forin1.sh
# 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

 

p.428 while 반복문

 

while [ 조건식 ]
do
 반복할 문장
done

 

# touch while1.sh
# gedit while1.sh
#!/bin/sh
while [ 1 ]
do
  echo "centos 7"
done
exit 0

 

# chmod 755 while1.sh
# ./while1.sh
종료하는 방법 : Ctrl+C 
다른 터미널에서 종료( -9 강제종료 )
  # ps -ef | grep while1.sh
  # kill -9 프로세스ID

 

p.431
# touch bce.sh
# 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

 

 

 

about author

PHRASE

Level 60  머나먼나라

이 세상에는 두 종류의 왕이 있다. 하나는 땅을 지배하는 왕이고, 하나는 마음을 지배하는 왕이다. -탈무드-

댓글 ( 4)

댓글 남기기

작성
  •    
  •    
  •  

리눅스 목록    more