쉘프로그래밍.txt
33KB · 810 lines
������
�� ��ü�� �ܾ ��忡 �����ϼż� ���ø� ���ϰ� ���Դϴ�.
1. Shell�� Shell programming�̶�?
2. �� ���� - ����, ����
3. �� ���� - ���ǹ�,�����
4. �� ���� - ���
5. �� ���� - �Լ�
6. �� ���� - ��ɾ�
7. �� ���� - ��ɽ���
8. �� ���� - �� ��ũ��Ʈ ���ο��� �Է� ����
9. �� ���� - ���
10. �츮�� ��� ���� �� ��ũ��Ʈ
11. ���ڷ�
1. Shell�� Shell programming�̶�?
���̶� ����� �����ؼ� MS-DOS�� ��ɾ� ó����� command.com�� ���ϴٰ� �� ��
�ִ�. ������ �� ���� MS�� �ͺ��� ��û���ٰ� �� �� �ִ�. �츮�� �������� ���
�ϸ鼭 ���� �ִ� �͵�� ��κ�� �� ��ũ������ ¥���ִ� �� ���� �˼� �ִ�.
���� ���ڸ� ���н�(������) �ý��ۿ� �̾�ִ� ������̽��ν� ���� ���� ��ɾ
�Է��ϸ� ���� �ش� ���� �ü���� ������ �� �ֵ��� ������ִ� ��Ȱ�� �Ѵ�.
���� ������ �پ��ϳ� ��� �츮��� ���� ��� bash�� tcsh�̴�.
+------------+-------------------------------------------------------------+
|�� ��Π| ������ ���� |
+------------+-------------------------------------------------------------+
|sh(Bourne) | ����� �� |
+------------+-------------------------------------------------------------+
|csh,tcsh,zsh| C ��, Berkeley ���н� ������ Bill Joy�� ����. |
+------------+-------------------------------------------------------------+
|ksh,pdksh | Korn ��, pdksh�� public domain Korn��. David Korn�� ���� |
+------------+-------------------------------------------------------------+
|bash | GNU ����Ʈ���� ���� linux�� �߿��� ��α�. Bourne Agine |
| | shell�̶� �θ���. |
+------------+-------------------------------------------------------------+
|rc | csh���� �� C�� ������. GNU ����Ʈ�� ���� |
+------------+-------------------------------------------------------------+
�� ��α���� ������ �����ϰ� © �� �ִ� ��������� ���ν� ������� �ʿ䰡
������ ��پ� �����ų �� �ִ� ���� ����� ������ ó���� ���� ó���� �䱸�ϴ�
�Ϳ� ������ϴٰ� �� �� �ִ�. �� ��α��� �ۼ����� �ΰ����ν� �ϳ��� ������
���� ������� �Է��Ͽ� ��ȭ����� �����ϴ� ��̸�, �ٸ� �ϳ��� ������ ����
��Ͽ� ������ ��� �����ϴ� ��̴�.
2. �� ���� - ����, ����
������ ȯ�溯���� ��ں���, �Ϲݺ����� ���еǸ� ������ ����ϱ� ���ؼ��� C�ʹ�
�� ó�� ������ִ� ��� �ƴ϶� ������ ó�� ����Ҷ� �����Ѵ�. ������ �⺻����
�� ���ڿ��� ����� �Ǹ� ������ ���� ������ $�� �տ� ����� echo������ ȣ����
�� �ȴ�.
(������ �ο�ó�� �ϱ��ؼ��� ��� ����ǥ(')�� $������ ���ų� $ �տ� \���ڸ� ��
����ν� $�� Ư���� �ǹ̸� ���ֹ��� �� �ִ�. ���� ������ ���� ����� ��ԵǸ�
�յڷ� ����ǥ�� ����־�� �ϸ�, ��ȣ(=) �յڿ� ���� ��� ���ƾ� �Ѵ�)
1) ȯ�溯��
���� �⵿�ϸ� �⺻������ ���õǴ� ������ν� �Ϲ������ ������� ������ ����
�ϱ� ���ؼ� �빮�ڷ� ǥ��ȴ�. �ٽ���� ������ ����� ����.
+----------+------------------------------------------------------------+
| ȯ�溯�� | ���� |
+----------+------------------------------------------------------------+
| $HOME | ���� Ȩ ���丮 |
+----------+------------------------------------------------------------+
| $PATH | ��ɾ� ã� ���Ǵ� �ݷ�(:)���� ���еǴ� ���丮 ��� |
+----------+------------------------------------------------------------+
| $IFS | �Է��ʵ� �����. ���� �Է�� �о��� �� ������ ���� |
| | �ϱ� ���� ����� ���Ǵ� ���� ���, ���� ����̽�, ��,|
| | ������ |
+----------+------------------------------------------------------------+
| $0 | �� ��ũ��Ʈ�� �̸� |
+----------+------------------------------------------------------------+
| $# | ���� ���� ���� |
+----------+------------------------------------------------------------+
| $$ | �� ��ũ��Ʈ�� ��μ��� ID. �� ��ũ��Ʈ ���ο��� �ߺ���� |
| | �ʴ� ���� �̸��� ���� �� ��� ���� |
+----------+------------------------------------------------------------+
2) ��ں���
�� ��ũ��Ʈ�� ��ڸ� �Ѱ�ٶ� �� ��ڵ鿡 ���� ������ ������ �ִ� ������ν�
���Ǵ� ��ڿ� ���õ� ������ ����� ����
+----------+------------------------------------------------------------+
| ��ں��� | ���� |
+----------+------------------------------------------------------------+
| $1, $2.. | ��ũ��Ʈ�� ���� ��� |
+----------+------------------------------------------------------------+
| $* | ��ũ��Ʈ�� ���� ��� ��ü��, �ϳ��� ������ ����Ǹ� IFS |
| | ȯ�� ������ ù ��° ���ڷ� ���еȴ�. |
+----------+------------------------------------------------------------+
| $@ | $*�� ���ϳ� IFS ȯ�� ������ ������ �ʴ´� |
+----------+------------------------------------------------------------+
3) �Ϲݺ���
�Ϲݺ����� Ư���� ����� ������ ��ҹ���� ���и� ��Ȯ� �ϸ� �ȴ�.
��) #!/bin/sh
echo "This Script Executable File : $0"
echo "Argument Count : $#"
echo "Process ID : $$"
echo "Argument List \$* : $*"
echo "Argument List \$@ : $@"
echo "Argument 1 : $1"
echo "Argument 2 : $2"
-->����
$chmod 755 argument (or chmod a+x argument)
$./test a1 a2
3. �� ���� - ���ǹ�,�����
1) test, []
�� ��ũ��Ʈ���� ���� ���� �Ǵ��� �� �ִ� ��� test�� []�� ���� ���� �ǹ̸�
���Ѵ�. []��ɾ�� �� ��ũ��Ʈ�� �ٸ� ��α��� ���� ���ϰ� ���̴� ����
�� �ϱ �Ѵ�.
a. ���ڿ� ��
+--------------------+--------------------------------------------------+
| ���ڿ� �� | �� |
+--------------------+--------------------------------------------------+
| string | string1�� �� ���ڿ��� �ƴϸ� � |
+--------------------+--------------------------------------------------+
| string1 = string2 | �� ���ڿ��� ���ٸ� � |
+--------------------+--------------------------------------------------+
| string1 != string2 | �� ���ڿ��� ���� �ʴٸ� � |
+--------------------+--------------------------------------------------+
| -n string | ���ڿ��� null�� �ƴ϶�� � |
+--------------------+--------------------------------------------------+
| -z string | ���ڿ��� null�̶�� � |
+--------------------+--------------------------------------------------+
b. �� ��
+-----------------------------+-----------------------------------------+
| ���� | �� |
+-----------------------------+-----------------------------------------+
| expression1 -eq expression2 | �� ǥ��� ���� ���ٸ� � |
+-----------------------------+-----------------------------------------+
| expression1 -ne expression2 | �� ǥ��� ���� ���� �ʴٸ� � |
+-----------------------------+-----------------------------------------+
| expression1 -qt expression2 | expression1�� expression2���� ũ�ٸ� � |
+-----------------------------+-----------------------------------------+
| expression1 -ge expression2 | expression1�� expression2���� ũ�ų� |
| | ���ٸ� �. |
+-----------------------------+-----------------------------------------+
| expression1 -lt expression2 | expression1�� expression2���� �۴ٸ� � |
+-----------------------------+-----------------------------------------+
| expression1 -le expression2 | expression1�� expression2���� �۰ų� |
| | ���ٸ� �. |
+-----------------------------+-----------------------------------------+
| ! expression | expression�� ����̶�� �, |
| | ��̶�� ��� |
+-----------------------------+-----------------------------------------+
c. ��� ����
+---------------+-------------------------------------------------------+
| ��� ���� | �� |
+---------------+-------------------------------------------------------+
| -d file | file�� ���丮��� � |
+---------------+-------------------------------------------------------+
| -e file | file�� �����Ѵٸ� � |
+---------------+-------------------------------------------------------+
| -f file | file�� �������̶�� � |
+---------------+-------------------------------------------------------+
| -g file | file�� set-group-id�� �����Ǿ� �ִٸ� � |
+---------------+-------------------------------------------------------+
| -r file | file�� �б� �����ϴٸ� � |
+---------------+-------------------------------------------------------+
| -s file | file�� ũ�Ⱑ 0�̶�� � |
+---------------+-------------------------------------------------------+
| -u file | file�� set-user-id�� �����Ǿ� ����� � |
+---------------+-------------------------------------------------------+
| -w file | file�� ���� �����ϴٸ� � |
+---------------+-------------------------------------------------------+
| -x file | file�� ���డ���ϴٸ� � |
+---------------+-------------------------------------------------------+
2) if
���� ���� ��Ʈ�Ͽ� ��������� ������ ������ �� �����Ų��.
a. ���1(��� if��)
------------------------------------------------------------------------
if [����]
then
����
fi
------------------------------------------------------------------------
b. ���2(if~else��)
------------------------------------------------------------------------
if [����]
then
����
else
����
fi
------------------------------------------------------------------------
c. ���3(�������� ���� �˻�)
------------------------------------------------------------------------
if [����]
then
����
elif [����]
then
����
else
����
fi
------------------------------------------------------------------------
3) for
������ ���� �ȿ��� ����� �����µ� ���ȴ�. ������ ���Ǵ� ���� ���
���ڿ��� ����̶� �����ϴ�. ����� �������� ��1, ��2�� ��ʷ� ����.
----------------------------------------------------------------------------
for ���� in ��1, ��2 ...
do
����
done
----------------------------------------------------------------------------
4) while
� ���� ������ Ƚ����ŭ �����Ű��� ������� for����� �����ϴ� �����
���� Ƚ���� ��Ȯ���Ҷ� ���ϴ�.
----------------------------------------------------------------------------
while ���ǹ�
do
����
done
----------------------------------------------------------------------------
5) until
while�� ���ϳ� ����� ��ϵ��� ����Ǵ� ��� �ƴ϶� ����� ���� ����ȴ�.
----------------------------------------------------------------------------
until ���ǹ�
do
����
done
----------------------------------------------------------------------------
6) select
���ϴ� ����Ʈ�� ����ϰ� ��� ���õ� ��� �����ش�. ����� ���� select�� ���
������ �ڵ������� ��� �� �����Ƿ� �ݵ�� break���� ����ؼ� ����� �Ѵ�.
��) #!/bin/sh
echo "What do you like?"
select var in "Linux" "Windows9x" "Win2k" "Win31" "Exit"
do
if [ "$var" = "Linux" ]; then
echo "You are a Linuxer!!!"
exit 0
elif [ "$var" = "Exit" ]; then
echo "The question ended"
exit 1
else
echo "$var is not the best way!!!"
echo "What do you like?"
fi
done
--> �� : What do you like?
1) Linux
2) Windows9x
3) Win2k
4) Win31
5) Exit
#?
7) case
���� ����� �˻��ϰ� �̿� ������ ������ ������� �ִ� ����� ������ ����
������ ����� ����ũ���� ���� ����Ѵ�.
---------------------------------------------------------------------------
case ���� in
���� [ | ���� ] ...) ���� ;;
���� [ | ���� ] ...) ���� ;;
....
esac
---------------------------------------------------------------------------
4. �� ���� - ���
���� ��ɾ �����Ҷ� ��� ���� ���� ���ؼ� ��� �ൿ�� ���Ǿ�� �� ���
AND�� OR����� ����ؼ� �ѹ��� ó���� �� �ִ�. �̰�� �� ��ũ��Ʈ �� �ƴ϶� ��
������� ��� �����ϴ�.
1) AND���
�ѹ��� ���� ���� �Ѳ��п� �����ų �� �ִ�. AND ��Ͽ��� ����� ���� ����
�ߴٸ� ����� ���� �����Ѵ�. ���� �������� ����Ͽ� �� ������ ��̶��
�� ��� ��ɾ ����ȴ�. ����� ��ȯ�Ǹ� ���̻� ��ɾ ������� �ʴ´�.
----------------------------------------------------------------------------
statment1 && statment2 && statment3 && ...
----------------------------------------------------------------------------
2) OR���
� �ϳ��� ���� ����� �����Ҷ��� �Ϸ�� ���� ��� �����Ѵ�. ���� ��
��� �����ϸ� �� �̻� ��� ��� �ٸ� ���� ������� �ʴ´�. ���� ��������
����Ͽ� �� ������ ����̶�� ������� ��ɾ ����ȴ�. ��� ���� ��ȯ�Ǹ�
���� �����.
----------------------------------------------------------------------------
statment1 || statement2 || statement3 || ...
----------------------------------------------------------------------------
3) AND�� OR�� ȥ��
�Ʒ��� ���� &&�� ||�� ȥ���Ͽ� ����Ҽ� �ִ�. ��̸� ����1�� ����ǰ� �����
�� ����2�� ����ȴ�
----------------------------------------------------------------------------
[ ���� ] && ����1 || ����2
----------------------------------------------------------------------------
��) [ -f file_one ] && command for true || command for false
5. �� ���� - �Լ�
�� ��ũ�� ���ο� �Ǵ� �ٸ� ��ũ��Ʈ ��Ͽ� �Լ��� ������ ���� ����� �� �ִ�.
�Լ��� ����ϸ� �ڵ带 ���ȭ �� �� �ְ� �ڵ��� ���������, ����� �����ϴ�.
���� �ٸ� ��ũ��Ʈ ���� ȣ���ؼ� �Լ��� ������ ���, ����� ����� ��ũ��Ʈ
�� ����ð��� ����� �Լ��� ���� �����ϴ� ��� ��ٷӱ� ������ ������̸�
�ܺ� ���� �Լ��� ������ �ʴ� ��� ����.
-----------------------------------------------------------------------------
function_name () {
statments
}
-----------------------------------------------------------------------------
�Լ��� ����ϴ� ���� �Լ���(function_name) �ڿ� ��ڸ� ����ش�. ���� �Լ�
���ο��� ����(local)������ ����ϰ�� �� ��쿡�� ������ �տ� local�̶� ����
����ָ� �ȴ�.
6. �� ���� - ��ɾ�
�� ��ũ��Ʈ �ȿ��� ����� �� �ִ� ���� �� �����ν� �ϳ��� ���θ�ɾ�̰� �ٸ�
�ϳ��� ����Ʈ �� �����ų �� �ִ� �Ϲݸ�ɾ�̴�. ���θ��� �ܺθ�ɺ���
�� �� ȿ�����̶�� ��� �����ϸ� ������� ��̴� ����.
1) break
for, while�̳� until���� ���� ����� ������ ���� ����� ��� �� ����Ѵ�.
��) while true
do
if [ -f file_name ]; then
break
fi
echo "file_name was not found"
done
exit 0
2) : ���
�ǹ̾���(null) ������ true�� ����� ������ �����ϰ� �ϱ� ���ؼ� ����Ѵ�.
while true�� ���� ��� while :�� ���� ��ü�� �� �����, ������ ����� ������
�� �ִ�. ��) : $(var=value)
3) continue
C������ ���ϸ� for, while �Ǵ� until ����� ������ ��� ���� ������ �ϰ�
��� �� ����� ���� �Ѵ�.
4) .���
���� ������ ���� �������.
5) echo
����ڸ� ����� ���� �����ϰ� ����Ѵ�. (����ٷ� �Ѿ��)
6) eval
���� ��� ���� ���ϴµ� ����Ѵ�.
��) foo=10
x=foo
y='$'$x
echo $y ==> ���� : $foo
foo=10
x=foo
eval y='$'$x
echo $y ==> ���� : 10
7) exec
��� ���� �ٸ� ��α����� ��ü�ϰų� ���� �б� ���� ���� ����Ѵ�.
8) exit n
��� ���� �����Ѵ�. ����� n���� �����Ѵ�.
+----------+--------------------------------+
| �����ڵ� | ���� |
+----------+--------------------------------+
| 0 | ���� |
+----------+--------------------------------+
| 1~125 | ��ũ��Ʈ���� ����ϴ� �����ڵ� |
+----------+--------------------------------+
| 126 | ���� ������ �ʾҴ� |
+----------+--------------------------------+
| 127 | �ش� ���� �߰��� �� ���� |
+----------+--------------------------------+
| 128�̻� | �ñ׳�� ���ߴ�. |
+----------+--------------------------------+
9) export
�ش� ������ �Ļ��� �ڽ� ��μ������� export�� ȯ�溯���� ���� ������ �����Ѵ�
10) expr
ǥ���� ���� ���ϴ� ����� x='expr $x+1'�� ���� ���ȴ�. ����� expr���ٴ�
$((����))�� ����Ѵ�.
11) printf
C������ printf�� ����ϴ�.
��� : printf "Format String" arg1 arg2 arg3 ...
+-------------+----------------------------------------+
| Ż�� ���ڿ� | ���� |
+-------------+----------------------------------------+
| \\ | �齽����(\) ���� |
+-------------+----------------------------------------+
| \a | ���(�� �Ǵ� �����) |
+-------------+----------------------------------------+
| \b | �齺��̽� ���� |
+-------------+----------------------------------------+
| \f | ��ǵ� ���� |
+-------------+----------------------------------------+
| \n | �����(newline) ���� |
+-------------+----------------------------------------+
| \r | ij���� ���� ���� |
+-------------+----------------------------------------+
| \t | �� ���� |
+-------------+----------------------------------------+
| \v | ���� �� ���� |
+-------------+----------------------------------------+
| \ooo | 8������ ǥ��Ǵ� �� ���� |
+-------------+----------------------------------------+
+-------------+----------------------------------------+
| ��ȯ �ĺ��� | ���� |
+-------------+----------------------------------------+
| d | ����� ��� |
+-------------+----------------------------------------+
| c | �� ���� ��� |
+-------------+----------------------------------------+
| s | ���� ��� |
+-------------+----------------------------------------+
| % | %���� ��� |
+-------------+----------------------------------------+
��) [lpay09@pay lpay09]$ printf "%s\n" hello
12) return
�� �Լ����� ���� ��ȯ�Ҷ� ���δ�.
13) set
�� ���ο��� �Ű� ��ڸ� �����Ѵ�. set�� ��ڷ� ���� ���ڿ��� ���鿡 ���� $1
���� ��ʴ�� ��Եȴ�.
��) #!/bin/sh
echo the eate is $(date)
set $(date)
echo The month is $2
exit 0
--->�� : the date is �� 2�� 11 02:21:57 KST 2001
The month is 2��
14) shift
���� ��ڸ� ��ڸ��� �Ʒ���(n->1��) �̵���Ų��.
��) #!/bin/sh
echo $1
shift
echo $1
shift 5
echo $1
���� : [lpay09@pay lpay09]$ ./shift 1 2 3 4 5 6 7 8 9 10
1
2
7
15) trap
���� ���൵�� �ñ׳�� ó���ϴ� �ñ׳� ó���⸦ ����� ����� �Ѵ�.
-----------------------------------------------------------------------------
trap command signal
-----------------------------------------------------------------------------
�� ��ũ��Ʈ�� ������ �Ʒ��� ����ǹǷ� ��ȣ�Ϸ��� �κ� ����� trap���� ���
�Ѵ�. trap����� �⺻���� ����Ϸ��� ��ɿ� - �� ����� �ȴ�. ��ȣ�� �����ҷ�
�� ''�� ���ڿ��� �ش�.
��) trap 'rm -rf /tmp/my_tmp_file_$$' INT
(INT�� ���� Ctrl+C�� �������� ���ȴ�)
16) unset
������ �Լ��� �����Ѵ�.
��) #!/bin/sh
foo="Hello world"
echo $foo
unset foo
echo $foo
7. �� ���� - ��ɽ���
1) �ܺ� ���� ���� ���� ������ ����־� ������ ������ ����Ϸ��� $(command)
������ �̿��ϸ� �ȴ�. �Ǵ� `command`�� ��ĵ� ��ȿ�ϴ�.
----------------------------------------------------------------------------
����=$(command)
-----------------------------------------------------------------------------
��) user=$(who)
2) �� Ȯ��
���� �־ ������ ��� ���ڿ��� ����� �DZ� ������ ���� �ҷ��� ����� ����
�ణ ������ ���ľ� �ȴ�.
----------------------------------------------------------------------------
���� = $((����))
----------------------------------------------------------------------------
���ij����� ����($1,$�� ����)�� �� �� ����� ���ڰ� �ƴ� ���ڿ�, �Ǵ�
���ڿ��� ���ִ� ������ ���� �װ͵� ��꿡�� ���ܵȴ�.
(0���� ���ֵǾ� ����� �̷���� �ʴ´�)
��) count=$(($count+1))
3) �Ű����� Ȯ��
�Ű����� Ȯ��̶� ������ ���� ���ڿ� ���� ��ü�ϴ� ����� �ܼ��� ��ü�Ӹ�
�ƴ϶� ���� ���� ���ڿ��� �����Ͽ� ���ϴ� ���ڿ��� ������ ���� �ִ�.
+------------------+--------------------------------------------------------+
| �Ű����� Ȯ�� | ���� |
+------------------+--------------------------------------------------------+
| ${param -default}| param�� ������� ����� default�� ��ü |
+------------------+--------------------------------------------------------+
| ${#param} | param�� ��̸� ����Ѵ�(�����´�) |
+------------------+--------------------------------------------------------+
| ${param%word} | ���������� word�� ��ġ�ϴ� parm�� �ּҺκ�(ù��° ��ġ)|
| | �� �����ϰ� ������� ��ȯ�Ѵ�. |
+------------------+--------------------------------------------------------+
| ${param%%word} | ���������� word�� ��ġ�ϴ� parm�� �ִ�κ�(������ ��ġ)|
| | �� �����ϰ� ������� ��ȯ�Ѵ�. |
+------------------+--------------------------------------------------------+
| ${param#word} | ó������ word�� ��ġ�ϴ� parm�� �ּҺκ�(ù��° ��ġ) |
| | �� �����ϰ� ������� ��ȯ�Ѵ�. |
+------------------+--------------------------------------------------------+
| ${param##word} | ó������ word�� ��ġ�ϴ� parm�� �ִ�κ�(������ ��ġ)��|
| | �����ϰ� ������� ��ȯ�Ѵ�. |
+------------------+--------------------------------------------------------+
��) #!/bin/sh
unset foo
echo ${foo:-bar}
foo=fud
echo ${foo:-bar}
foo=/usr/bin/X11/startx
echo ${foo#*/}
echo ${foo##*/}
foo=/usr/local/etc/local/networks
echo ${foo%local*}
echo ${foo%%local*}
exit 0
--> �� : bar
fud
usr/bin/X11/startx
startx
/usr/local/etc
/usr
8. �� ���� - �� ��ũ��Ʈ ���ο��� �Է� ���� (Here Documents)
�� ���ο��� ��ɾ �Է�� �����ϴ� ������ ���� �Է�� ��ġ Ű���忡�� �Է�
�� �Ͱ� ���� �ൿ�Ѵ�.
-------------------------------------------------------------------------------
��� << ���Ṯ�ڿ�
�Է°�
���Ṯ�ڿ�
-------------------------------------------------------------------------------
��) #!/bin/sh
cat << !funy!
Are funy?
I'm a boy.
!funy!
�ٸ� �����δ� ed�����Ϳ� ������� ���� ������ �� �ִ�.
��) #!/bin/sh
ed text << !funy!
%s/boy/girl
w
q
!funy!
text���� ���� : I'm a boy ---> ������ I'm a girl
9. �� ���� - ���
�� ��ũ��Ʋ�� ����ϴ� ��� �ſ� ������ ����� �ɸ��� Ư���� ������ ����.
���� echo���� ����Ͽ� ������ ���� ���캸�ų�, ������ �ִ� �κ�� ��������
������ ��ȣ ��ȭ����� �Է��ϴ� ��Ʈ�� �� �� �ִ�.
���� ����� �ɼ�� ����ϰų� set���� ����Ͽ� ������ ����� �� �ִ�.
+----------------+----------------+--------------------------------------------+
| ����� �ɼ� | set �ɼ� | ���� |
+----------------+----------------+--------------------------------------------+
| sh -n <script> | set -o noexec | ���� ������ üũ�Ѵ�. |
| | set -n | ���� �����Ű�� �ʴ´� |
+----------------+----------------+--------------------------------------------+
| sh -v <script> | set -o verbose | ���� �����ϱ� ���� ���� �����Ѵ�. |
| | set -v | |
+----------------+----------------+--------------------------------------------+
| sh -x <script> | set -o xtrace | ������ ���� ó���� ��� �����Ѵ�. |
| | set -x | |
| | set -o nounset | ���ǵ�� ��� ������ ������ �� ������ |
| | set -u | ����Ѵ�. |
+----------------+----------------+--------------------------------------------+
(set�� �ɼ� �÷��� �Ӷ��� -o��, ������ +o�� ����Ѵ�.)
������ �Ʒ��� ���� ���� ��ũ��Ʈ�� ó���� ������ ��� EXIT �ñ׳�� ��
�ؼ� �����ߴ�� Ȯ���� �� �ִ�.
-------------------------------------------------------------------------------
trap 'echo Exiting: critical variable = $critical variable' EXIT
-------------------------------------------------------------------------------
10. �츮�� ��� ���� �� ��ũ��Ʈ (httpd)
1 : #!/bin/sh
2 : #
3 : # Startup script for the Apache Web Server
4 : #
5 : # chkconfig: 345 85 15
6 : # description: Apache is a World Wide Web server. It is used to server \
7 : # HTML file and CGI
8 : # processname: httpd
9 : # pidfile: /var/run/httpd.pid
10 : # config: /etc/httpd/conf/httpd.conf
11 :
12 : # Source function library.
13 : . /etc/rc.d/init.d/functions
14 :
15 : # See how we were called
16 : case "$1" in
17 : start)
18 : echo -n "Starting httpd : "
19 : daemon httpd
20 : echo
21 : touch /var/lock/subsys/httpd
22 : ;;
23 : stop)
24 : echo -n "Shutting down http : "
25 : killproc httpd
26 : echo
27 : rm -f /var/lock/subsys/httpd
28 : rm -f /var/run/httpd.pid
29 : ;;
30 : status)
31 : status httpd
32 : ;;
33 : restart)
34 : $0 stop
35 : $0 start
36 : ;;
37 : reload)
38 : echo -n "Reloading httpd : "
39 : killproc httpd -HUP
40 : echo
41 : ;;
42 : *)
43 : echo "Usage : $0 {start|stop|restart|reload|status}"
44 : exit 1
45 : esac
46 :
47 : exit 0
1 : #!/bin/sh
2 : #
3 : # functions This file contains functions to be used by most or all
4 : # shell scripts in the /etc/init.d directory.
5 : #
6 : # Version: @(#) /etc/init.d/functions 1.01 26-Oct-1993
7 : #
8 : # Author: Miquel van Smoorenburg, <miquels
at drinkel.nl.mugnet.org>
9 : # Hacked by: Greg Galloway and Marc Ewing
10 : #
11 :
12 : # First set up a default search path.
13 : export PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin"
14 :
15 : # Get a sane screen width
16 : [ -z "$COLUMNS" ] && COLUMNS=80
17 :
18 : # Read in our configuration
19 : if [ -z "$BOOTUP" ]; then
20 : if [ -f /etc/sysconfig/init ]; then
21 : . /etc/sysconfig/init
22 : else
23 : # This all seem confusing? Look in /etc/sysconfig/init,
24 : # or in /usr/doc/initscripts-*/sysconfig.txt
25 : BOOTUP=color
26 : RES_COL=60
27 : MOVE_TO_COL="echo -en \\033[${RES_COL}G"
28 : SETCOLOR_SUCCESS="echo -en \\033[1;32m"
29 : SETCOLOR_FAILURE="echo -en \\033[1;31m"
30 : SETCOLOR_WARNING="echo -en \\033[1;33m"
31 : SETCOLOR_NORMAL="echo -en \\033[0;39m"
32 : LOGLEVEL=1
33 : fi
34 : if [ -x /sbin/consoletype ]; then
35 : if [ "`consoletype`" = "serial" ]; then
36 : BOOTUP=serial
37 : MOVE_TO_COL=
38 : SETCOLOR_SUCCESS=
39 : SETCOLOR_FAILURE=
40 : SETCOLOR_WARNING=
41 : SETCOLOR_NORMAL=
42 : fi
43 : fi
44 : fi
45 :
46 : if [ "$BOOTUP" != "verbose" ]; then
47 : INITLOG_ARGS="-q"
48 : else
49 : INITLOG_ARGS=
50 : fi
51 :
52 : # A function to start a program.
53 : daemon() {
54 : # Test syntax.
55 : gotbase=
56 : user=
57 : nicelevel=0
58 : while [ "$1" != "${1##-}" -o "$1" != "${1##+}" ]; do
59 : case $1 in
60 : '') echo '$0: Usage: daemon [+/-nicelevel] {program}'
61 : return 1;;
62 : --check)
63 : shift
64 : base=$1
65 : gotbase="yes"
66 : shift
67 : ;;
68 : --user)
69 : shift
70 : daemon_user=$1
71 : shift
72 : ;;
73 : -*|+*) nicelevel=$1
74 : shift
75 : ;;
76 : *) nicelevel=0
77 : ;;
78 : esac
79 : done
80 :
81 : # Save basename.
82 : [ -z $gotbase ] && base=`basename $1`
83 :
84 : # See if it's already running.
85 : pidlist=`pidofproc $base`
86 :
87 : pid=
88 : for apid in $pidlist ; do
89 : [ -d /proc/$apid ] && pid="$pid $apid"
90 : done
91 :
92 : [ -n "$pid" ] && ps h $pid >/dev/null 2>&1 && return
93 :
94 : # make sure it doesn't core dump anywhere; while this could mask
95 : # problems with the daemon, it also closes some security problems
96 : ulimit -c 0
97 :
98 : # Echo daemon
99 : [ "$BOOTUP" = "verbose" ] && echo -n " $base"
100 :
101 : # And start it up.
102 : if [ -z "$daemon_user" ]; then
103 : nice -n $nicelevel initlog $INITLOG_ARGS -c "$*" && success "$base
startup" || failure "$base startup"
104 : else
105 : nice -n $nicelevel initlog $INITLOG_ARGS -c "su $daemon_user -c
\"$*\"" && success "$base startup" || failure "$base startup"
106 : fi
107 : }
108 :
109 : # A function to stop a program.
110 : killproc() {
111 : RC=0
112 : # Test syntax.
113 : if [ $# = 0 ]; then
114 : echo "Usage: killproc {program} [signal]"
115 : return 1
116 : fi
117 :
118 : notset=0
119 : # check for second arg to be kill level
120 : if [ "$2" != "" ] ; then
121 : killlevel=$2
122 : else
123 : notset=1
124 : killlevel="-9"
125 : fi
126 :
127 : # Save basename.
128 : base=`basename $1`
129 :
130 : # Find pid.
131 : pidlist=`pidofproc $base`
132 : &