Teaching:
Feedback
3 | pwd; ls |
4 | cd Desktop && ls |
5 | ping put.poznan.pl Ctrl+Z jobs fg Ctrl+C |
6 | ping put.poznan.pl & # that command will spam the screen with its results, but the shell still normally accepts commands jobs fg Ctrl+Z bg |
7 | echo $VAR VAR=text echo $VAR |
8 | OTHER=$VAR echo $OTHER |
9 | SIZE=5 echo "The file size is ${SIZE}MB" |
11 | #!/bin/sh echo "First arg: $1" echo "Third arg: $3" |
12 | #!/bin/sh echo "This script has been called as $0 and has $# arguments." |
13 | LANG=ja_JP.UTF-8 date vim :q LANG=de_DE.UTF-8 rm -rf /root/.ssh/nope lscpu |
14 | PS1="\w> " PS1="\[\e[1;30m\][\[\e[33m\]\u\[\e[30m\]@\[\e[34m\]\h\[\e[0;1m\] \w\[\e[30m\]]\$\[\e[0m\] " # (There are plenty prompt generators in the Internet) |
15 | lspci PATH="/sbin:/usr/sbin:$PATH" lspci |
16 | X=42 Y=42 X=$((X+1)) Y=$((Y+2)) Z=$((X*Y)) echo $((Z%128)) |
# You may also abuse the syntax and run: echo $((X=43, Y=42, X=X+1, Y=Y+2, Z=X*Y, Z%128)) | |
17 | echo "In the current directory there are $(ls | wc -l) files" |
18 | pstree -au > processes_$(date +%H_%M_%S).log |
19 | START=$(date +%s%N); sleep 1; END=$(date +%s%N); echo $((END-START)) |
20 | alias year="cal -my" year 2025 |
Exercise 21
#!/bin/sh echo "What is your name:" read NAME echo "Hello $NAME!"
Exercise 22
#!/bin/sh read ONE TWO echo $((ONE+TWO))
Exercise 23
[ -d dirname ] && cd dirname
X=1 Y=2 [ "$X" -gt "$Y" ] || echo "Y≥X"
Exercise 24
if rm filename then echo "Removed the file" else echo "Failed to remove the file" fi
Exercise 25
#!/bin/sh if [ $# -ne 2 ] then echo "Wrong number of arguments" exit 1 fi if [ "$1" -le "$2" ] then : else # notice that 'else' branch is executed also for non-number arguments; [ "$1" -gt "$2" ] would fail for non-numeric arguments echo "the first argument must be lesser then or equal to the second argument" exit 1 fi
Exercise 26
#!/bin/sh case "$1" in *.pdf) pdftotext "$1" -;; *.zip) unzip -l "$1";; *) cat "$1";; esac
Exercise 27
#!/bin/sh for VAR in "$@" # 'in "$@"' can be left out, as "$@" is the default argument list for the for loop do echo "=== $VAR ===" head -n1 "$VAR" tail -n1 "$VAR" done
Exercise 28
#!/bin/sh for POW in $(seq ${1:-20}) do echo "2**$POW=$((2**POW))" done
Exercise 29
#!/bin/sh E=1 V=2 while ((V<${1:-10000})) do echo "2**$E=$V" V=$((V*2)) E=$((E+1)) done
Exercise 30
#!/bin/sh NUM=0 while [ -e myProg_$NUM.log ] do NUM=$((NUM+1)) done LOG=myProg_$NUM.log date > $LOG
Exercise 31
user@host ~ $ myFunction(){ echo "Hello world!"; } user@host ~ $ myFunction Hello world!
Exercise 32
user@host ~ $ repeatText(){ for I in $(seq $1); do echo "$2"; done; } user@host ~ $ repeatText 3 "Example text." Example text. Example text. Example text.
Exercise 33
myTree(){ ls -A "$1" 2>/dev/null | while read F do echo "$2$F" [ ! -L "$1/$F" -a -d "$1/$F" ] && myTree "$1/$F" " $2" done }
Exercise 34
user@host ~ $ trap "fortune" INT user@host ~ $ ^C Falling rock. user@host ~ $
Exercise 35
#!/bin/sh TEMPFILE=$(mktemp) trap "echo \"Cleaning up the temporary file $TEMPFILE\"; rm $TEMPFILE" exit echo "Created a temporary file $TEMPFILE" date > "$TEMPFILE" sleep 12s date >> "$TEMPFILE"