Operating systems 1 -- 2008-2009 -- info.uvt.ro/Laboratory 3

From Wikiversity

Quick links: front; laboratories agenda, 1, 2, 3, 4, 5, 6, projects, evaluation, tools, references.



  • in order to create a variable (or to assign a new value to an existing one):
b='abc 123'
  • do not leave spaces between the = sign and either the name or the value:
a = abc123
  • in order to destroy a variable:
unset a
unset b


  • just by using the name of the variable will not yield its value:
echo a b c
  • you have to use the special form $name or ${name}:
    • variables that do not exist are substituted with the empty string;
echo $a $b $c
echo ${a} ${b} ${c}
  • by using the $name form you can not concatenate the value with other alpha-numeric characters (you have to use the ${name} variant):
echo z$az z$bz z$cz
echo z${a}z z${b}z z${c}z
  • by using the " quoting style -- called weak quoting -- the variables are still substituted:
echo "$a" "$b" "$c"
echo "${a}" "${b}" "${c}"
  • by using the ' quoting style --- called strong quoting or full quoting -- the variables are not substituted:
echo '$a' '$b' '$c'
echo '${a}' '${b}' '${c}'

Default defined (internal) variables[edit]

  • examples (of important variables):
echo "${SHELL}"
echo "${UID}"
echo "${PWD}"
echo "${HOME}"
echo "${EDITOR}"
echo "${PAGER}"
echo "${HOSTNAME}"
  • to see all the defined variables use:

Environment variables[edit]

  • example:
export EDITOR=mcedit
export PATH="${PATH}:~/bin"
  • to see all the exported variables use:

Positional variables[edit]

  • create a new script:
nano ./test.sh
  • fill it with the following content:

echo '${0}='"${0}"
echo '${#}='"${#}"
echo '${1}='"${1}"
echo '${2}='"${2}"
echo '${*}='"${*}"
echo '${@}='"${@}"
  • make it executable:
chmod +x ./test.sh
  • execute it with different parameters:
./test.sh a
./test.sh a b
./test.sh a b c
./test.sh a b c d
./test.sh 'a b c' d e

Pipes and redirections[edit]

  • syntax for redirection:
<command> [argument] ... > <output-file>
<command> [argument] ... < <input-file>
<command> [argument] ... < <input-file> > <output-file>
  • syntax for pipes:
<command-1> [argument] ... { | <command-2> [argument] ... } ...
  • examples:
    • saving the listing of all files (recursively) into a file for later processing:
find >./listing.txt
    • sorting the listing:
sort <./listing.txt
    • saving the sorted listing into another file:
sort <./listing.txt >./sorted-listing.txt
    • the same as above but in one step:
find | sort >./sorted-listing.txt
    • listing all files in current directory sorted by name:
ls | sort

Basic filter commands[edit]


  • syntax:
wc [flag] ... [file] ...
  • examples:
    • displaying the number of lines, words and characters in a file:
wc /etc/group
    • displaying the number of files inside the /etc folder:
ls /etc | wc -l
  • important flags:
    • -l or --lines;
    • -w or --words;
    • -m or --chars;
  • references:

cat and tac[edit]

  • syntax:
cat [file] ...
tac [file] ...
  • examples:
    • concatenating some files:
cat /etc/passwd /etc/group
    • displaying a file with the lines in reverse order:
tac /etc/group

head and tail[edit]

  • syntax:
head [flag] ... [file]
tail [flag] ... [file]
  • examples:
    • displaying the first couple (default is 10) lines of a file:
head /etc/group
    • displaying the first 2 lines:
head -n 2 /etc/group
    • displaying the last two files in the current directory:
ls | tail -n 2
  • important flags:
    • -n or --lines;
    • -f or --follow (only for tail) used to continuously monitor the file contents;
  • references:


  • syntax:
sort [flag] ... [file] ...
  • examples:
    • sort a file:
sort /etc/passwd
  • important flags:
    • -f or --ignore-case;
    • -n or --numeric-sort;
    • -u or --unique;
    • -r or --reverse;
  • references:


  • syntax:
grep <flag> ... <pattern> [file]
  • examples:
    • display the line containing the word root:
grep root /etc/passwd
    • display the files that contain the word bash:
find / | grep bash
    • display the files that end in the word .sh:
find / | grep -E -e '\.sh$'
  • important flags:
    • -n or --line-number;
    • -o or --only-matching;
    • -v or --invert-match;
    • -c or --count;
    • -i or --ignore-case;
    • -E or --extended-regexp;
    • -G or --basic-regexp;
    • -e;
  • references:


  • syntax:
tr [flag] ... <set-1> [set-2]
  • examples:
    • replacing double spaces with a single space:
nano ./text.txt
tr -s ' ' <./text.txt >./text-tr.txt
cat ./text-tr.txt
    • replacing digits with letters from a to j:
nano ./text.txt
tr 0-9 a-j <./text.txt >./text-tr.txt
  • important flags:
    • -c or --complement;
    • -d or --delete;
    • -s --squeeze-repeats;
  • references:


  • syntax:
    • command:
sed [flag] ... { -e <operator> } ... [file] ...
    • operator:
    • address:
  • examples:
    • remove all lines containing root:
sed '/root/d' </etc/group
    • replace all occurrences of the word root with ADMIN:
sed -e 's/root/ADIMN/g' </etc/group
    • print only thoes lines starting with a:
sed -n -r -e '/^a/p' </etc/group
    • print the first 10 lines:
sed -n -e '1,10p' </etc/group
sed -n -e '1,+9p' </etc/group


  • write a script that takes as an argument a file path, and moves it to a special recycle bin folder (located in /tmp) (hint: use mv):
stat ./test.sh
./safe-delete.sh ./test.sh
stat ./test.sh
stat /tmp/recycle/test.sh
  • write a script that takes as arguments a folder path and a pattern, and it prints all file names containing the given pattern, in an reversed order (hint: use find, grep, sort and / or tac):
./pattern-find.sh /etc bash
  • write a script that takes as an argument a file, and modifies its content by replacing every space with a _ character (hint: use tr, but be careful as you need an intemediary file, and you must use redirection):
nano ./text.txt
./underscore.sh ./text.txt
cat ./text.txt
  • write a script resembling the previous one, but this time it makes all characters upper-case:
nano ./text.txt
./uppercase.sh ./text.txt
cat ./text.txt
  • write a script that takes as an argument a file, and writes a list of all the (unique) words appearing in it, the case-ing does not matter (hint: use grep with -o and -E arguments, and sort):
nano ./text.txt
./words.sh ./text.txt
  • write a script that takes no arguments, but instead encrypts the standard input with the Rot13 algorithm (hint: use tr):
nano ./text.txt
./rot13.sh <./text.txt >./text-enc.txt
cat ./text-enc.txt