Quick viewing(Text Mode)

YOUNGMIN KWON / TONY MIONE Some Useful UNIX Commands

YOUNGMIN KWON / TONY MIONE Some Useful UNIX Commands

CSE320 System Fundamentals II Hello World YOUNGMIN KWON / TONY MIONE Some useful commands

Directory related commands ◦ :lists directory contents. ◦ Example: ls –al # List all files with long (extended) : create a directory. ◦ Example: mkdir abc # Create a new directory called abc in the current dir ◦ Example: mkdir –p development/src/mainmodule # Create directory mainmodule inside of src inside of development. Create any directories in that path that do not yet exist ◦ : change directory. ◦ Example: cd abc # Change working dir to abc ◦ Example: cd .. # Change working dir to parent of current directory ◦ : remove a directory. ◦ Example: rmdir abc # Remove directory abc inside the current directory ◦ : print current directory. ◦ Example: pwd # Print the path of the current working directory

(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 2 Some useful UNIX commands

File Related commands ◦ : copy files. ◦ Example: cp * abc/ # Copy all files in the current dir to dir abc ◦ Example: cp a.txt b.txt # Copy the a.txt to the file b.txt ◦ : move files. ◦ Example: mv abc/* bcd/* # Move all files from dir abc to dir bcd ◦ Example: mv a.txt b.txt # Move the file a.txt to the file b.txt ◦ : print the contents of a file. ◦ Example: cat a.txt # Print contents of file a.txt to the screen ◦ : looking for a pattern. ◦ Example: grep hello * # Look for the string Hello in all files in # the current working directory

(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 3 Some useful UNIX commands

man (manual page) -> Prints Unix reference pages ◦ section number 2 is for system calls, 3 is for library routines ◦ man 3 printf ◦ man 2 fork ◦ man sin ◦ man –k # list related to # list man page related to

(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 4 Hello.c

// #include tells the compiler to copy the contents of // the specified file to this file. // The line below will copy the contents of stdio.h to hello.i #include

// main is the function that starts the program int main() {

// printf prints out the parameter to the screen printf("hello, world\n");

// returning 0 from main indicates a normal completion. // returning non-zero means abnormal termination. return 0; }

(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 5 Compiling Hello.c To compile hello.c To create hello instead of a.out run gcc –o hello hello.c gcc hello.c It will create a.out. To run this, enter the following: ./hello To run a.out It will produce the same output as ./a.out a.out It will print out hello, world

(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 6 printf

int main() { printf("char value: %c\n", 'a'); printf("int value: %d\n", 'a'); printf("char value: %c, int value: %d\n", 97, 97); printf("hex number: %x\n", 97); printf("float value: %f\n", 1.0f / 3); printf("double value: %lf\n", 1.0 / 3); printf("string value: %s, %s\n", "hello", "world"); }

Format Specifiers Tutorial 1

Format Specifiers Reference

(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 7 printf

int main() { printf("char value: %c\n", 'a'); printf("int value: %d\n", 'a'); printf("char value: %c, int value: %d\n", 97, 97); // … }

char value: a int value: 97 char value: a, int value: 97

(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 8 printf

int main() { //… printf("hex number: %x\n", 97); printf("float value: %f\n", 1.0f / 3); printf("double value: %lf\n", 1.0 / 3); printf("string value: %s, %s\n", "hello", "world"); }

hex number: 61 float value: 0.333333 double value: 0.333333 string value: hello, world

(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 9 on printf

// string literals are the addresses where the are printf("string pointer: %d, %d\n", "hello", "world");

// more about strings printf("%d, %c, %c, %c, %c, %c\n", "hello", "hello"[0], "hello"[1], "hello"[2], "hello"[3], "hello"[4]);

string pointer: 4196476, 4196470 4196476, h, e, l, l, o

(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 10 More on printf

char *str = "hello"; printf("%d, %c, %c, %c, %c, %c\n", str, str[0], str[1], str[2], str[3], str[4]);

int adr = (int)"hello"; printf("%d, %c, %c, %c, %c, %c\n", adr, ((char*)adr)[0], ((char*)adr)[1], ((char*)adr)[2], ((char*)adr)[3], ((char*)adr)[4]);

4196476, h, e, l, l, o 4196476, h, e, l, l, o

(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 11 More on printf

// Can we do this? printf("function pointer: %d\n", printf); printf("function pointer: %p\n", main); function pointer: 4195392 function pointer: 0x400534

(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 12 scanf: to read user’s input

#include int main() { Enter your name: tony // read a string hello tony. char name[100]; Enter a number: 123 printf("Enter your name: "); scanf("%99s", name); read 123. printf("hello %s.\n", name); Note 1: format specifiers // read an integer number are the same as those in int num; printf("Enter a number: "); printf() scanf("%d", &num); printf("read %d.\n", num); Note 2: Parameter to return 0; receive the value is } ALWAYS a pointer (or a crash will occur).

(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 13 More on scanf

#include #include • How to compile the program above. int main() $ gcc -o scan scan.c { /tmp/ccNDavtQ.o:scan.c:function main: // read a floating point number error: undefined reference to 'sin' float fnum; collect2: ld returned 1 status printf("Enter a floating point number: "); scanf("%f", &fnum); -lm option to gcc will fix the error printf("read %f.\n", fnum); printf("sin(%f) = %f.\n", fnum, sin(fnum)); gcc –o scan scan.c -lm return 0; } ./scan

Enter a floating point number: 0.21 read 0.210000. sin(0.210000) = 0.208460.

(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 14 Arithmetic Operators

#include int main() { int a = 0xff, b = 0x05, c = 0x50; printf("a: %5d, b: %5d, a + b: %5d\n", a, b, a + b); printf("a: %5d, b: %5d, a - b: %5d\n", a, b, a - b); printf("a: %5d, b: %5d, a * b: %5d\n", a, b, a * b); printf("a: %5d, b: %5d, a / b: %5d\n", a, b, a / b); printf("a: %5d, b: %5d, a %% b: %5d\n", a, b, a % b); return 0; }

a: 255, b: 5, a + b: 260 a: 255, b: 5, a - b: 250 a: 255, b: 5, a * b: 1275 a: 255, b: 5, a / b: 51 a: 255, b: 5, a % b: 0

(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 15 Bitwise Operators printf("a: %5d, b: %5d, a & b: %5d\n", a, b, a & b); printf("c: %5d, b: %5d, c | b: %5d\n", c, b, c | b); printf("a: %5d, b: %5d, a ^ b: %5d\n", a, b, a ^ b);

// One’s complement vs Two’s complement printf("b: %5d, ~b: %5d (%x)\n", b, ~b, ~b); printf("-1: %x, -2: %x, -3: %x\n", -1, -2, -3); printf("b: %5d, b << 1: %5d\n", b, b << 1); printf("b: %5d, b >> 1: %5d\n", b, b >> 1); a: 255, b: 5, a & b: 5 c: 80, b: 5, c | b: 85 a: 255, b: 5, a ^ b: 250 b: 5, ~b: -6 (fffffffa) -1: ffffffff, -2: fffffffe, -3: fffffffd b: 5, b << 1: 10 b: 5, b >> 1: 2

(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 16 Bitwise Operators

11111111 a: 255, b: 5, a & b: 5 c: 80, b: 5, c | b: 85 & 00000101 00000101 01010000 | 00000101 01010101

(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 17 Bitwise Operators

11111111 a: 255, b: 5, a ^ b: 250 b: 5, ~b: -6 (fffffffa) ^ 00000101 11111010

~ 00000101 11111010 => (0xfa)

(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 18 Bitwise Operators

-1: ffffffff, -2: fffffffe, -3: fffffffd, -4: fffffffc, -5: fffffffb b: 5, b << 1: 10 b: 5, b >> 1: 2

00000101 >> 1 00000101 << 1 00000010 00001010

(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 19 Logical Operators

printf("a: %5d, b: %5d, a == b: %5d\n", a, b, a == b); printf("a: %5d, b: %5d, a != b: %5d\n", a, b, a != b); printf("a: %5d, b: %5d, a > b: %5d\n", a, b, a > b); printf("a: %5d, b: %5d, a >= b: %5d\n", a, b, a >= b); printf("a: %5d, b: %5d, a < b: %5d\n", a, b, a < b); printf("a: %5d, b: %5d, a <= b: %5d\n", a, b, a <= b);

a: 255, b: 5, a == b: 0 a: 255, b: 5, a != b: 1 a: 255, b: 5, a > b: 1 a: 255, b: 5, a >= b: 1 a: 255, b: 5, a < b: 0 a: 255, b: 5, a <= b: 0

printf("a: %5d, b: %5d, a && b: %5d\n", a, b, a && b); printf("a: %5d, b: %5d, a || b: %5d\n", a, b, a || b); printf("a: %5d, !a: %4d, !!a: %5d\n", a, !a, !!a);

a: 255, b: 5, a && b: 1 a: 255, b: 5, a || b: 1 a: 255, !a: 0, !!a: 1

(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 20 More Logical Operators

printf("a: %5d, b: %5d, a && b: %5d\n", a, b, a && b); printf("a: %5d, b: %5d, a || b: %5d\n", a, b, a || b); printf("a: %5d, !a: %4d, !!a: %5d\n", a, !a, !!a);

// Note that in C, any non-zero value is true (1)

a: 255, b: 5, a && b: 1 a: 255, b: 5, a || b: 1 a: 255, !a: 0, !!a: 1

(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 21 Side Effects from a Compiler Optimization a > b && printf("a > b && print\n");

// a is greater than b – the next statement // is known false before && so it ‘short-circuits’ // and does not print a < b && printf("a < b && print\n");

// a is greater than b – the next statement // is known true before || so it ‘short-circuits’ // and does not print a > b || printf("a > b || print\n"); a < b || printf("a < b || print\n"); // a is greater than b a > b && print a < b || print

(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 22 Side Effects from a Compiler Optimization // Typical recursive gcd: // gcd: Euclidean algorithm int gcd(int a, int b) { int gcd(int a, int b) if (a > b) { { return gcd(a-b, b); return a > b && gcd(a - b, b) || } else { a < b && gcd(b - a, a) || if (b > a) { printf("gcd: %d\n", a); return gcd(b-a, a); } } else { return a; } } }

(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 23 Questions?

(C) CSE320 YOUNGMIN KWON / TONY MIONE -SUNY KOREA, 2019 24