-
Notifications
You must be signed in to change notification settings - Fork 3
/
bash
94 lines (74 loc) · 1.45 KB
/
bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# To implement a for loop:
for file in *; do
echo $file found;
done
# while loop
while [ expression ]; do
commands
done
# if statement
if [expr]; then
commands
elif
commands
else
command
fi
# To implement a case command:
case "$1"
in
0) echo "zero found";;
1) echo "one found";;
2) echo "two found";;
3*) echo "something beginning with 3 found";;
esac
# functions
fname(){
commands
}
# Turn on/off debugging:
set -x
set +x
# Turn on/off fail exit:
set -e
set +e
# Turn on/off pip fail:
set -o pipefail
set +o pipefail
# How all bash scripts should start (sharp-bang)
#!/bin/bash
# <<< redirect words to stdin:
less <<< "foo bar"
# ctrl-x ctrl-e opens editor to work on long complex command
# split with space
IFS=" "
for LINE in `cat /etc/passwd`
do
echo $LINE
done
# read line
while read LINE
do
echo $LINE
done < /etc/passwd
# read list form file
children_projects=( $(< project-name.txt) )
# read list form command
children_projects=( $(repo list -n) )
# declare array.
declare -A animals
animals=( ["moo"]="cow" ["woof"]="dog" )
declare -A animals=( ["moo"]="cow" ["woof"]="dog")
echo "${animals[moo]}"
for sound in "${!animals[@]}"; do echo "$sound - ${animals[$sound]}"; done
## values
"${animals[@]}"
## keys
"${!animals[@]}"
# fix set -x
[[ -o xtrace ]] && ts='set -x' || ts='set +x'; set +x
eval "$ts"
# get result in -e
command && RET=$? || RET=$?
# make comand alway success
command && true