Bash Flashcards
retrieve the value of the variable x
$x
explicitly declare variable to integer or array
declare -i i
declare -a A
print string including variable x
echo “Hei, $x”
command line arguments
$#
$1, $2, $3…
$@ for all
exit status of the last executed command
$?
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
verbose
!/bin/sh -x
list files in tree
ls -R # –Recursive
search
fgrep [pattern] [file] -r # fixed
grep [pattern] [file] -r # regex
ls | grep ^A
store result of command as a variable
time = $(date)
list files sorted
ls -s | sort -r
ls -Ss
if/else-statements
if [ -a $path ]; then ... elif [ -d $path ]; then ... else ... fi
write to file from stdin
cat > file.txt
while-loop
read command arguments one by one
while [ $# -gt 0 ] do print $1; shift; done
append to file
cat appendThis»_space; toThis