1. Homepage of Dr. Zoltán Porkoláb
    1. Home
    2. Archive
  2. Teaching
    1. Timetable
    2. Multiparadigm programming (MSc)
    3. C programming (BSc for physicists)
    4. Project tools (BSc)
    5. Bolyai College
    6. C++ (for foreign studenst)
    7. Software technology lab
    8. BSc and MSc thesis
  3. Research
    1. Templight
    2. CodeChecker
    3. CodeCompass
    4. Projects
    5. Publications (up to 2011)
    6. PhD students
  4. Affiliations
    1. Dept. of Programming Languages and Compilers
    2. Ericsson Hungary Ltd

Shell programming

In UNIX systems, the command interpreter is just an ordinary program, reading lines from the standard input – and writes to standard output and standard error.

Such command are called as filter in UNIX.

There are various command interpreters – shells – in UNIX systems:

  • sh (Bourne shell)
  • csh (C shell)
  • ash (Almquist shell)
  • ksh (Korn shell)
  • zsh (Z shell)
  • bash (Bourne-again shell)

Configuration

/etc/profile
~/.profile
~/.bash_profile
~/.bash_login
~/.bashrc

Basics

Metacharacter and escaping

* all files (except when starting with .),

\ escaping:

$ echo \#

$ echo *
arch bin Desktop Documents Downloads ftp LXSeries Music Pictures Public Templates tmp Videos work

$ echo \*
*

$ echo '**'
**

$ echo "**"
**

Command arguments

$ echo hello world
hello world
$ echo hello      world
hello world
$ echo 'hello      world'
hello      world

Exit code:

$ echo $?
0
$ ls -l | grep '^d' | grep  xftp 
$ echo $?
1

Structure of commands

Pipeline

$ echo hello world | grep 'o w'
hello world
$ ls -l
összesen 2440
drwxr-xr-x 10 gsd users    4096 jan 16 21.31 arch
drwxr-xr-x  4 gsd users    4096 jan 29 21.52 Asztal
drwxr-xr-x  2 gsd users    4096 szept 22 02.09 bin
drwxr-xr-x  3 gsd users    4096 jan 23 23.14 books
drwxr-xr-x  2 gsd users    4096 jan 23 23.17 Calibre Library
-rw-r--r--  1 gsd users 2083012 jan 11 00.50 dell_hspa_config.pdf
drwxr-xr-x  3 gsd users    4096 febr 20 18.54 Dokumentumok
-rw-r--r--  1 gsd users     550 jan 30 19.19 d.txt
drwxr-xr-x  7 gsd users    4096 febr 18 23.41 ftp
drwxr-xr-x  3 gsd users    4096 2007 jún 21 ICAClient
drwxr-xr-x  4 gsd users    4096 jan 29 22.37 Képek
-rw-r--r--  1 gsd users  341213 febr  6 14.32 Kőzettan.pdf
drwxr-xr-x  2 671 users    4096 okt  3 21.20 Letöltések
drwxr-xr-x  2 gsd users    4096 szept 22 02.17 Nyilvános
drwxr-xr-x  2 gsd users    4096 szept 22 02.09 public_html
drwxr-xr-x  2 gsd users    4096 szept 22 02.17 Sablonok
drwxr-xr-x  6 gsd users    4096 febr 13 20.38 Videók
drwxr-xr-x  5 gsd users    4096 jan 26 23.08 work
drwxr-xr-x  3 gsd users    4096 okt  1 23.46 Zenék
$ ls -l | grep '^d' | wc -l
16
$ ls -l | grep '^d' | grep ftp | wc -l
1

Logical operations

|| &&

$ ls -l | grep '^d' | grep  xftp || echo false
false

$ ls -l | grep '^d' | grep  ftp && echo true
true

$ test -d zz || mkdir zz

Sequence and backgrounding

; &

$ sleep 10; echo kesz 
kesz
$ sleep 10 & echo kesz 
[1] 7802
kesz
$ sleep 10 & echo kesz; ps 
[2] 7805
kesz
 PID TTY          TIME CMD
4237 pts/0    00:00:00 bash
7805 pts/0    00:00:00 sleep
7806 pts/0    00:00:00 ps
$ (sleep 10 ; echo kesz)& ps 
[3] 7809
 PID TTY          TIME CMD
4237 pts/0    00:00:00 bash
7809 pts/0    00:00:00 bash
7810 pts/0    00:00:00 ps
7811 pts/0    00:00:00 sleep
$ (sleep 10 ; echo kesz)& ps >out
$ ((sleep 10 ; echo kesz)& ps) >out

Shell variables

Pre-defined variables

$?  exit code

$!  last background process pid

$0 ... $9  shell parameters

$#  number of parameters

$*  all parameters
 
"$@" all parameters

$x  x variable (PATH, HOME, etc...)

Shell variables and subshells

$ i=5
$ echo $i
5
$ bash
$ echo $i

$ i=8
$ exit
exit
$ echo $i
5
$ 
export j=6
$ echo $j
6
$ bash
$ echo $j
6

Source

$ cat >x.sh
X=99
$ echo $X

$ sh x.sh 
$ echo $X

$ source x.sh 
$ echo $X
99

Alternatively one can use the . command.

Quoting

$ i=0
$ echo expr $i + 1
expr 0 + 1

$echo i=expr $i + 1
i=expr 0 + 1

echo i=`expr $i + 1`
i=1
$ echo `ls -l`
összesen 2440 drwxr-xr-x 10 gsd users 4096 jan 16 21.31 arch drwxr-xr-x 4 gsd users 4096 jan 29 21.52 Asztal drwxr-xr-x 2 gsd users 4096 szept 22 02.09 bin drwxr-xr-x 3 gsd users 4096 jan 23 23.14 books drwxr-xr-x 2 gsd users 4096 jan 23 23.17 Calibre Library -rw-r--r-- 1 gsd users 2083012 jan 11 00.50 dell_hspa_config.pdf drwxr-xr-x 3 gsd users 4096 febr 20 18.54 Dokumentumok -rw-r--r-- 1 gsd users 550 jan 30 19.19 d.txt drwxr-xr-x 7 gsd users 4096 febr 18 23.41 ftp drwxr-xr-x 3 gsd users 4096 2007 jún 21 ICAClient drwxr-xr-x 4 gsd users 4096 jan 29 22.37 Képek -rw-r--r-- 1 gsd users 341213 febr 6 14.32 Kőzettan.pdf drwxr-xr-x 2 671 users 4096 okt 3 21.20 Letöltések drwxr-xr-x 2 gsd users 4096 szept 22 02.17 Nyilvános drwxr-xr-x 2 gsd users 4096 szept 22 02.09 public_html drwxr-xr-x 2 gsd users 4096 szept 22 02.17 Sablonok drwxr-xr-x 6 gsd users 4096 febr 13 20.38 Videók drwxr-xr-x 5 gsd users 4096 jan 26 23.08 work drwxr-xr-x 3 gsd users 4096 okt 1 23.46 Zenék

Alternative formats

$ echo $(ls -l)
$ eval $(echo  ls )