Bash Flashcards

1
Q

retrieve the value of the variable x

A

$x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

explicitly declare variable to integer or array

A

declare -i i

declare -a A

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

print string including variable x

A

echo “Hei, $x”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

command line arguments

A

$#
$1, $2, $3…
$@ for all

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

exit status of the last executed command

A

$?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

comparisons and if-statements

A

Integer comparison (but don’t use it)
if [ $i -eq 10 ]; then

fi

String comparison
if[ “$name” == “Henrik” ]; then

fi

Solution: Assume all variables are strings and use double quotes when comparing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

verbose

A

!/bin/sh -x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

list files in tree

A

ls -R # –Recursive

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

search

A

fgrep [pattern] [file] -r # fixed
grep [pattern] [file] -r # regex

ls | grep ^A

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

store result of command as a variable

A

time = $(date)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

list files sorted

A

ls -s | sort -r

ls -Ss

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

if/else-statements

A
if [ -a $path ]; then
    ...
elif [ -d $path ]; then
    ...
else
    ...
fi
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

write to file from stdin

A

cat > file.txt

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

while-loop

read command arguments one by one

A
while [ $# -gt 0 ]
do
    print $1;
    shift;
done
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

append to file

A

cat appendThis&raquo_space; toThis

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

for-loop removing all PDF-files

A

files = ‘ls *.pdf’

for file in $files
do
rm -f $file
done

17
Q

arithmetic expressions

A

((counter++))

declare -i i
for ((i=0; i < $n; i++)); do

done

18
Q

search for files and run a command for each file

A
find dir -name *.txt -exec cat {} \;
# {} is the current filename
19
Q

man find

A
  • name ‘*.pdf’
  • type f
  • size +200
  • exec … {} \;
  • atime +90 # days
  • print
20
Q

pack files into one file

A

tar -cvf files.tar mytree file1 file2

21
Q

unpack tar

A

tar -xvf files.tar

22
Q

list new files

A

declare -i minuteLimit = “$2*3600”

find $1 -type f -mmin -$minuteLimit -print | xargs ls -1Ssk

23
Q

remove large files

A

find $1 -type f -size +$2k -print -exec rm {} \;

24
Q

sort file into another file

A

sort $1 -o $2