Shell Workshop

fhLUG

Linux User Group FH Hagenberg

ls -R


.:
man   cat    less   grep   sed   find   xargs   echo
sort  uniq   wc

./sh:
cd                 io redirect   pipes   variables   exit codes
cmd substitution   if            for     while       wildcards   

man workshop

workshop(1)                               fhLUG

NAME
	workshop – a little bit of shell scripting

SYNOPSIS
	workshop [questions ...]

DESCRIPTION
	This workshop should give a rough overview about the
	possibilities one has with shell scripting to automate tasks and make
	life generally easier.

	If executed without questions it will just output a bunch of facts
	which will become boring rather quickly. If passed one or more
	questions it tries to answer them in a funny but interesting way on stdout.

EXIT STATUS
	0 This command returns successfully most of the time.

	1 This command should not fail.

	2 This is an indication that something is seriously wrong.

SEE ALSO
	More information can be found at http://fhLUG.at.

HISTORY
	This is the first time this workshop was held.

BUGS
	Exit status might not be correct.

fhLUG                                     June, 2012

Command basics

$ ls                  # list directory contents
$ ls -l               # long information
$ grep pattern file   # grep in file
$ grep pattern        # grep in stdin

cat file

This is the content of the file. It is simply printed to stdout (the terminal).
`tac' prints a file's lines in reverse order

Other ways to display a file's contents is `more' and
`less'. They even allow you to scroll through a file.

grep 'scroll' file

They even allow you to scroll through a file.

ls | grep '^.a'


man
cat
xargs

Regular expressions (man grep)

Basic (grep -G):
	.		Any single character
	\.		A single period character
	[ab1-5]		Any character inside the brackets
	^ $		Match beginning and end of line, respectively

Extended (grep -E):
	? * +		Optional, zero or more, one or more
	{n,m}		Match at least n, no more than m times
	(...)		Group patterns

sed 's/[tT]erminal/console/' file

This is the content of the file. It is simply printed to stdout (the console).
...

find . -name 'w*'

./wc
./sh/while
./sh/wildcards

find . -type d -print0 | xargs -0 echo "Directories: "

Directories: . ./sh

echo 'Hello World'

Hello World

ls | sort

cat
echo
find
grep
less
man
sed
sort
uniq
wc
xargs

cat some_file | sort | uniq


1. `uniq'
2. can
3. only
4. work
5. with
6. sorted
7. files.
8. 
9. `-c' prints a count for each line

wc file

 5  43 234

shell

Changing directories

~$ cd dir/subdir
~/dir/subdir$ cd ..
~/dir$ cd -
~/dir/subdir$ cd
~$

IO redirect

$ ls > dir_listing
$ cat < dir_listing
$ find 2> errors
$ find 2> /dev/null

Pipes

$ cat file | sort | uniq | wc -l
$ sort -u file | wc -l

Variables

$ VAR=value
$ echo "We have stored: $VAR"
We have stored: value
$ echo 'Our variable: $VAR'
Our variable: $VAR
$ ls "$HOME"
...

Exit codes

$ ls
$ echo $?
0

Command substitution

$ echo $(date)
$ echo `date`

Conditionals

$ if [ $? -eq 0 ]; then
	echo yay!
else
	echo meh.
fi

if read -p 'Enter name: ' name; then
	echo "Name: $name"
fi

Loops: while

$ while read -p '> ' line; do
	echo "You entered: $line"
done

Loops: for

$ for word in a b c 'd e f'; do
	echo "Word: $word"
done

$ for i in $(seq 1 100); do
	echo "i = $i"
done

Wildcards

$ ls a*
$ ls a???
$ echo *.txt