Curs 05 - Interfața în linia de comandă

Demo

Variabila PATH

Dacă rulați în terminal comanda:

echo $PATH

Veți vedea o listă de directoare de genul:

/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

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

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

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

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'
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
student@myPc:~/USO/test$ echo \"
"
student@myPc:~/USO/test$ echo ""

student@myPc:~/USO/test$ echo \"\"
""

Pentru ca un caracter special să fie utilizat ca un caracter obișnuit în bash, el trebuie să fie escapat.

Expandări

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
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

cat și redirectarea

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
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

tac, rev și nl

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

  • 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

head și tail

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

  • head -n 2 → afișează primele 2 linii din fisier
  • tail -n 2 → afișează ultimele 2 linii din fisier

sort, uniq și wc

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

  • sort → sortează intrările
  • uniq → elimină duplicatele din fișier
  • wc → numară elementele din fisier

cut și tr

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
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

  • tr → elimină caractere
  • cut → afișeaza doar liniile pe care le dăm de la tastatură

uso/cursuri/curs-05.txt · Last modified: 2020/11/09 11:34 by bogdan_ionut.jidovu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0