0. Minishell subject!
๊ธฐ๋ณธ ๋ณด๊ธฐ
Search
โข
์๋ซํ ' ์ด๋ " ๋ ํฒ ํ์ ์์, \ ์ด๋ ; . ๊ฐ ์์ผ๋ฉด ์๋จ
โข
์ ์ญ ๋ณ์ 1๊ฐ ๋ง ๊ฐ๋ฅ
โข
์๋ก์ด ์ปค๋งจ๋๋ฅผ ์
๋ ฅ ๋ฐ์๋๋ prompt ๋ฅผ ์ถ๋ ฅํด์ผ๋
โข
History๊ฐ ๋ผ์ผํจ
โข
PATH ๋ณ์๋, ์๋ ๊ฒฝ๋ก, ์ ๋ ๊ฒฝ๋ก ์ ์๋๊ฑธ ๊ธฐ๋ฐ์ผ๋ก ์ฌ๋ฐ๋ฅด๊ฒ ๊ฒ์ํ๊ณ ์คํํด์ผํ๋ค.
โข
๊ตฌํํด์ผํ builtin
โฆ
echo (n ์ต์
)
โฆ
cd (์ ๋, ์๋ ๊ฒฝ๋ก๋ง)
โฆ
pwd
โฆ
export
โฆ
unset
โฆ
env
โฆ
exit
โข
๋ฆฌ๋ค์ด๋ ์
๋์ผํจ
โข
ํ์ดํ ์์ด์ผํจ
โข
ํ๊ฒฝ ๋ณ์๊ฐ ์๋ํด์ผํจ
โข
$? ๋ ๊ฐ์ฅ ์ต๊ทผ ์คํ๋ ํ์ดํ๋ผ์ธ์ ์ข
๋ฃ ๊ฐ์ด ๋ค์ด๊ฐ์ผํจ
โข
ctrl-C, ctrl-D, ctrl-\ ์๋ํด์ผ๋
โฆ
ctrl-C : prompt ์ถ๋ ฅ
โฆ
ctrl-D : ์ ์ข
๋ฃ
โฆ
ctrl-\ : ์๋ฌด๊ฒ๋ ์ํจ
์คํ๋ถ
โข
builtin ํจ์๋ฅผ ์ฐ์ ๊ตฌํํด์ผ ํ๋ค.(mantatory์ ์๋ ํจ์๋ค!)
ํ์ฑ
โข
; ์์
โข
ํ์ฑํด์ ๋ช
๋ น์ด์ ์คํ๋ฌธ์ ๋๊ฒจ์ฃผ์ด์ผํ๋ค.
1. ํจ์ ์ ๋ฆฌํ๊ธฐ!
์ด๋ฏธ ์๋ ํจ์
โข
printf, malloc, free, write, open, read, close, exit
fork
function
forkํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ์๋ก์ด ํ๋ก์ธ์ค๋ฅผ ํ๋ ํธ์ถํ๋ค.
์ด๋, ํธ์ถํ๋ ์์ ํ๋ก์ธ์ค๋ ๋ถ๋ชจ ํ๋ก์ธ์ค์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ทธ๋๋ก ๋ณต์ฌํ์ฌ ๊ฐ์ง๋ค.
๊ทธ๋ฆฌ๊ณ forkํจ์ ํธ์ถ ์ดํ ๊ฐ์์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ ์คํ๋๋ค.
#include <unistd.h> //header file
pid_t fork(void);
C
๋ณต์ฌ
return value
โข
์ฑ๊ณต ์ : ๋ถ๋ชจ ํ๋ก์ธ์ค์์๋ ์์ ํ๋ก์ธ์ค์ PID๊ฐ, ์์ ํ๋ก์ธ์ค์์๋ 0
โข
์คํจ ์ : -1
example
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int x = 0;
fork();
x = 1;
printf("PID : %d, x : %d\n", getpid(), x);
return (0);
}
C
๋ณต์ฌ
$ ./a.out
PID : 8270, x : 1
PID : 8271, x : 1
Shell
๋ณต์ฌ
๊ฒฐ๊ตญ x ์ ๊ฐ์ ๋์
ํ ๋, fork() ๋ช
๋ น ์ด์ ์ ๋์
ํ๋ ์ดํ์ ๋์
ํ๋ ์๊ด์ด ์๋ค!
fork๋ฅผ ํ๋ฉด ์์ ํ๋ก์ธ์ค๊ฐ ๋ถ๋ชจ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ทธ๋๋ก ๋ณต์ฌํด์ ๊ฐ์ง๊ธฐ ๋๋ฌธ.
์ดํ ๊ฐ์ ์์
์ ๊ฐ ํ๋ก์ธ์ค๊ฐ ๊ฐ๋ณ์ ์ผ๋ก ์คํํ๋ค.
๋ํ ์์ ํ๋ก์ธ์ค๋ fork()ํจ์๋ฅผ ์คํํ์ง ์๊ธฐ ๋๋ฌธ์ ์ด๋ getpid()์ return value๋ 0์ด๋ค. ๋ฐ๋ผ์ getpid()์ return value๋ฅผ ๊ธฐ์ค์ผ๋ก ์์์ธ์ง ๋ถ๋ชจ์ธ์ง ์ ์ ์๋ค.
#include <stdio.h>
#include <unistd.h>
int main(void)
{
pid_t pid;
pid = fork();
if(pid > 0) // ๋ถ๋ชจ ์ฝ๋
printf("๋ถ๋ชจ ํ๋ก์ธ์ค ์
๋๋ค.");
else // ์์ ์ฝ๋
printf("์์ ํ๋ก์ธ์ค ์
๋๋ค.");
return (0);
}
C
๋ณต์ฌ
wait
function
1.
์์ ํ๋ก์ธ์ค๊ฐ ๋์ ์ค์ด๋ฉด ์ํ๋ฅผ ์ป์ด์ฌ ๋๊น์ง ๋๊ธฐ
2.
wait() ํจ์ ํธ์ถ์๊ฐ ์๊ทธ๋์ย ๋ฐ์ย ๋๊น์ง ๋๊ธฐ
3.
์์ ํ๋ก์ธ์ค๊ฐ ์ข
๋ฃ๋ ์ํ๋ผ๋ฉด ์ฆ์ ํธ์ถ์ด ๋ฐํ๋์ด ์ํ๋ฅผ ์ป์, ์ด ๋ wait() ํจ์๋ ์์ ํ๋ก์ธ์ค์ ํ๋ก์ธ์ค ID๋ฅผ ๋ฐํ
4.
์์ ํ๋ก์ธ์ค๊ฐ ์๋ค๋ฉด ํธ์ถ์ด ์ฆ์ ๋ฐํ๋๋ฉฐ, ์๋ฌ๊ฐ์ ๋ฐํ
#include <sys/wait.h>
pid_t wait(int *statloc);
C
๋ณต์ฌ
wait()์ ์ธ์ status ๋ฅผ ํตํ์ฌ ์์ ํ๋ก์ธ์ค์ ์ํ๋ฅผ ๋ฐ์์ฌ ์ ์๋๋ฐ,
์์ํ๋ก์ธ์ค์ ์ํ๊ฐ์ ์์ํ๋ก์ธ์ค์ ์ข
๋ฃ๊ฐ *ย 256(FF) ์ด๋ค.
return value
โข
์ฑ๊ณต ์ : PID
โข
์คํจ ์ : -1
example
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pidi;
int status,i;
pid = fork();
if (pid > 0)
{
pid_t waitpid;
printf("๋ถ๋ชจ PID : %d\n", getpid());
waitpid = wait(&status); // ์์ ํ๋ก์ธ์ค์ ์ํ๋ฅผ ๋ฐ์์ฌ ๋๊น์ง wait!
if (waitpid == -1)
printf("error\n");
else
{
if (WIFEXITED(status))
printf("์์ ํ๋ก์ธ์ค ์ ์ ์ข
๋ฃ\n");
else if (WIFSIGNALED(status))
printf("์์ ํ๋ก์ธ์ค ๋น์ ์ ์ข
๋ฃ: %d\n", WTERMSIG(status));
}
}
else if(pid == 0){ // ์์ ํ๋ก์ธ์ค
sleep(6);
printf("์์ PID : %ld \n",(long)getpid());
}
else { // fork ์คํจ
perror("fork Fail! \n");
return (-1);
}
return (0);
}
C
๋ณต์ฌ
$ ./a.out
๋ถ๋ชจ PID : 10695
์์ PID : 10696
์์ ํ๋ก์ธ์ค ์ ์ ์ข
๋ฃ
Shell
๋ณต์ฌ
wait ๋ช
๋ น์ ์ฌ์ฉํ ๋ ์์น์ ์ฃผ์ํ์ฌ์ผ ํ๋ค!
๋ง์ฝ ์ ์ฝ๋์์ ๋ถ๋ชจ pid๋ฅผ ์ถ๋ ฅํ๋ ๋ถ๋ถ์ด wait๋ช
๋ น์ด ์๋๋ก ๋ด๋ ค๊ฐ๋ค๋ฉด, ๋ถ๋ชจ pid์ถ๋ ฅ์ด ์์ํ๋ก์ธ์ค๊ฐ ์ข
๋ฃ๋ ๋๊น์ง ๊ธฐ๋ค๋ ธ๋ค๊ฐ ์ถ๋ ฅํ๊ฒ ๋๋ค.
wait()๊ด๋ จ ํจ์์์ ์์ ํ๋ก์ธ์ค์ ์ข
๋ฃ ์ํ๋ฅผ ํ์ธํ๊ธฐ ์ํด์ ์ธ์๋ก ์ ๋ฌํ๋ ๋ณ์ status ๊ฐ์ ์๋์ ๊ฐ์ด ๊ตฌ์ฑ๋์ด ์๋ค! ๊ธฐ์กด์๋ 255๋ฅผ ๊ธฐ์ค์ผ๋ก ๋น๊ต ํ์์ง๋ง ์๋ ๋งคํฌ๋ก๋ฅผ ํ์ฉํด์ ๊ฐ๋จํ๊ฒ ํ์ธํ ์ ์๋ค.
๊ธฐ๋ณธ ๋ณด๊ธฐ
Search
WIF๋ก ์์ํ๋ macro๋ ์์ ๋ฐ์ดํธ์ ์ํ๋ฅผ ํ๋จํ๋ ํจ์์ด๋ฉฐ ๋๋จธ์ง macroํจ์๋ ์์ธ ๊ฐ์ ์ฝ์ ์ ์์ต๋๋ค.
์ด์ ์์ ํ๋ก์ธ์ค๋ฅผ ์ค๋ ์๊ฐ๋์ ์คํ์ํค๊ณ ํฐ๋ฏธ๋์์ kill๋ช
๋ น์ผ๋ก ์์ ํ๋ก์ธ์ค๋ฅผ ์ข
๋ฃ์์ผ๋ณด์!
$ kill -9 13439
๋ถ๋ชจ PID : 13438, ์์ PID : 13439
์์ ํ๋ก์ธ์ค ๋น์ ์ ์ข
๋ฃ: 9
Shell
๋ณต์ฌ
waitpid
function
waitpid()๋ wait()ํจ์์ ๋์ผํ๊ฒ ์์ ํ๋ก์ธ์ค๋ฅผ ๊ธฐ๋ค๋ฆด ๋ ์ฌ์ฉํ๋ค. ํ์ง๋ง ์์ ํ๋ก์ธ์ค๊ฐ ์ข
๋ฃ๋ ๋๊น์ง ์ฐจ๋จ๋๋ ๊ฒ์ ์ํ์ง ์์ ๊ฒฝ์ฐ, ์ต์
์ ์ฌ์ฉํ์ฌ ์ฐจ๋จ์ ๋ฐฉ์งํ ์ ์๋ค.
#include <sys/wait.h> //header file
pid_t waitpid(pid_t pid, int *status, int options);
C
๋ณต์ฌ
โข
pid : wait()ํ ์์ ํ๋ก์ธ์ค์ ์ ํ
โฆ
pid < -1 : ๊ทธ๋ฃนID๊ฐ ์ ๋๊ฐ๊ณผ ๊ฐ์ ์์ ํ๋ก์ธ์ค๋ฅผ ๊ธฐ๋ค๋ฆฐ๋ค
โฆ
pid == -1 : ์๋ฌด ์์ ํ๋ก์ธ์ค ID๋ผ๋ ๊ธฐ๋ค๋ฆฐ๋ค
โฆ
pid == 0 : ์์ ๊ณผ ๊ฐ์ ํ๋ก์ธ์ค ๊ทธ๋ฃน ID๋ฅผ ๊ฐ์ง ์์ ํ๋ก์ธ์ค๋ฅผ ๊ธฐ๋ค๋ฆฐ๋ค.
โฆ
pid > 0 : ์ธ์๋ก ๋์ด์จ pid๋ฅผ ๊ฐ์ง ์์ ํ๋ก์ธ์ค๋ง ๊ธฐ๋ค๋ฆฐ๋ค.
โข
status : wait()ํจ์์ ๋์ผ
โข
options : 0 ํน์ ์์์ ์กฐํฉ์ผ๋ก ์ค์ ๋๋ค
โฆ
0 : ๊ฒฐ๊ณผ๋ฅผ return ํ ๋๊น์ง blockํ๋ค
โฆ
WNOHANG : ํ์ฌ ์ข
๋ฃ๋ ์์ ํ๋ก์ธ์ค๊ฐ ์์ผ๋ฉด blockํ์ง ์๊ณ ๋ฐ๋ก 0์ ๋ฐํํ๋ค
โฆ
WUNTRACED : ์์ ํ๋ก์ธ์ค๊ฐ STOPํ๋ฉด ๋ฐํํ๋ค
โฆ
WCONTINUED : STOP๋์๋ ์์ ํ๋ก์ธ์ค๊ฐ ์ฌ ์คํ๋๋ฉด ๋ฐํํ๋ค
return value
โข
์ฑ๊ณต ์ : PID โ ์ํ๊ฐ ๋ณ๊ฒฝ๋ ์์ ํ๋ก์ธ์ค ID (status๊ฐ ์ํ๊ฐ ์ ์ฅ)
โข
์ฑ๊ณต ์ : 0 โ option์ด WNUHANG์ธ ๊ฒฝ์ฐ ์ข
๋ฃ๋ ์์ ํ๋ก์ธ์ค๊ฐ ์์ผ๋ฉด 0์ return
โข
์คํจ ์ : -1 โ wait ์ค ์ค๋ฅ๊ฐ ๋ฐ์, ์ค๋ฅ ๋ด์ฉ์ errno ์ ์ญ๋ณ์์ ์ค์
example
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid;
int status,i;
pid = fork();
if (pid > 0)
{
pid_t waitpid;
printf("๋ถ๋ชจ PID : %d, ์์ PID : %d\n", getpid(), pid);
}
else if(pid == 0){ // ์์ ํ๋ก์ธ์ค
for (int i = 0 ; i < 3 ; ++i)
sleep(1);
printf("์์ PID : %ld \n",(long)getpid());
printf("์์ ์ข
๋ฃ\n");
}
else { // fork ์คํจ
perror("fork Fail! \n");
return -1;
}
pid = waitpid(pid, &status, 0);
if (pid == -1 )
{
printf("error\n");
}
else
{
if (WIFEXITED(status)){
printf("ํ๋ก๊ทธ๋จ์์ exited(%d) ๋๋ main์์ return ;ํ์ฌ ์ข
๋ฃ๋์์ต๋๋ค.\n",
WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
printf("siganl %d๋ฒ์ด ๋ฐ์ํ์ฌ ์ข
๋ฃ๋์์ต๋๋ค.\n", WTERMSIG(status));
} else if (WIFSTOPPED(status)) {
printf("signal %d๋ฒ์ผ๋ก ์ธํ์ฌ stop๋์์ต๋๋ค. \n", WSTOPSIG(status));
} else if (WIFCONTINUED(status)) {
printf("stop๋ ํ๋ก์ธ์ค๋ฅผ ์งํํฉ๋๋ค\n");
}
}
return 0;
}
C
๋ณต์ฌ
wait3, wait4
function
wait3, wait4 ํจ์๋ ์ฌ์ฉ์ ์๊ฐ ์ ๋ณด๋ฅผ ํ์ธํ ์ ์๋ค.
#include <sys/types.h> //header file
#include <sys/wait.h>
pid_t wait3(int *staloc, int options, struct rusage *rus);
pid_t wait4(pid_t pid, int *staloc, int options, struct rusage *rus);
C
๋ณต์ฌ
parameters
โข
staloc, options, pid : ์์ ๋์ผ
โข
rusage : ์์ ํ๋ก์ธ์ค์ ๋ฆฌ์์ค ์ฌ์ฉ๋์ ๋ํ ์ ๋ณด๊ฐ ๋ด๊ธด๋ค. (struct rusage ํํ๋ก ๋ด๊ธด๋ค)
return value
โข
์ฑ๊ณต ์ : PID, ๋ง์ฝ ํจ์๊ฐ WNOHANG ์ต์
์ผ๋ก ์คํ๋์๊ณ , ์์ ํ๋ก์ธ์ค๊ฐ ์์ง ์ข
๋ฃ๋์ง ์์์ ๋, 0
โข
์คํจ ์ : -1
example
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/resource.h>
void ViewRUsage(pid_t pid, struct rusage *pru);
int main()
{
pid_t cpid = fork();
if(cpid == -1)
{
perror("error fork");
return 0;
}
if(cpid>0)
{
printf("<parent> fork child pid:%u\n",cpid);
int rvalue = 0;
struct rusage ru;
pid_t wpid = wait3(&rvalue,0, &ru);
ViewRUsage(wpid, &ru);
}
else
{
printf("<child> pid:%d \n",getpid());
int i=0,j=0;
for(i=0;i<100000;i++)
{
fprintf(stderr,".");
}
}
return 0;
}
void ViewRUsage(pid_t pid, struct rusage *pru)
{
printf("\n=== pid rusage info ===\n");
struct timeval *tv = &(pru->ru_utime);
printf("user CPU time used [%ld]sec [%d]usec\n", tv->tv_sec,tv->tv_usec );
tv = &(pru->ru_stime);
printf("system CPU time used [%ld]sec [%d]usec\n", tv->tv_sec,tv->tv_usec );
}
C
๋ณต์ฌ
signal
ํ๋ก์ธ์ค๊ฐ ์๊ทธ๋์ ๋ฐ๊ฒ ๋๋ฉด
1. ์๊ทธ๋์ ํด๋น๋๋ ๊ธฐ๋ณธ ๋์์ ํ๊ฑฐ๋
2. ๊ทธ ์๊ทธ๋์ ๋ฌด์ํ๊ฑฐ๋
3. ์ฌ์ฉ์๊ฐ ์ ์ํ ํจ์๋ฅผ ํตํด ๋์ ๋ฐฉ์์ ๋ฐ๊ฟ ์ ์๋ค.
function
์์ ๊ฐ์ ์๊ทธ๋์ ์ ์ดํ ์ ์๋ ํจ์. signal๋ฒํธ๋ฅผ ๋ฐ์์ ํด๋น signal์์์ ํ๋์ ์ง์ ํ ์ ์๋ค.
#include <signal.h> //header file
void (*signal(int signum, void (*handler)(int)))(int);
C
๋ณต์ฌ
parameters
โข
signum : ์๊ทธ๋ ๋ฒํธ
โข
(*handler)(int) : ์๊ทธ๋์ ์ฒ๋ฆฌํ ํธ๋ค๋ฌ
return value
โข
์ฑ๊ณต ์ : void *()(int); ์ด์ ์ ์ค์ ๋ ์๊ทธ๋ ํธ๋ค๋ฌ
โข
์คํจ ์ : null(?)
์๋๋ MacOS์ ์ ์๋์ด ์๋ Signal๋ค์ด๋ค.
โข
SIGKILL, SIGSTOP ๋ฑ์ ์ ์ดํ ์ ์๋ค.
example
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
void (*old_fun)(int);
void sigint_handler(int signo)
{
printf("signo : %d", signo);
printf("Ctrl-C ํค๋ฅผ ๋๋ฅด์
จ์ฃ !!\n");
printf("๋ ๋๋ฅด์๋ฉด ์ข
๋ฃ๋ฉ๋๋ค.\n");
signal(SIGINT, old_fun);
//signal( SIGINT, SIG_DFL);
}
int main( void)
{
old_fun = signal(SIGINT, sigint_handler);
// printf("asdf : %d", SIGTSTP);
// printf("zxcv : %d", SIGCHLD);
while (1)
{
printf("forum.falinux.com\n");
sleep(1);
}
}
C
๋ณต์ฌ
kill
function
ํ๋ก์ธ์ค์ ์๊ทธ๋์ ์ ์กํด์ฃผ๋ ํจ์. ์๊ทธ๋ SIGKILL์ ๋ณด๋ด๋ฉด ์์ kill๊ณผ ๊ฐ์ ์ญํ ์ ํ๋ค.
#include <signal.h> //header file
int kill(pid_t pid, int signo)
C
๋ณต์ฌ
parameters
โข
pid, signo : ์์ ๋์ผ
return value
โข
์ฑ๊ณต ์ : 0
โข
์คํจ ์ : -1
getcwd
function
ํ์ฌ ์์
์ค์ธ ๋๋ ํ ๋ฆฌ๋ฅผ ๊ฐ์ ธ์จ๋ค
#include <unistd.h> //header file
char *getcwd(char *buf, size_t size);
C
๋ณต์ฌ
parameters
โข
buf : ํ์ฌ ์์
๋๋ ํ ๋ฆฌ๋ฅผ ์ ์ฅํ buffer. ๋ง์ฝ buffer๊ฐ null์ด๋ฉด malloc์ ํตํด์ ํ ๋น๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ค. ๋ฐ๋ผ์ ์ด ๊ฒฝ์ฐ ๋ฌด์กฐ๊ฑด free๋ฅผ ํด์ฃผ์ด์ผ ํ๋ค.
โข
size : buffer์ ํ ๋น๋ ๋ฉ๋ชจ๋ฆฌ์ ํฌ๊ธฐ
return value
โข
์ฑ๊ณต ์ : ํฌ์ธํฐ
โข
์คํจ ์ : null (์ค๋ฅ ๋ด์ฉ์ errno์ ์ ์ฅ๋๋ค)
example
#include <unistd.h>
#include <stdio.h>
#include <limits.h>
int main() {
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("Current working dir: %s\n", cwd);
} else {
perror("getcwd() error");
return 1;
}
return 0;
}
C
๋ณต์ฌ
chdir
function
์์
๋๋ ํ ๋ฆฌ ํด๋ ๋ณ๊ฒฝํ๊ธฐ (cd in linux)
// man 2 chdir
#include <unistd.h> //header file
int chdir(const char *dirname);
C
๋ณต์ฌ
return value
โข
์ฑ๊ณต ์ : 0
โข
์คํจ ์ : -1
example
#include <stdio.h>
#include <limits.h>
#include <unistd.h> // chdir header
int main(void)
{
char buffer[PATH_MAX] = { 0, };
char changeDir[PATH_MAX] = { "/Users/42seoul/minishell/" };
int result = chdir(changeDir);
if(result == 0)
printf("Success\n");
else
perror("Fail...\n");
return 0;
}
C
๋ณต์ฌ
stat
function
ํ์ผ์ ํฌ๊ธฐ, ๊ถํ, ์์ฑ์ผ์, ์ต์ข
๋ณ๊ฒฝ์ผ ๋ฑ ํ์ผ์ ์ํ๋ ์ ๋ณด๋ฅผ ์ป๋ ํจ์์ด๋ค.
stat(2) ํจ์๋ symbolic link์ธ ํ์ผ์ path๋ก ๋๊ธฐ๋ฉด ๊ทธ ์๋ณธ ํ์ผ์ย ์ ๋ณด๋ฅผ ์ป๋๋ค.
// man 2 stat
#include <sys/stat.h> //header file
int stat(const char *path, struct stat *buf);
C
๋ณต์ฌ
parameters
โข
path : ํ์ผ ๋ช
๋๋ ํ์ผ์ ๋ํ ์๋๊ฒฝ๋ก/์ ๋๊ฒฝ๋ก
โข
buf : ํ์ผ์ ์ํ ๋ฐ ์ ๋ณด๋ฅผ ์ ์ฅํ ๊ตฌ์กฐ์ฒด
struct stat {
dev_t st_dev;
ino_t st_ino;
mode_t st_mode;
nlink_t st_nlink;
uid_t st_uid;
gid_t st_gid;
dev_t st_rdev;
off_t st_size;
blksize_t st_blksize;
blkcnt_t st_blocks;
time_t st_atime;
tiem_t st_mtime;
time_t st_ctime;
};
C
๋ณต์ฌ
return value
โข
์ฑ๊ณต ์ : 0
โข
์คํจ ์ : -1
example
์์ธ ์์๋ ์๋ ๋ธ๋ก๊ทธ๋ฅผ ๊ฐ๋ฉด ํ์ธํ ์ ์๋ค.
lstat, fstat
#include <unistd.h>
int lstat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
C
๋ณต์ฌ
โข
lstat : symbolic link์ธ ํ์ผ ์์ฒด์ ์ ๋ณด๋ฅผ ์ป๋๋ค. ๋๋จธ์ง๋ ๊ฐ๋ค.
โข
fstat : ์ธ์๋ก ๊ฐ์ ธ์จ fd(์ด๋ ค์๋ file)์ ์ ๋ณด๋ฅผ ์ป์ด์จ๋ค. ๋๋จธ์ง๋ ๊ฐ๋ค.
execve
function
์คํ๊ฐ๋ฅํ ํ์ผ์ธ filename์ ์คํ ์ฝ๋๋ฅผ ํ์ฌ ํ๋ก์ธ์ค์ ์ ์ฌํ์ฌ ๊ธฐ์กด์ ์คํ ์ฝ๋์ ๊ต์ฒดํ์ฌ ์๋ก์ด ๊ธฐ๋ฅ์ผ๋ก ์คํํ๋ค.ย ย ์ฆ, ํ์ฌ ์คํ๋๋ ํ๋ก๊ทธ๋จ์ ๊ธฐ๋ฅ์ ์์ด์ง๊ณ filename ํ๋ก๊ทธ๋จ์ ๋ฉ๋ชจ๋ฆฌ์ Loadingํ์ฌ ์ฒ์๋ถํฐ ์คํํ๋ค.
#include <unistd.h> //header file
int execve(const char *filename, char *const argv[], char *const envp[]);
C
๋ณต์ฌ
parameters
โข
filename : ๊ต์ฒดํ ์คํ ํ์ผ / ๋ช
๋ น์ด. path์ ๋ช
๋ น์ด๊ฐ ํฉ์ณ์ง ํํ๋ผ๊ณ ์๊ฐํ๋ฉด ํธํจ
ex) "usr/bin/ls"
โข
argv : execve์๋ argc๊ฐ ์์ผ๋ฏ๋ก array์ ๋ง์ง๋ง ์์ ๋ค์์ NULL์ด ์๋ค
โข
envp : key = value ํํ์ ํ๊ฒฝ๋ณ์ ๋ฌธ์์ด ๋ฐฐ์ด ๋ฆฌ์คํธ. ๋ง์ง๋ง์ NULL. ๋ง์ฝ ์ด๋ฏธ ์ค์ ๋ ํ๊ฒฝ๋ณ์๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด environ ์ ์ญ๋ณ์๋ฅผ ๊ทธ๋ฅ ์ฌ์ฉํ๋ค.
return value
โข
์ฑ๊ณต ์ : ์์ (์ด๋ฏธ ์ง์ ๋ ํ๋ก๊ทธ๋จ์ ๋ก์ง์ผ๋ก ์คํ๋๊ธฐ ๋๋ฌธ์ return์ ๋ฐ์ ์ ์๋ค)
โข
์คํจ ์ : -1
๋ช
๋ น์ด ์์น๋ฅผ ์ฐพ์๋ณด์
which "๋ช
๋ น์ด"
โ ๋ง์ฝ alias๊ฐ ๋์ด ์์ผ๋ฉด ํด์ ํ๊ณ ํ์ธ ํด์ผ ํ๋ค.
example
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
extern char **environ;
int main(int argc, char *argv[], char **envp)
{
char **new_argv;
char command[] = "ls";
int idx;
new_argv = malloc(sizeof(char *) * argc + 1);
// ๋ช
๋ น์ด๋ฅผ ls๋ก ๋ณ๊ฒฝ
new_argv[0] = command;
// command line์ผ๋ก ๋์ด์จ parameter๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉ
for (idx = 1 ; idx < argc ; ++idx)
new_argv[idx] = argv[idx];
// argc๋ฅผ execve ํ๋ผ๋ฏธํฐ์ ์ ๋ฌํ ์ ์๊ธฐ ๋๋ฌธ์ NULL์ ๋์ผ๋ก ์ง์ ํด์ฐ์ ์ผ ํจ
new_argv[argc] = NULL;
if (execve("/bin/ls", new_argv, environ) == -1){
fprintf(stderr, "ํ๋ก๊ทธ๋จ ์คํ error: %s\n", strerror(errno));
return 1;
}
printf("์ดํ ๋ก์ง์ ์คํ๋์ง ์์ต๋๋ค..\n");
return (0);
}
C
๋ณต์ฌ
dup
function
ํ์ผ ๋์คํฌ๋ฆฝํฐ๋ฅผ ๋ณต์ ํ์ฌ ๋ค๋ฅธ ํ์ผ ๋์คํฌ๋ฆฝํฐ ์์ฑ
#include <unistd.h> //header file
int dup(int file_des);
C
๋ณต์ฌ
parameters
โข
file_des : ํ์ผ ๋์คํฌ๋ฆฝํฐ
return value
โข
์ฑ๊ณต ์ : ํ์ผ ๋์คํฌ๋ฆฝํฐ
โข
์คํจ ์ : -1
example
#include <unistd.h> // dup header file
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int fd1, fd2;
if ((fd1 = open("file_1.txt", O_RDONLY)) < 0)
{
printf("file open error\n");
exit(0);
}
fd2 = dup(fd1);
printf("fd1 : %d, fd2 : %d\n", fd1, fd2);
close(fd1);
close(fd2);
return (0);
}
C
๋ณต์ฌ
โข
ํญ์ ๋ฎ์ ์์ ์๋ฅผ ๋ฐํํจ
dup2
function
fd2 ์ ๊ธฐ์กด์ ํ์ผ ๋์คํฌ๋ฆฝํฐ๋ฅผ ๋ณต์ .
๋ง์ผ fd2๊ฐ ์ด๋ฏธ ์ด๋ ค์์ผ๋ฉด fd2๋ฅผ ๋ซ์ ํ ๋ณต์ .
#include <unistd.h> //header file
int dup2(int file_des1, int file_des2);
C
๋ณต์ฌ
parameters
โข
file_des : ๊ธฐ์กด์ ํ์ผ ๋์คํฌ๋ฆฝํฐ
โข
file_des2 : ์๋ก์ด ํ์ผ ๋์คํฌ๋ฆฝํฐ
return value
โข
์ฑ๊ณต ์ : -1 ์ด์ธ์ ๊ฐ
โข
์คํจ ์ : -1
example
int main(void)
{
int fd;
if ((fd = open("file_1.txt", O_RDONLY)) < 0){
printf("file open error\n");
exit(0);
}
+- /* ํ์ค ์ถ๋ ฅ์ file_1.txt ํ์ผ๋ก redirection ํจ */
dup2(fd, 1);
/* ํ์ค ์ค๋ฅ๋ฅผ file_1.txt ํ์ผ๋ก redirection ํจ */
dup2(fd, 2);
close(fd);
return (1);
}
C
๋ณต์ฌ
pipe
function
์๋ก ๋
๋ฆฝ๋ ํ๋ก์ธ์ค๋ค์ด ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ์ ์ ์๊ฒ ๋์์ค๋ค.
#include <unistd.h> //header file
int pipe(int fd[2]);
C
๋ณต์ฌ
parameters
โข
fd[0] : ํจ์ ํธ์ถ ํ fd[0]์ ๋ฐ์ดํฐ๋ฅผ ์
๋ ฅ ๋ฐ์ ์ ์๋ fd๊ฐ ๋ด๊น(ํ์ดํ ์ถ๊ตฌ)
โข
fd[1] : ํจ์ ํธ์ถ ํ fd[1]์ ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅํ ์ ์๋ fd๊ฐ ๋ด๊น(ํ์ดํ ์
๊ตฌ)
return value
โข
์ฑ๊ณต ์ : 0
โข
์คํจ ์ : -1
example
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
int pipes[2] ;
void parent_proc() {
char * buf = 0x0;
ssize_t s;
size_t len = 0;
// ์ฝ๊ธฐ์ฉ ํ์ดํ๋ฅผ ๋ซ๋๋ค
close(pipes[0]);
while ((s = getline(&buf, &len, stdin)) != -1) {
buf[s - 1] = 0x0;
ssize_t sent = 0;
char * data = buf;
while (sent < s) {
sent += write(pipes[1], buf + sent, s - sent);
}
free(buf);
buf = 0x0;
len = 0;
}
close(pipes[1]);
}
void child_proc() {
char buf[32];
ssize_t s;
// ์ฐ๊ธฐ์ฉ ํ์ดํ๋ฅผ ๋ซ๋๋ค
close(pipes[1]);
while ((s = read(pipes[0], buf, 31)) > 0) {
buf[s + 1] = 0x0;
printf(">%s\n", buf);
}
exit(0);
}
int main() {
pid_t child_pid;
int exit_code;
if (pipe(pipes) != 0) {
perror("Error");
exit(1);
}
printf("%d %d\n", pipes[0], pipes[1]);
child_pid = fork();
if (child_pid == 0)
child_proc();
else
parent_proc();
return (0);
}
C
๋ณต์ฌ
opendir
function
๋๋ ํ ๋ฆฌ ์ด๊ธฐ
#include <dirent.h> // header file
DIR * opendir(const char * name);
C
๋ณต์ฌ
parameters
โข
name : open ํ ๋๋ ํ ๋ฆฌ์ ์ด๋ฆ
return value
โข
์ฑ๊ณต ์ : DIR ํฌ์ธํฐ ๋๋ ํ ๋ฆฌ ์ ๋ณด ๊ตฌ์กฐ์ฒด ํฌ์ธํฐ
โข
์คํจ ์ : NULL
readdir
function
๋๋ ํ ๋ฆฌ ๋ด์ ์๋ ๋ชจ๋ ํ์ผ๊ณผ ๋๋ ํ ๋ฆฌ ์ ๋ณด ๊ตฌํ๊ธฐ
#include <dirent.h> // header file
struct dirent *readdir(DIR *dirp);
C
๋ณต์ฌ
parameters
โข
dirp : open ํ ๋ ๋ฐํ๋๋ DIR ํฌ์ธํฐ
return value
โข
์ฑ๊ณต ์ : ๋๋ ํ ๋ฆฌ๋ด์ ํ์ผ/๋๋ ํ ๋ฆฌ์ ๋ํ ์ ๋ณด๋ฅผ ๊ฐ์ง ๊ตฌ์กฐ์ฒด ํฌ์ธํฐ
โข
์คํจ ์ : NULL
โข
readdir()์ ์ฒ์ ํธ์ถํ๋ฉด opendir()๋ฅผ ํตํด ์ฐ ๋๋ ํ ๋ฆฌ ๋ด์ ์ฒซ๋ฒ์งธ ํ์ผ์ ๋ํ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๊ณ , ๋ค์ ํธ์ถ์ ๋๋ฒ์งธ ํ์ผ์ ๋ํ ์ ๋ณด, ๋ค์ ํธ์ถ์ ์ธ๋ฒ์งธ ํ์ผ์ ํธ์ถ,,, ํ๋ค๊ฐ ๋์ด์ ํ์ผ์ด๋ ๋๋ ํ ๋ฆฌ๊ฐ ์์ผ๋ฉด NULL ๋ฐํ
closedir
function
๋๋ ํ ๋ฆฌ ๋ซ๊ธฐ
#include <dirent.h> // header file
int closedir(DIR *dirp);
C
๋ณต์ฌ
parameters
โข
dirp : open ํ ๋ ๋ฐํ๋๋ DIR ํฌ์ธํฐ
return value
โข
์ฑ๊ณต ์ : 0
โข
์คํจ ์ : -1
example (opendir/readdir/closedir)
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
int main()
{
DIR *dir_info;
struct dirent *dir_entry;
mkdir("test_A", 0755); // ์คํํ์ผ์ด ์๋ ๊ณณ์ dir ์์ฑ
mkdir("test_B", 0755);
dir_info = opendir("."); // ํ์ฌ ๋๋ ํ ๋ฆฌ ์ด๊ธฐ
if (dir_info)
{
while((dir_entry = readdir(dir_info))) // ๋๋ ํ ๋ฆฌ ์์ ์๋ ๋ชจ๋ ํ์ผ๊ณผ ๋๋ ํ ๋ฆฌ ์ถ๋ ฅ
printf("%s\n", dir_entry->d_name); // d_name : ์ฝํ ๋๋ ํ ๋ฆฌ/ํ์ผ์ ์ด๋ฆ ์ ์ฅ
closedir(dir_info);
}
return (0);
}
C
๋ณต์ฌ
strerror
function
์์คํ
์ค๋ฅ ๋ฒํธ์ ๋ํ ์ค๋ฅ ๋ฉ์ธ์ง๋ฅผ ๋ฌธ์์ด๋ก ๋ฐํํ๋ ํจ์์ด๋ค.
#include <string.h>
char *strerror(int errnum);
C
๋ณต์ฌ
parameters
โข
errnum : ์ค๋ฅ ๋ฒํธ
return value
โข
์ฑ๊ณต ์ : ์ค๋ฅ ๋ฒํธ์ ์ผ์นํ๋ ์ค๋ฅ ๋ฉ์ธ์ง ๋ฌธ์์ด
โข
์คํจ ์ : NULL or "Unknown error nnn"
example
#include <errno.h>
#include <string.h>
#include <stdio.h>
int main(int argc, char **argv)
{
FILE *fp;
if ((fp = fopen(argv[1], "r")) == NULL)
{
fprintf(stderr, "errno[%d] : %s\n", errno, strerror(errno));
return (1);
}
return (0);
}
C
๋ณต์ฌ
errno
๋ง์ง๋ง์ผ๋ก ๋ฐ์ํ ์๋ฌ ๋ฒํธ
#include <errno.h>
extern int errno;
C
๋ณต์ฌ
isatty
function
๋งค๊ฐ๋ณ์๋ก ๋ฃ์
#include <unistd.h>
int isatty(int fildes);
C
๋ณต์ฌ
parameters
โข
fildes : fd, ํ์ผ๋์คํฌ๋ฆฝํฐ
return value
โข
์ฑ๊ณต ์ : 1 (ํฐ๋ฏธ๋์ ์ฐ๊ฒฐ๋์ด ์๋ ๊ฒฝ์ฐ)
โข
์คํจ ์ : 0
example
์ฝ๋ ์คํ ์ ํ์ฌ pc์ ์ด๋ค ttyํ์ผ๋ค์ด ์๋์ง ํ์ธํด๋ณด์
$ cd /dev
$ ls
C
๋ณต์ฌ
tty(teletypewriter)
๋ฆฌ๋
์ค ๋๋ฐ์ด์ค ๋๋ผ์ด๋ธ ์ค์์ ์ฝ์์ด๋ ํฐ๋ฏธ๋์ ์๋ฏธ
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main()
{
int fd;
printf("%d\n", isatty(0));
fd = open("/dev/ttyp0",O_RDONLY);
printf("fd : %d, %d\n", fd, isatty(fd));
return (0);
}
C
๋ณต์ฌ
ttyname
function
fd์ ๋ํ ํฐ๋ฏธ๋ ์ด๋ฆ์ ์ป์ด์จ๋ค.
#include <unistd.h>
char *ttyname(int desc);
C
๋ณต์ฌ
parameters
โข
desc : fd
return value
โข
์ฑ๊ณต ์ : ํฐ๋ฏธ๋ ์ด๋ฆ, ์์ผ๋ฉด "(null)"
โข
์คํจ ์ : NULL
example
#include <unistd.h>
#include <stdio.h>
int main(void)
{
printf("your tty name is : %s\n", ttyname(0));
printf("your tty name is : %s\n", ttyname(1));
printf("your tty name is : %s\n", ttyname(2));
return (0);
}
C
๋ณต์ฌ
โ funcStudy git:(main) โ ./a.out
your tty name is : /dev/ttys008
your tty name is : /dev/ttys008
your tty name is : /dev/ttys008
Shell
๋ณต์ฌ
ttyslot
function
ttyslot - find the slot of the current user's terminal in somefile
#include <unistd.h>
int ttyslot(void);
C
๋ณต์ฌ
return value
โข
์ฑ๊ณต ์ : slot number
โข
์คํจ ์ : 0, -1
example
C
๋ณต์ฌ
โข
ioctl
โข
getenv
โข
tcsetattr
โข
tcgetattr
tgetent
function
์ปค์ ์ ์ด๋ฅผ ์ํด์๋ termcap ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ์ ์ํ ์ธํ
์ tgetent() ํจ์๋ก ์์๋๋ค.
termcap ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ ์ปดํ์ผ ์ -lncurses ์ต์
์ ๋ถ์ฌ์ฃผ์ด์ผ ํ๋ค.
#include <curses.h>
#include <term.h>
int tgetent(char *bp, const char *name);
C
๋ณต์ฌ
parameters
โข
bp : ๋ฒํผ ํฌ์ธํฐ
โข
name : ๋ฃจํด์ด๋ฆ(?)
return value
โข
์ฑ๊ณต ์ : 1
โข
ํด๋นํญ๋ชฉ์ด ์์ ๊ฒฝ์ฐ : 0
โข
terminfo ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ฐพ์ ์ ์๋ ๊ฒฝ์ฐ : -1
example
#include <termcap.h>
char *env = getenv("TERM"); //TERM ์ค์ ์ ๊ฐ์ ธ์ด
if (env == NULL)
env = "xterm"; //๋๋ถ๋ถ ๊ธฐ๋ณธ๊ฐ์ xterm
tgetent(NULL, env); // xterm ์ค์ ์ ์ฌ์ฉํ๋๋ก ์ด๊ธฐํ
char *cm = tgetstr("cm", NULL); // cursor motion
char *ce = tgetstr("ce", NULL); // ํ์ฌ ์ปค์ ์์น์์ ํ ์ค์ ๋๊น์ง ์ง์ฐ๊ธฐ
C
๋ณต์ฌ
tgetflag
function
??
#include <curses.h>
int tgetflag(char id[2]);
C
๋ณต์ฌ
parameters
โข
id : ??
return value
โข
์ฑ๊ณต ์ : the boolean entry for id
โข
์คํจ ์ : 0
tgetnum
function
??
#include <curses.h>
int tgetnum(char id[2]);
C
๋ณต์ฌ
parameters
โข
id : ??
return value
โข
์ฑ๊ณต ์ : the numeric entry for id
โข
์คํจ ์ : -1
tgetstr
function
์ฌ์ฉํ ์ ์๋ ๋ฃจํด์ ๊ฐ์ ธ์ด
#include <curses.h>
char *tgetstr(char id[2], char **area);
C
๋ณต์ฌ
parameters
โข
id : ??
โข
area : ??
return value
โข
์ฑ๊ณต ์ : the string entry for id
โข
์คํจ ์ : 0
tgoto
function
๋์ ์คํ์ ํ์ํ ๋ช
๋ น์ ๋ฐ๊ธฐ
#include <curses.h>
char *tgoto(char *cap,int col,int row);
C
๋ณต์ฌ
parameters
โข
cap : ๋ช
๋ น์ด
โข
col : ์ด๋ํ ํ
โข
row : ์ด๋ํ ์ด
return value
โข
์ฑ๊ณต ์ : ??
โข
์คํจ ์ : ??
example
// (5, 5) ์์น๋ก ์ปค์๋ฅผ ์ฎ๊ธฐ๊ธฐ
// ํฐ๋ฏธ๋ ์ฐฝ์ ๊ฐ์ฅ ์ผ์ชฝ ์๊ฐ (0, 0)
#include <unistd.h>
#include <termcap.h>
int putchar_tc(int tc)
{
write(1, &tc, 1);
return (0);
}
int main(void)
{
tgetent(NULL, "xterm");
char *cm = tgetstr("cm", NULL);
tputs(tgoto(cm, 5, 5), 1, putchar_tc);
// (5, 5) ์์น๋ก ์ด๋ํ๋(cm - cursor motion) ๋ช
๋ น์ tputs์ ์ ๋ฌํ์ฌ ์คํ
}
// ce ๋ช
๋ น์ ๊ฒฝ์ฐ tgoto ์์ด ๋ฐ๋ก tputs(ce, 1, putchar_tc)์ ํ์์ผ๋ก ์ฌ์ฉํด์ฃผ๋ฉด ๋๋ค.
// ์์ : tputs์ ์์
C
๋ณต์ฌ
tputs
function
๋์ ์คํ ํ๊ธฐ
#include <curses.h>
int tputs(const char *str, int affcnt, int (*putc)(int));
C
๋ณต์ฌ
parameters
โข
str : ??
โข
affcnt : ??
โข
putc : ??
return value
โข
์ฑ๊ณต ์ : ??
โข
์คํจ ์ : ??
example
// ce - clear line from cursor ๋ช
๋ น
#include <unistd.h>
#include <termcap.h>
int putchar_tc(int tc)
{
write(1, &tc, 1);
return (0);
}
int main(void)
{
tgetent(NULL, "xterm");
char *cm = tgetstr("cm", NULL);
tputs(ce, 1, putchar_tc);
}
C
๋ณต์ฌ
2. ๋ช ๋ น์ด ์ ๋ฆฌํ๊ธฐ!
echo (-n ์ต์
)
โข
C์ธ์ด์์์ printf ์ ๊ฐ์ ์ญํ ์ ํจ.
1) ์ต์
์์ด โ ์์ฝ ํ ๊ฐํ์ถ๋ ฅ
โ \ ๋ฅผ ๋ง๋๋ ํน๋ณํ๊ฒ ํด์ํ์ง ์๊ณ ๋ฌธ์ ๊ทธ๋๋ก ์ถ๋ ฅ
2) -n ์ต์
ย โ ์์ฝ ํ ๊ฐํํ์ง ์์
testcase โ ๋ฌด์กฐ๊ฑด ๋๋ ค๋ณผ ๊ฒ...
echo
echo hello
echo -n hello
echo hello -n
echo -nnn hello
echo -n-n hello
echo -nnn hello -n
echo -nnn -n hello
echo -
echo - -
echo --
echo -n -n -nhjk hello
echo -n -n -n -n -n hello
echo "hello World"
echo "hello World"
echo \\\"\'''\"
Plain Text
๋ณต์ฌ
cd (์๋๊ฒฝ๋ก ๋๋ ์ ๋๊ฒฝ๋ก๋ง)
โข
"change directory"์ ์ฝ์๋ก ํน์ ๋๋ ํ ๋ฆฌ๋ก ์ด๋ํ๊ณ ์ถ์ ๋ ์ฌ์ฉ.
cd ๋ค์ ๊ฐ๊ณ ์ถ์ ๋๋ ํ ๋ฆฌ ๊ฒฝ๋ก๋ฅผ ์
๋ ฅ.
cd / : ๋ฃจํธ ๋๋ ํ ๋ฆฌ๋ก ์ด๋ (์ ๋ ๊ฒฝ๋ก)
cd /home/pi : /home/pi ๋ก ์ด๋ (์๋ ๊ฒฝ๋ก)
pwd (์ต์
์์ด)
โข
"print working directory"์ ์ฝ์๋ก ํ์ฌ ์์
์ค์ธ ๋๋ ํ ๋ฆฌ์ ์ ๋ ๊ฒฝ๋ก๋ฅผ ๋ฐํ
export (์ต์
์์ด)
โข
์ฌ์ฉ์ ํ๊ฒฝ ๋ณ์๋ฅผ ์ ์ญ ๋ณ์๋ก ์ค์ (๋ณ์๋ช
์ ๊ณต๋ฐฑ์ด ์์ผ๋ฉด ์๋จ)
# ์์
export <๋ณ์๋ช
>=<๊ฐ> : export JAVA_HOME=/user/lib/java-7-openjdk-amd64/
Shell
๋ณต์ฌ
# ์ง์ฆ๋๋ ์์
export a #key๊ฐ๋ง ์์ฑ
export b= #value์ ์๋ฌด ๊ฐ ์์
export c=hello
export c+=world #ํ๊ฒฝ๋ณ์ ๋ค์ ๋ง๋ถ์ด๊ธฐ
export d="oh my god" #echo์ถ๋ ฅ๊ณผ export์ถ๋ ฅ ๋ค๋ฆ
export e=elephant f=flower
Bash
๋ณต์ฌ
echo๋ก ์ถ๋ ฅ
(๋งค๊ฐ๋ณ์ ์๋ export) ์ถ๋ ฅ๊ฒฐ๊ณผ
unset (์ต์
์์ด)
โข
๋ณ์๋ฅผ ์ ๊ฑฐ
# ์์
[root@wiki ~] str="hello world"
[root@wiki ~] echo $str
hello world
[root@wiki ~] unset str
[root@wiki ~] echo $str
[root@zetawiki ~]
Shell
๋ณต์ฌ
env (์ต์
๊ณผ ๋งค๊ฐ๋ณ์ ์์ด)
โข
์ ์ญ ๋ณ์ ์ค์ ๋ฐ ์กฐํ
exit (์ต์
์์ด)
โข
shell ์ข
๋ฃ
', " (๋ฐ์ดํ)
<, >, >> (๋ฆฌ๋๋ ์
)
- file descriptor aggregation์ ์ ์ธํ๊ณ bash์์์ฒ๋ผ ์๋
โข
<ย : ์
๋ ฅ ๋ฆฌ๋ค์ด๋ ์
โข
>ย : ์ถ๋ ฅ ๋ฆฌ๋ค์ด๋ ์
- ์ถ๋ ฅ๋ ํ์ผ์ ์กด์ฌํ์ง ์์ ๊ฒฝ์ฐ ์๋์์ฑ
- ๋ง์ฝ ์ด๋ฏธ ์กด์ฌํ๋ ๊ฒฝ์ฐ ๋ฎ์ด์ฐ๊ธฐ
โข
>>ย : > ์ ๊ฐ์ง๋ง ์ด๋ฏธ ํ์ผ์ด ์กด์ฌํ๋ ๊ฒฝ์ฐ ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ๋ค
<<
โข
์ฐ์ฐ์์ ์ค๋ฅธ์ชฝ์ ์ง์ ๋ ๊ฒ์ด ํ ์ค (์ : EOF)์์ ์ถฉ์กฑ ๋ ๋๊น์ง ์ค ๋ฐ๊ฟ์ ํฌํจํ ์ฌ์ฉ์ ์
๋ ฅ์ ๊ฐ์ ธ์ต๋๋ค.
โข
EOF ๊ฐ์ ์ ์ธํ๊ณ ์ฝ์ ๋ชจ๋ ๊ฒ์ ์ผ์ชฝ ํ๋ก๊ทธ๋จ์ ํ์ค ์
๋ ฅ์ผ๋ก ๋ณด๋
๋๋ค.
cat << EOF
> Hello
> World
> EOF
--result--
Hello
World
Shell
๋ณต์ฌ
| (ํ์ดํ)
โข
command1 | command2 ์ ๊ฐ์ ํํ๋ก ์ฌ์ฉ๋๊ณ , command1์ ํ์ค ์ถ๋ ฅ์ command2์ ํ์ค ์
๋ ฅ์ผ๋ก ์ ๋ฌ. (&๋ฅผ ๋ถ์ด๋ฉด ํ์ค ์๋ฌ๋ ํจ๊ป ์ ๋ฌ)