Programming/linux왕초보
shell programming - 명령어
bluelimn
2008. 9. 11. 14:56
728x90

▼ [예제]
$vi sample1
#!/bin/sh
rm -rf file*
echo > file1
echo file2
mkdir file3
echo > file4
for var in file*
do
if [-d "$var"]
then
break;
fi
done
echo "directory name : $var"
exit 0
$chmod +* sample1
$./sample1
directory name : file3 |





: $(variable := value)
▼ [예제]
$vi sample2
#!/bin/sh
rm -r file
if [-f file]
then
:
else
echo "file did not exist"
fi
exit 0
$chmod +* sample2
$./sample2
file did not exist |



▼ [예제]
$vi sample3
#!/bin/sh
rm -rf file*
echo > file1
echo file2
mkdir file3
echo file4
for var in file*
do
if [-d "$var"]
then
continue
fi
echo "file name : $var"
done
exit 0
$chmod +* sample3
$./sample3
file name : file1
file name : file2
file name : file3 |



▼ [예제]
$vi sample3
#!/bin/sh
rm -rf file*
echo > file1
echo file2
mkdir file3
echo file4
for var in file*
do
if [-d "$var"]
then
continue
fi
echo "file name : $var"
done
exit 0
$chmod +* sample3
$./sample3
file name : file1
file name : file2
file name : file3 |



.Shellscript







▼ [예제]
$vi sample6
#!/bin/sh
if [-f /etc/passwd]
then
exit 0
fi
exit 1 |




▼ [예제]
$vi sample7
#!/bin/sh
string1="shell programming"
export string2="linux shell"
sample8
$vi sample8
#!/bin/sh
echo"$string1"
echo "string2"
$chmod +* sample7 sample8
$./sample7
linux shell |



var2='expr $var1 + 1'





printf "format string" parameter1 parameter2 ...
▼ [formar string의 변환식별자]




▼ [formar string의 변환식별자]
$vi sample9
#!/bin/sh
echo Date : $(date)
set %(date)
echo Date : $6년 $2 $3$ $4
exit 0
$chmod +* sample9
$./sample9
Date : 일 7월 22 13:38:54 KST 2001
Date : 2001년 7월 22일 13:38:54 |





▼ [예제]
$vi sample10
#!/bin/sh
echo Date : $(date)
set %(date)
while ["$1"=!=""]
do
echo $1
shift
done
exit 0
$chmod +* sample10
Date : 일 7월 22 13:38:54 KST 2001
일
7월
22
13:49:31
KST
2001
$./sample9
Date : 일 7월 22 13:38:54 KST 2001
Date : 2001년 7월 22일 13:38:54 |





trap 'command' signo signo [signo]
▶ interupt processing
● Shell 프로그램의 수행은 인터럽트가 걸릴 수 있으며, 사용자는 <Break> / <Del> / <Reboot>키를 사용하여 키보드로 인터럽트를 걸 수 있다.
● 기본적으로 모든 Shell 프로그램은 인터럽트가 될 수 있을 뿐만 아니라 Shell 프로그램이 끝나기 전에 임시파일을 지울 경우나 임계 영역의 수행을 종료시킬 때 사용자가 인터럽트 처리하는 것이 필요하다. |
▶ interupt
● Shell 프로그램은 trap 명령을 이용하여 인터럽트를 다룰 수 있다
● trap 명령은 인터럽트를 다루기 위해 Unit Operating System 과 Interface를 함
● 유닉스 운영체계는 signal이라고 하는 특성을 제공한다
● signal은 프로그램에게 비정상적인 사항이 발생되었음을 알려주는 방법이고 키보드나 다른 프로그램으로부터 올 수 있다. |
▶ Signal
● 커널과 프로세스, 프로세스와 프로세스간에 어떤 이벤트 발생 시 프로세스에게 전달되는 수단으로 사용된다
● 프로세스들은 로그아웃이나 인터럽트와 같은 몇 가지 이벤트들이 발생하게 될 경우 시스템에게 전송하고 대부분의 시그널들은 프로세스들이 중단시킴
● forground에서 수행되는 명령어만이 키보드가 만드는 신호를 인식하고, background에서 수행중인 프로그램에 대해서는 kill 명령을 사용하여 신호전달이 가능 |
▶ trap 기능
● 신호에 따른 명령 수행
: 인터럽트 신호를 받아서 프로그래머가 원하는 작업을 수행하게 함
● 신호의 무시
: 프로세스를 중단시키는 대신 해당 신호망을 무시함
● 신호의 재설정
: 신호를 원래의 값으로 되돌려 놓음 |


728x90