lecture 2 linux basic commands,shell and make september 8, 2015 kyu ho park

44
Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Upload: belinda-jennings

Post on 31-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Lecture 2Linux Basic Commands,Shell and

Make

September 8, 2015Kyu Ho Park

Page 2: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

Linux Basics

Page 3: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

root 계정 전환ubuntu@omap:~$ sudo passwd root[sudo] password for ubuntu: temppwdEnter new UNIX password: temppwd ( 또는 원하는 패스워드 )Retype new UNIX password: temppwd ( 또는 원하는 패스워드를 다시 입력 )passwd: password updated successfullyubuntu@omap:~$ su rootPassword: temppwd ( 또는 위에서 입력한 패스워드 )root@omap:/home/ubuntu#

Page 4: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

Linux commands File related:

ls , cd, mkdir, rm, df, du, mount, unmount, mkfs touch, chmod, chown, tar, cp, mv,

Process related:clear, whoami, whereis, su, pwd

Page 5: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

Shell-command interpreter

‘User’LinuxLinux

Shell

Page 6: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

Shell programming-script : it is a file that contains shell command and it is a shell program.

-Creating a Script#!/bin/bash

# An example of a shell script that replaces the command ‘ls –l > output.txt’

ls –l > output.txt

exit 0

Page 7: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

Shell Syntax Variables Conditions Program control: if, elif, for, while, until, case Functions Lists

Page 8: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

Project1-Task1

Page 9: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

Tree search

9

Page 10: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

$tree.├── Algol│   ├── Ch1│   │   ├── c1p1│   │   ├── c1p1.c│   │   ├── c1p2│   │   ├── c1p2.c│   │   ├── c1p3│   │   ├── c1p3.c│   │   ├── data│   │   ├── data.txt│   │   ├── ex03-01│   │   ├── ex03-01.c│   │   ├── ex04-01│   │   ├── ex04-01.c│   │   ├── ex10-01.c│   │   ├── ex12-01c│   │   ├── ex12-01c.c│   │   ├── ex12-01s│   │   ├── ex12-01s.c│   │   ├── fifo│   │   ├── honor│   │   ├── honor.c│   │   ├── newdata1.txt│   │   ├── newdata.txt│   │   ├── newname.txt│   │   ├── test.txt│   │   └── test.txt.soft -> test.txt│   ├── Ch2│   │   ├── mem-region│   │   └── mem-region.c│   ├── fileTest│   │   └── data│   ├── infile│   ├── outfile│   ├── pipewr│   ├── pipewr.c│   ├── testWelcom│   ├── testWelcom.c│   └── testWelcom.s├── clone├── clone.c├── DDriver│   ├── Makefile│   ├── P301│   ├── P301.c│   ├── P303│   ├── P303.c│   └── test.c├── express├── fork├── fork.c├── sh1├── shQuot├── traverse└── treesearch

10

Page 11: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

Interactive program

Page 12: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

Making a script executable

Page 13: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

Shell script( commands in a file )

Page 14: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

variables

Page 15: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

Quoting

Page 16: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

if~fi if [ -d $directory ]; then ---- fi

if[ -d $directory ] then --- fi

Page 17: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

if~fi -d file //True if the file is a directory -e file //True if the file exists -f file //True if the file is a regular file -x file //True if the file is executable ----

Page 18: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

‘if~fi’ control structure if condition then

statements else

statements fi

Page 19: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

forSample script

Page 20: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

for

Page 21: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

while while condition do statements done

Page 22: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

functions function( ) {

statements }

Page 23: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

function example

Page 24: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

Project 2 Simple C programing

Basic Linux System Callsopen(),read(),write(),lseek(),close()

24

Page 25: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

Basic Linux System Callsopen(),read(),write(),lseek(),close()

25

open( ) #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> int open(const char *path, int oflags); int open(const char *path, int oflags, mode_t mode); Open with oflag=O_CREATE, we must use the three-pa-

rameter form with mode. mode: S_IRUSER: Read permission, owner S_IWGRP:Write permission, group S_IXOTH: Execute permission, others oflag={O_RDONLY,O_WRONLY,O_RDWR} ored

with{O_APPEND, O_TRUNC, O_CREATE, O_EXCL}

Page 26: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

read(), write() read()

#include <unistd.h>size_t read(int fildes, void *buf,

size_t nbytes);

write() #include <unistd.h>

size_t write(int fildes, const void *buf, size_t nbytes);

26

Page 27: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

lseek() lseek()

#include <unistd.h>#include <sys/types.h>off_t lseek(int fildes, off_t offset, int

whence); //lseek() system call sets the read/write pointer of a file descriptor fildes. whence: SEEK_SET: offset is an absolute position, SEEK_CUR:offset is relative to the current position, SEEK_END:offset is relative to the end of the file.

27

Page 28: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

rand( ) rand( )

#include <stdlib.h>#define RAND_MAX 0x7ff

rand() //it will generate a number of [0, 0x7ff]

28

Page 29: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

memalign() memalign() allocates size bytes and re-

turns a pointer to the allocated memory. The memory address will be a multiple of boundary, which must be a power of two.

29

Page 30: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

make and makefile

Page 31: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

make [-f filename]% make [-f filename] - if you don’t use –f option, the default file is Make-file or makefile. -if there are Makefile and makefile at the same time, Makefile will be selected.

Page 32: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

example/*file: main.c*/#include <stdio.h>#include “test.h”

int main(void){

test1();test2();printf(“Hello world!\

n”);return 0;

}

/*file: test.h*/void test1(void);void test2(void);

/*file: test1.c*/void test1(void){

}

/*file: test2.c*/void test2(void){

}

Page 33: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

Without ‘make’%gcc –c main.c%gcc –c test1.c%gcc –c test2.c%gcc –o test main.o test1.o test2.o

Page 34: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

With ‘make’-Makefile %vi Makefile test : main.o test1.o test2.o

gcc –o test main.o test1.o test2.o main.o : test.h main.c

gcc –c main.c test1.o : test.h test1.c

gcc –c test1.c test2.o : test.h test2.c

gcc –c test2.c

%makegcc –c main.cgcc –c test1.cgcc –c test2.cgcc –o test main.o test1.o test2.o

Page 35: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

test2.c is modified%vi test2.c/*file:test2.c*/#include <stdio.h>void test2(void){

printf(“This is test2.c\n”);} %make gcc –c test2.c gcc -0 test main.o test1.o test2.o

Page 36: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

Macros in a makefile

Page 37: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

Page 38: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

LabelMakefile2:

Page 39: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

make clean

Page 40: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

Typical MACRO var.#Which compilerCC =gcc #Option flagsCFLAGS = -g –Wall#Program source filesSRCS = main.c test1.c test2.c#object filesOBJS#Library filesLIBS#Library directoriesLIBDIRS

Page 41: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

Page 42: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

Error

Page 43: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

Implicit rule$@ #Name of the current target$? # List of prerequisites(files the target depends on) #changed more recently than the cur-rent target$< #Name of the current prerequisite$* #Name of the prerequisite, without any suffix

Page 44: Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park

Computer Engineering Research Labora-tory

Kernel Compile1.Kernel Configuration to give the information of current H/W, Kernel, Network characteristics to the newly made kernel. -make config, make menuconfig, make xconfig2.Kernel compile -make bzImage or make zImage( for a small sized

kernel) - make( after Linux version 2.6)

3.Kernel install -make install