Bash Flashcards Preview

INF3331 > Bash > Flashcards

Flashcards in Bash Deck (24)
Loading flashcards...
1

retrieve the value of the variable x

$x

2

explicitly declare variable to integer or array

declare -i i
declare -a A

3

print string including variable x

echo "Hei, $x"

4

command line arguments

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

5

exit status of the last executed command

$?

6

comparisons and if-statements

# 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

7

verbose

#!/bin/sh -x

8

list files in tree

ls -R # --Recursive

9

search

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

ls | grep ^A

10

store result of command as a variable

time = $(date)

11

list files sorted

ls -s | sort -r
ls -Ss

12

if/else-statements

if [ -a $path ]; then
...
elif [ -d $path ]; then
...
else
...
fi

13

write to file from stdin

cat > file.txt

14

while-loop
read command arguments one by one

while [ $# -gt 0 ]
do
print $1;
shift;
done

15

append to file

cat appendThis >> toThis

16

for-loop removing all PDF-files

files = 'ls *.pdf'

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

17

arithmetic expressions

((counter++))

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

18

search for files and run a command for each file

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

19

man find

-name '*.pdf'
-type f
-size +200
-exec ... {} \;
-atime +90 # days
-print

20

pack files into one file

tar -cvf files.tar mytree file1 file2

21

unpack tar

tar -xvf files.tar

22

list new files

declare -i minuteLimit = "$2*3600"
find $1 -type f -mmin -$minuteLimit -print | xargs ls -1Ssk

23

remove large files

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

24

sort file into another file

sort $1 -o $2