lk/basics/conditionals.md

84 lines
1.1 KiB
Markdown
Raw Normal View History

2022-01-16 18:20:39 +00:00
---
title: "conditionals"
2022-01-26 21:29:48 +00:00
tags: [ "Documentation", "Basics" ]
2022-01-16 18:20:39 +00:00
---
2020-01-02 00:04:35 +00:00
# If statements
Test statement equality as so:
```
read t1
read t2
if test $t1 != $t2; then
echo 'variables do not match'
else
echo 'variables match'
fi
exit 0
```
# Case Structure
These deal with multiple states rather than forking conditions.
Example:
```
#!/bin/bash
# Simple case demonstration
echo "What's your favourite creature?"
read CRE
case $CRE in
human | humanoids ) echo "Why is $CRE always standard?"
;;
troll | monsters ) echo "Not exactly known for their character ..."
;;
owlbears | monsters ) echo "Really you're a wizard fan"
;;
esac
# While and Until
2021-05-15 14:41:45 +00:00
This prints from 1 until 9.
2020-01-02 00:04:35 +00:00
2021-05-15 14:41:45 +00:00
> COUNTER=1
2020-01-02 00:04:35 +00:00
2021-05-15 14:41:45 +00:00
> while [ $COUNTER -lt 2 ]; do
2020-01-02 00:04:35 +00:00
2021-05-15 14:41:45 +00:00
> ((COUNTER++))
2020-01-02 00:04:35 +00:00
2021-05-15 14:41:45 +00:00
> echo $COUNTER
2020-01-02 00:04:35 +00:00
> done
```
There's also 'until', which stops when something is true, rather than keeping going when something is true.
# For
> for i in $( ls ); do
> du -sh $i
> done
# Sequences
The sequences tool counts up from X in jumps of Y to number Z.
Count from 1 to 10.
> seq 10
Count from 4 to 11.
> seq 4 11
Count from 1 to 100 in steps of 5.
> seq 1 5 100