PARS ENTERPRISE™ A PERSIAN MINIATURE ENCYCLOPEDIA
No adult sexual content is presented; however, due to the nature of the conceptual & philosophical art, a minimum age of 16 is required for entry and registration.
#!/bin/bash
Tells the system to use Bash to run the script.
chmod +x script.sh # Make executable
./script.sh # Run
NAME="Alice"
echo "Hello, $NAME"
=$VAR to accessif [ "$AGE" -ge 18 ]; then
echo "Adult"
else
echo "Minor"
fi
Comparison operators:
=, !=, -z, -n-eq, -ne, -lt, -le, -gt, -gefor i in 1 2 3; do
echo $i
done
while [ "$x" -le 5 ]; do
echo $x
((x++))
done
greet() {
echo "Hello, $1"
}
greet "Bob"
$1, $2, etc. are argumentsreturn optional; use echo for outputif [ -f file.txt ]; then
echo "It's a file"
fi
-f: regular file-d: directory-e: exists-r, -w, -x: read/write/execute| Command | Purpose |
|---|---|
read VAR |
Read input |
echo |
Print text |
cd, pwd |
Change/print directory |
exit |
Exit script |
source file |
Run a script in current shell |
set -e to exit on error#!/bin/bash -x for debugging"$VAR" to avoid word splitting$? for last command status#!/bin/bash # Shebang
chmod +x script.sh # Make script executable
./script.sh # Run script
NAME="Alice"
echo "Hello, $NAME"
="$VAR" to prevent word splittingif [ "$A" -gt 10 ]; then
echo "Greater"
elif [ "$A" -eq 10 ]; then
echo "Equal"
else
echo "Less"
fi
Operators:
| Type | Operators |
|---|---|
| Strings | =, !=, -z, -n |
| Numbers | -eq, -ne, -lt, -le, -gt, -ge |
| Files | -f, -d, -e, -r, -w, -x |
# For Loop
for i in 1 2 3; do echo $i; done
# While Loop
while [ $x -le 5 ]; do echo $x; ((x++)); done
myfunc() {
echo "Arg1 is $1"
}
myfunc "Hello"
read -p "Enter name: " NAME
echo "You typed: $NAME"
| Symbol | Description |
|---|---|
$0 |
Script name |
$1..$9 |
Arguments |
$# |
Number of arguments |
$@ |
All arguments |
$? |
Exit status of last command |
$$ |
PID of script |
[ -f file.txt ] && echo "Regular file"
[ -d dir ] && echo "Directory"
[ -e path ] && echo "Exists"
set -e # Exit on error
set -u # Error on undefined variables
set -x # Debug mode (prints commands)