CS/System Programming

Linux Shell & Basic Commands

dawonny 2022. 3. 19. 02:48
728x90
반응형
Shell?

- 유저가 운영체제에 접근하도록 해주는 interface(명령어 기반)

- Functionality : * 다른 프로그램을 (command line으로) 실행

                     * 파일 관리(OS안의 파일과 프로세스를 command line으로 관리)

                       터미널에서 command를 치면 shell을 통해서 kernel(즉, OS)에 전달해서 기능함

                     * 프로세스들을 관리

- A program like any other one

- log on 할때 실행됨.

 


흔히 사용되는 Shells

- /bin/sh         The Bourne Shell / POSIX shell

– /bin/csh       C shell

– /bin/tcsh      Enhanced C She

– /bin/ksh       Korn shell

– /bin/bash     Free ksh clone


Shell의 기본 폼(source)
while (read command line from user) {
  parse the command line
  execute the command
}

계속 프로그램을 실행시키다가 command나 타이핑을 하면 입력받은 명령어를 parsing(분석)해서 전달함.


Shell Interactive Use

 

When you log in, you interactively use the shell:

• Command history(명령어 기록)

• Command line editing(명령어 수정)

• File expansion (tab completion support) (특정 dir의 파일을 볼 수 있음. tab 누르면 확인가능)

• Command expansion(명령어 입력하고 tab 누르면 해당되는 걸 알려줌.)

• Key bindings

• Spelling correction

• Job control


Frequently Used Command

pwd print a current working directory

cd change working directory

cat concatenate files and print on the standard output

chmod change a file access permission

vi create/edit files

ls list contents of the current directory

rm remove file

mv rename file

cp copy a file

touch create an empty file

mkdir create a directory

rmdir remove a directory

more display a file page by page

od display binary files

ln make a link to a file (symbolic or hard link)

file determine file type

passwd change the user password

split split a file

 


Simple commands

 

 simple command: sequence of non blanks arguments separated by blanks or tabs.

 

 1st argument (numbered zero) usually specifies the name of the command to be executed.

 

 Any remaining arguments:

  • Are passed as arguments to that command.

  • Arguments may be filenames, pathnames, directories or special options

  • Special characters are interpreted by shell

 

 기본 명령어를 실행

 

 Parsing into command in arguments is called splitting


Types of Arguments

 A command example with several options

-> 두 개의 파일을 압축해서 archive 라는 파일을 만들어 내라는 의미

 

 Options/Flags

   • convention: -X or --longname

 

 Parameters

   • may be files, may be strings

   • depends on command


Linux manual sections

명령어에 대한 정보를 알고 싶으면 매뉴얼을 잘 활용할 것.

ex) man ls 이렇게  치면 어떻게 사용하는지 등의 정보가 잘 나옴.

 


Linux File System

현재 사용하고 있는 구조를 이해하기 위해서는

리눅스 파일 시스템이 디스플레이 되는 구조를 이해할 필요가 있음.

 

- Directory hierarchy

기본적으로 리눅스는 트리 형태의 Directory hierarchy 구조를 파일 시스템에서 제공하고 있음.

-> 최상위에 있는 걸  root directory 라고 함 (windows 에서는 폴더라는 개념. 파일을 담을 수 있음)

    cd / 하면 root directory로 이동 가능

- HOME directory

   • a directory assigned to a user

   • logging in을 하면, 유저는 유저의 home에 처음으로 위치함

   • usually placed under /home

   • Q: Where is the home for a user ‘mtk’ in the above system?

 


File/Directory Path

- 절대 경로

   root로 부터 시작해서 그 위치까지의 모든 경로를 보여준다.

  e.g. /home/avr/java/Go.java

- 상대 경로

   내가 현재 있는 위치 기준으로 보여줌

 

현재는 single dot( . )

그 위의 디렉토리(부모)면 double dots( . . )

 

Ex)


File Permission

- 각각의 파일/디렉토리는 접근권한이 있음

   * 각각의 파일은 소유자가 있다

   * 리눅스는 유저의 group을 지원함

      e.g. sp2020 : a group of all students in SP class

   * file/directory specifies a permission to owner, group, and others.

r 이 있으면 read 가능

w이 있으면 write 가능

x 있으면 execute 가능

 

만약 - 가 되어있는 거면 불가능 하다는 것

r-x는 읽기가능하고 실행가능하지만 쓰기는 불가능하다 라는 의미

-rwxr-wr-x

사용자 그룹(사용자 포함) 이외의 다른사람들


- 접근 권한을 어떻게 바꾸는가?

  • use command : chmod [permission] [filename]

ex) myFile.txt 의 파일에 대한 모든 접근권한을 소유자와 그룹에게 주고

    그 외의 다른 사람들에게는 read 권한만 주고 싶음

    111111100(2) = 774(8)

    $ chmod 774 myFile.txt

 

 


Linux Programing Process

각종 system call interface 를 통해서 시스템프로그래밍을 하고 

프로그램 소스파일을 실제 실행을 해야하는데 

소스파일을 가지고 실행하려면 실행파일을 만들어야 함.

컴파일을 하면 실행파일이 만들어짐! -> 이걸 실행시킴.

 

이런 과정을 아래에 도식화 함.

컴파일을 하려면 gcc나 g++ 를 씀

(g++ 는 c++ 컴파일러임)


gcc Compiler

gcc -o test test.c : test.c 라고 하는 소스파일을 이용해서 test라는 실행파일을 만들어 내겠다.

 

녹색으로 된 파일은 리눅스에서 실행시킬 수 있는 파일을 의미

실행파일을 실행하려면 ./실행파일  이렇게 치면 됨(실행파일을 현재 디렉토리에서 실행)

. / 붙여야함!

 

소스가 2개이상일 때에는

gcc file1.c file2.c -o fileout

-> compile and link file1.c and file2.c, then generates the fileout

 

gcc -c file1.c file2.c

-> compile only and generates the object files

 

gcc –o fileout file1.c file2.c

-> link the file1.c file2.c, then generates the fileout

 

 


개인적으로 편해서 덧붙이는 명령어

ctrl + P : 이전 명령어

ctrl + N : 다음 명령어

 

728x90
반응형