Shell Script Cheat Sheet
Array
- Test code:
#!/bin/sh
array=(0 1 2 3)
echo $array
echo ${array[0]}
echo ${array[@]}
- Output:
0
0
0 1 2
if
- Test code:
#!/bin/sh
val0=0
val1=1
#
if [ $val0 = 0 ]; then
echo val0 = 0
elif [ $val0 = 1 ]; then
echo val0 = 1
else
echo otherwise
fi
# AND
if [ $val0 = 0 ] && [ $val1 != 1 ]; then
echo true
else
echo false
fi
# OR
if [ $val0 = 1 ] || [ $val1 = 1 ]; then
echo true
else
echo false
fi
file=foo.txt
dir=bar
# Existing of file or dir
if [ -f ${file} ] && [ -d ${dir} ]; then
echo true
else
echo false
fi
- Output:
val0 = 0
false
true
false
for
- Test code:
#!/bin/sh
array=(0 1 2)
#
for i in `seq 10`; do
echo $i
done
#
for j in ${array[@]}; do
echo $j
done
- Output:
1
2
3
4
5
6
7
8
9
10
0
1
2
Input
- Test code:
echo -n "input > "
read VAR
echo "output > $VAR"
- Output:
input > hello
output > hello
Function
- Definition:
func() {
echo hello
}
func_arg() {
echo "arg1 = $1, arg2 = $2"
}