This is an old revision of the document!


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

Demo

Variabila PATH

Daca rulati in terminal comanda:

echo $PATH

Veti vedea o lista 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

Daca puneti un executabil in oricare din aceste directoare, nu mai este nevoie sa setati calea catre executabil si puteti sa il rulati direct ca o comanda, 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 cateva exemple de rulare a comenzii ls impreuna 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 sa fie utilizat in bash, el trebuie sa 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

Notatiile de tipul $((…)) evalueaza expresiile aritmetice in bash. </note>

==== 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 → afiseaza continutul fisierului
  * tac → afiseaza continutul fisierului in ordine inversa a liniilor
  * rev → afiseaza liniile inversate (primul element de pe linie devine ultimul)
  * nl → afiseaza numarul 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>

uso/cursuri/curs-05.1604866935.txt.gz · Last modified: 2020/11/08 22:22 by ebru.resul
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