Lab 1 - Introduction: Environment setup. UNIX command line introduction. C programming in the UNIX command line.

 

 

Introduction: Grading/attandance rules, Lab tests on the exam server, Course website 


Environment setup

 

 

 


UNIX command line introduction

It is the most powerful and flexible way to get things done in an operating system, provided that you know what you are doing.

a. Linux: Sh, Bash, Ksh, ...
b. Windows: Cmd (remember from assembly, building, linking and executing code), Powershell, ...
c. MacOS: Terminal (actually Bash)

 

A. Basic unix commands

 

  1.  ls
  2.  mkdir numefir
  3.  cd numedir
  4.  cd ..
  5.  cd ../..
  6.  cd /home
  7.  echo $HOME
  8.  cd $HOME     (or  cd ~ )
  9.  clear
  10.  cp sursa destinatie (ex. ../fisier.txt .) ; cp cale_fisier cale_director
  11.  cp *.ext .        
  12.  cp -R ../bbbc .
  13.  mv sursa destinatie
  14.  mv numedirvechi numedirnou
  15.  rm fis.txt
  16.  rmdir numedir
  17.  (rmdir -r recursiv...)
  18.  ls -d */
  19.  ls -l
  20.  man ls      (use keyboard to browse, to exit type Q)
  21.  ls *.ext
  22.  date
  23.  cal
  24.  grep cuvand numefisier
  25.  ifconfig
  26.  ping google.ro
  27.  ps
  28.  kill -9 PID (we'll try this later :D )
  29.  touch fis.txt 
  30.  echo "Hello Pumba!" > fis.txt
  31.  echo "Goodbye my lover!" > fis.txt
  32.  cat fis.txt  | sort 
  33.  finger

Grouping/separating comds with:
 ; | && || () {}

Redirecting:

0 - stdin

1 - stdout

2 - stderr

output:  > ...  (redirect output to... and overwrite)               >> ... (append to ...) 

input:    ... <   (provide input to ...)                                    ... << DELIMITER (read input continuously until the specified DELIMITER comes up)       

error:      2>...    (redirect standsrd error to ..)

>&1   >&2

pipe:   cmd1  | cmd2   (the output from cmd1  will be input for cmd2); eaxmple: cat fis.txt | sort

 

 

Homework 1: ls, cd, mkdir, pwd, man, head, tail, cut, clear, kill, echo, less, cp, find, zip, unzip, tar, gzip, gunzip, write, who, finger, ping, cat, sort, wc, rm, chmod, passwd, ps, fg, bg, file, w, mv, df, du, comm, diff, mesg, date, cal, nohup, ssh, scp, pine/mutt/alpine (for email), mesg n, whatis, apropos

 


B. Key combinations and terminal usage tips

Ctrl+S locks the console => Ctrl+Q to unlock
Ctrl+C terminates the current process
Ctrl+Z send the current process in background (Ctrl+L to restore or fg command)
Ctrl+D exit (runs the exit command)

 

 

C. Manual

 

 

 

 


 

C programming in the UNIX command line

 

A. Command line editors

    vim editor

syntax on
set tabstop=4
set expandtab

 

Homework 2: learn vi  (or a similar editor)

VIM interactive tutorial: http://www.openvim.com/tutorial.html

Practice recovering a file in VIM: https://www.zachpfeffer.com/single-post/Practice-Recovering-a-File-in-Vim

B. Coding in C 

To open the code editor:

>> vi helloworld.c    

or

>> joe helloworld.c

etc.

 

C. Compile, debug and execute

 To compile (this will create the output executable file names hello or print on the screen the compiling errors):

 >> gcc -Wall -o hello helloworld.c 

To execute:

>> ./hello

 

Hello world example program: 

 

helloworld.c

--------------------------------------------
#include <stdio.h>
int main(int argc, char** argv) {
       printf(“Hello COVID world!\n”);
       return 0;
}

          --------------------------------------------

 

Homework 3: Learn/remember C (not C++). Ex. Write a C program that reads 10 real numbers, stores them into an array and computes the average, min and max.