This shows you the differences between two versions of the page.
uso:cursuri:curs-05 [2018/11/07 23:15] mbarbulescu |
uso:cursuri:curs-05 [2022/10/31 22:54] (current) sergiu.weisz |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== Curs 05 - Interfața în linia de comandă ======= | ====== Curs 05 - Interfața în linia de comandă ======= | ||
- | * [[https://drive.google.com/open?id=13BjaEg6a-g2jLExEYaDZzbYHvpXIl9el|Slide-uri curs]] | + | * [[https://docs.google.com/presentation/d/1Dk4gnDZd3TefbtyG3i4qSoO7YoXZOP5q/edit?usp=sharing&ouid=108131427433094834232&rtpof=true&sd=true|Slide-uri curs]] |
* **Cuvinte cheie**: interfață, GUI, CLI, prompt, comandă, argumente, command completion, istoric de comenzi, shell, terminal, documentare, libreadline, ''>'', ''<'', ''&'', ''|'', ''||'', ''&&'', '';'', ''%%"%%'', %%'%%, ''\'', ''$'', one liner, variabile, variabile de mediu, escaping, expandare, globbing | * **Cuvinte cheie**: interfață, GUI, CLI, prompt, comandă, argumente, command completion, istoric de comenzi, shell, terminal, documentare, libreadline, ''>'', ''<'', ''&'', ''|'', ''||'', ''&&'', '';'', ''%%"%%'', %%'%%, ''\'', ''$'', one liner, variabile, variabile de mediu, escaping, expandare, globbing | ||
- | * [[http://elf.cs.pub.ro/uso/res/carte/uso_cap-07-cli.pdf|Capitolul 7 din carte: Interfața în linia de comandă]] | + | * **Suport de curs** |
- | * **Suport de vechi** | + | * [[https://github.com/systems-cs-pub-ro/carte-uso/releases | Utilizarea sistemelor de operare]] |
- | * [[http://books.google.com/books?id=_JFGzyRxQGcC | Introducere în sisteme de operare]] | + | * Secțiunea 7 - Interfata in linia de comanda |
- | * [[http://books.google.com/books?id=_JFGzyRxQGcC&pg=PA36 | Capitolul 12 - Shell scripting]] | + | |
- | * Secțiunile 12.1, 12.2, 12.6, 12.75 | + | |
- | * [[http://elf.cs.pub.ro/uso/res/carte/uso_cap-08-hw.pdf | Capitolul 8 Componente Hardware]] | + | |
- | * [[http://elf.cs.pub.ro/uso/res/carte/uso_cap-09-boot.pdf | Capitolul 9 Pornirea sistemului]] | + | |
- | * **Suport de vechi** | + | |
- | * [[http://books.google.com/books?id=_JFGzyRxQGcC | Introducere în sisteme de operare]] | + | |
- | * [[http://books.google.com/books?id=_JFGzyRxQGcC&pg=PA36 | Capitolul 12 - Shell scripting]] | + | |
- | * Secțiunile 12.1, 12.2, 12.6, 12.75 | + | |
+ | <HTML> | ||
+ | <center> | ||
+ | <iframe src="https://docs.google.com/presentation/d/e/2PACX-1vSgCQrUZIoJoRMvmJZZzYrrPCpdW2Id2_Rjv0rec-IKsKx0tkpXc8jnPS5NLCSq1g/embed?start=false&loop=false&delayms=3000" frameborder="0" width="480" height="389" allowfullscreen="true" mozallowfullscreen="true" webkitallowfullscreen="true"></iframe> | ||
+ | </center> | ||
+ | </HTML> | ||
/* | /* | ||
Line 36: | Line 33: | ||
*/ | */ | ||
+ | |||
+ | |||
+ | ===== Demo ===== | ||
+ | |||
+ | ==== Variabila PATH ==== | ||
+ | |||
+ | Dacă rulați în terminal comanda: | ||
+ | <code>echo $PATH</code> | ||
+ | Veți vedea o listă de directoare de genul: | ||
+ | <code>/home/student/.local/share/umake/bin:/home/student/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin</code> | ||
+ | Dacă puneți un executabil în oricare din aceste directoare, nu mai este nevoie să setați calea către executabil și puteți să îl rulați direct ca o comandă, folosind numele executabilului. | ||
+ | |||
+ | ==== Globbing ==== | ||
+ | |||
+ | <code> | ||
+ | student@myPc:~/USO/test$ ls | ||
+ | endian.c endian.o hello hello.c Makefile socket.c struct.c struct.o | ||
+ | student@myPc:~/USO/test$ ls *.c | ||
+ | endian.c hello.c socket.c struct.c | ||
+ | student@myPc:~/USO/test$ ls end?an.c | ||
+ | endian.c | ||
+ | student@myPc:~/USO/test$ ls [a-s]*.c | ||
+ | endian.c hello.c socket.c struct.c | ||
+ | student@myPc:~/USO/test$ ls *.{c,o} | ||
+ | endian.c endian.o hello.c socket.c struct.c struct.o | ||
+ | </code> | ||
+ | |||
+ | Mai sus avem câteva exemple de rulare a comenzii ls impreună cu expresii regulate. | ||
+ | |||
+ | Ce reprezintă fiecare operator? | ||
+ | * ***:** 0 sau mai multe caractere | ||
+ | * **+:** 1 sau mai multe caractere | ||
+ | * **?:** 0 sau 1 caracter | ||
+ | * **[a-s]:** orice literă de la "a" la "s" | ||
+ | * **[A-Za-z]:** orice literă, mică sau mare | ||
+ | * **{c,o}:** c sau o | ||
+ | |||
+ | ==== Escapări ==== | ||
+ | |||
+ | <code> | ||
+ | student@myPc:~/USO/test$ ls -l | ||
+ | total 0 | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:41 'ana are mere.txt' | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:18 endian.c | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:18 endian.o | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:00 hello | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:00 hello.c | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:00 Makefile | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:01 socket.c | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:00 struct.c | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:01 struct.o | ||
+ | student@myPc:~/USO/test$ ls ana are mere.txt | ||
+ | ls: cannot access 'ana': No such file or directory | ||
+ | ls: cannot access 'are': No such file or directory | ||
+ | ls: cannot access 'mere.txt': No such file or directory | ||
+ | student@myPc:~/USO/test$ la "ana are mere.txt" | ||
+ | 'ana are mere.txt' | ||
+ | student@myPc:~/USO/test$ ls 'ana are mere.txt' | ||
+ | 'ana are mere.txt' | ||
+ | student@myPc:~/USO/test$ ls ana\ are\ mere.txt | ||
+ | 'ana are mere.txt' | ||
+ | </code> | ||
+ | |||
+ | <code> | ||
+ | student@myPc:~/USO/test$ echo "Hello World" | ||
+ | Hello World | ||
+ | student@myPc:~/USO/test$ echo Hello World | ||
+ | Hello World | ||
+ | student@myPc:~/USO/test$ echo "Hello \"World\"" | ||
+ | Hello "World" | ||
+ | student@myPc:~/USO/test$ echo "Hello "World"" | ||
+ | Hello World | ||
+ | </code> | ||
+ | |||
+ | <code> | ||
+ | student@myPc:~/USO/test$ echo \" | ||
+ | " | ||
+ | student@myPc:~/USO/test$ echo "" | ||
+ | |||
+ | student@myPc:~/USO/test$ echo \"\" | ||
+ | "" | ||
+ | </code> | ||
+ | |||
+ | <note>Pentru ca un caracter special să fie utilizat ca un caracter obișnuit în bash, el trebuie să fie escapat.</note> | ||
+ | |||
+ | ==== Expandări ==== | ||
+ | |||
+ | <code> | ||
+ | student@myPc:~/USO/test/files$ touch $(seq -f "file-%02g.txt" 1 20) | ||
+ | student@myPc:~/USO/test/files$ touch file.txt | ||
+ | student@myPc:~/USO/test/files$ ls | ||
+ | file-01.txt file-04.txt file-07.txt file-10.txt file-13.txt file-16.txt file-19.txt | ||
+ | file-02.txt file-05.txt file-08.txt file-11.txt file-14.txt file-17.txt file-20.txt | ||
+ | file-03.txt file-06.txt file-09.txt file-12.txt file-15.txt file-18.txt file.txt | ||
+ | student@myPc:~/USO/test/files$ rm -rf $(seq -f "file-%02g.txt" 1 20) | ||
+ | student@myPc:~/USO/test/files$ ls | ||
+ | file.txt | ||
+ | </code> | ||
+ | |||
+ | <code> | ||
+ | student@myPc:~/USO/test/files$ a=6 | ||
+ | student@myPc:~/USO/test/files$ echo a | ||
+ | a | ||
+ | student@myPc:~/USO/test/files$ echo $a | ||
+ | 6 | ||
+ | student@myPc:~/USO/test/files$ echo $((a)) | ||
+ | 6 | ||
+ | student@myPc:~/USO/test/files$ a++ | ||
+ | |||
+ | Command 'a++' not found, did you mean: | ||
+ | |||
+ | command 'a+' from deb aplus-fsf | ||
+ | command 'g++' from deb g++ | ||
+ | command 'c++' from deb g++ | ||
+ | command 'c++' from deb clang | ||
+ | command 'c++' from deb libc++-helpers | ||
+ | command 'c++' from deb pentium-builder | ||
+ | command 'ac++' from deb aspectc++ | ||
+ | command 'ag++' from deb aspectc++ | ||
+ | |||
+ | Try: sudo apt install <deb name> | ||
+ | |||
+ | student@myPc:~/USO/test/files$ ((a++)) | ||
+ | student@myPc:~/USO/test/files$ echo $a | ||
+ | 7 | ||
+ | student@myPc:~/USO/test/files$ b=$((a-4)) | ||
+ | student@myPc:~/USO/test/files$ echo $b | ||
+ | 3 | ||
+ | </code> | ||
+ | |||
+ | |||
+ | ==== cat și redirectarea ==== | ||
+ | |||
+ | <code> | ||
+ | student@myPc:~/USO/test/files$ echo Ana are mere > test.txt | ||
+ | student@myPc:~/USO/test/files$ cat test.txt | ||
+ | Ana are mere | ||
+ | student@myPc:~/USO/test/files$ echo "Ana are mere 2" >> test.txt | ||
+ | student@myPc:~/USO/test/files$ cat test.txt | ||
+ | Ana are mere | ||
+ | Ana are mere 2 | ||
+ | </code> | ||
+ | |||
+ | <code> | ||
+ | student@myPc:~/USO/test/files$ id student &> /dev/null && echo "da" || echo "nu" | ||
+ | da | ||
+ | student@myPc:~/USO/test/files$ id student &> /dev/null && echo "da" || echo "nu" | ||
+ | nu | ||
+ | </code> | ||
+ | |||
+ | |||
+ | ==== tac, rev și nl ==== | ||
+ | |||
+ | <code> | ||
+ | student@myPc:~/USO/test/files$ echo "Ana are mere 2" >> test.txt | ||
+ | student@myPc:~/USO/test/files$ ls | ||
+ | test.txt | ||
+ | student@myPc:~/USO/test/files$ tac test.txt | ||
+ | Ana are mere 2 | ||
+ | Ana are mere | ||
+ | student@myPc:~/USO/test/files$ cat test.txt | ||
+ | Ana are mere | ||
+ | Ana are mere 2 | ||
+ | student@myPc:~/USO/test/files$ rev test.txt | ||
+ | erem era anA | ||
+ | 2 erem era anA | ||
+ | student@myPc:~/USO/test/files$ nl test.txt | ||
+ | 1 Ana are mere | ||
+ | 2 Ana are mere 2 | ||
+ | </code> | ||
+ | |||
+ | <note> | ||
+ | * cat → afișeaza conținutul fișierului | ||
+ | * tac → afișeaza conținutul fișierului în ordinea inversă a liniilor | ||
+ | * rev → afișeaza liniile inversate (primul element de pe linie devine ultimul) | ||
+ | * nl → afișeaza numărul liniei | ||
+ | </note> | ||
+ | |||
+ | ==== head și tail ==== | ||
+ | |||
+ | <code> | ||
+ | student@myPc:~/USO/test/files$ cat test.txt | ||
+ | Ana are mere | ||
+ | Ana are mere 2 | ||
+ | Ana are mere 3 | ||
+ | Maria are pere | ||
+ | Ana are mere | ||
+ | student@myPc:~/USO/test/files$ cat test.txt | head -n 2 | ||
+ | Ana are mere | ||
+ | Ana are mere 2 | ||
+ | student@myPc:~/USO/test/files$ cat test.txt | tail -n 2 | ||
+ | Maria are pere | ||
+ | Ana are mere | ||
+ | </code> | ||
+ | |||
+ | <note> | ||
+ | * head -n 2 → afișează primele 2 linii din fisier | ||
+ | * tail -n 2 → afișează ultimele 2 linii din fisier | ||
+ | </note> | ||
+ | |||
+ | ==== sort, uniq și wc ==== | ||
+ | |||
+ | <code> | ||
+ | student@myPc:~/USO/test/files$ cat test.txt | ||
+ | Ana are mere | ||
+ | Ana are mere 2 | ||
+ | Ana are mere 3 | ||
+ | Maria are pere | ||
+ | Ana are mere | ||
+ | student@myPc:~/USO/test/files$ sort test.txt | ||
+ | Ana are mere | ||
+ | Ana are mere | ||
+ | Ana are mere 2 | ||
+ | Ana are mere 3 | ||
+ | Maria are pere | ||
+ | student@myPc:~/USO/test/files$ sort -r -n test.txt | ||
+ | Maria are pere | ||
+ | Ana are mere 3 | ||
+ | Ana are mere 2 | ||
+ | Ana are mere | ||
+ | Ana are mere | ||
+ | student@myPc:~/USO/test/files$ wc test.txt | ||
+ | 5 17 71 test.txt | ||
+ | student@myPc:~/USO/test/files$ wc -c test.txt | ||
+ | 71 test.txt | ||
+ | student@myPc:~/USO/test/files$ wc -l test.txt | ||
+ | 5 test.txt | ||
+ | student@myPc:~/USO/test/files$ sort test.txt > test22.txt | ||
+ | student@myPc:~/USO/test/files$ cat test22.txt | ||
+ | Ana are mere | ||
+ | Ana are mere | ||
+ | Ana are mere 2 | ||
+ | Ana are mere 3 | ||
+ | Maria are pere | ||
+ | student@myPc:~/USO/test/files$ uniq test22.txt | ||
+ | Ana are mere | ||
+ | Ana are mere 2 | ||
+ | Ana are mere 3 | ||
+ | Maria are pere | ||
+ | </code> | ||
+ | |||
+ | <note> | ||
+ | * sort → sortează intrările | ||
+ | * uniq → elimină duplicatele din fișier | ||
+ | * wc → numară elementele din fisier | ||
+ | </note> | ||
+ | |||
+ | ==== cut și tr ==== | ||
+ | |||
+ | <code>student@myPc:~/USO/test$ ls -l | ||
+ | total 4 | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:41 'ana are mere.txt' | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:18 endian.c | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:18 endian.o | ||
+ | drwxr-xr-x 2 student student 4096 nov 8 18:36 files | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:00 hello | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:00 hello.c | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:00 Makefile | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:01 socket.c | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:00 struct.c | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:01 struct.o | ||
+ | student@myPc:~/USO/test$ ls -l | tr -d '.' | ||
+ | total 4 | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:41 ana are meretxt | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:18 endianc | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:18 endiano | ||
+ | drwxr-xr-x 2 student student 4096 nov 8 18:36 files | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:00 hello | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:00 helloc | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:00 Makefile | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:01 socketc | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:00 structc | ||
+ | -rw-r--r-- 1 student student 0 nov 8 17:01 structo | ||
+ | student@myPc:~/USO/test$ ls -l | tr -d ' ' | ||
+ | total4 | ||
+ | -rw-r--r--1studentstudent0nov817:41anaaremere.txt | ||
+ | -rw-r--r--1studentstudent0nov817:18endian.c | ||
+ | -rw-r--r--1studentstudent0nov817:18endian.o | ||
+ | drwxr-xr-x2studentstudent4096nov818:36files | ||
+ | -rw-r--r--1studentstudent0nov817:00hello | ||
+ | -rw-r--r--1studentstudent0nov817:00hello.c | ||
+ | -rw-r--r--1studentstudent0nov817:00Makefile | ||
+ | -rw-r--r--1studentstudent0nov817:01socket.c | ||
+ | -rw-r--r--1studentstudent0nov817:00struct.c | ||
+ | -rw-r--r--1studentstudent0nov817:01struct.o | ||
+ | student@myPc:~/USO/test$ ls -l | tr -d '-' | ||
+ | total 4 | ||
+ | rwrr 1 student student 0 nov 8 17:41 ana are mere.txt | ||
+ | rwrr 1 student student 0 nov 8 17:18 endian.c | ||
+ | rwrr 1 student student 0 nov 8 17:18 endian.o | ||
+ | drwxrxrx 2 student student 4096 nov 8 18:36 files | ||
+ | rwrr 1 student student 0 nov 8 17:00 hello | ||
+ | rwrr 1 student student 0 nov 8 17:00 hello.c | ||
+ | rwrr 1 student student 0 nov 8 17:00 Makefile | ||
+ | rwrr 1 student student 0 nov 8 17:01 socket.c | ||
+ | rwrr 1 student student 0 nov 8 17:00 struct.c | ||
+ | rwrr 1 student student 0 nov 8 17:01 struct.o | ||
+ | </code> | ||
+ | |||
+ | <code>student@myPc:~/USO/test/files$ cat test.txt | ||
+ | Ana are mere | ||
+ | Ana are mere 2 | ||
+ | Ana are mere 3 | ||
+ | Maria are pere | ||
+ | Ana are mere | ||
+ | student@myPc:~/USO/test/files$ cat test.txt | cut -d' ' -f1 | ||
+ | Ana | ||
+ | Ana | ||
+ | Ana | ||
+ | Maria | ||
+ | Ana | ||
+ | student@myPc:~/USO/test/files$ cat test.txt | cut -d' ' -f1,3 | ||
+ | Ana mere | ||
+ | Ana mere | ||
+ | Ana mere | ||
+ | Maria pere | ||
+ | Ana mere | ||
+ | </code> | ||
+ | |||
+ | <note> | ||
+ | * tr → elimină caractere | ||
+ | * cut → afișeaza doar liniile pe care le dăm de la tastatură | ||
+ | </note> |