-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.sh
executable file
·191 lines (161 loc) · 2.88 KB
/
base.sh
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/bash
# Variables
NAME="Adam"
a="My name is $NAME"
b="My name is ${NAME}-Szustak"
c=$(ls -a)
d=$((1 * 3))
NAME="Borys" echo "second cmd"
echo $NAME
# User inputs
read -p "Enter your name: " NAME
read -t 10 -sp "Enter password: " PASSWD
echo $NAME $PASSWD
# Exit status
true
echo $?
false
echo $?
# Number comparison
NR1=22
NR2=44
if [[ "$NUM1" -gt "$NUM2" ]]; then
echo "$NR1 is greater than $NR2"
else
echo "$NR1 is less than or equal $NR2"
fi
# String comparison & regex
AMOUNT=15
if [[ "$AMOUNT" == 15USD ]]; then
echo "It costs $AMOUNT"
elif [[ "$AMOUNT" =~ ^[0-9]+[A-Z]+$ ]]; then
echo "It costs another amount $AMOUNT"
elif ! [[ "$AMOUNT" =~ ^[0-9]+[A-Z]+$ ]]; then
echo "perhaps $AMOUNT is not correct"
fi
# Logical Operators
VALUE=33
if [[ "$VALUE" -ge "$NR1" && "$VALUE" -le "$NR2" ]]; then
echo "Value is located between"
fi
# File conditions
FILE=test1
if [[ -e "$FILE" ]]; then
if [[ -d "$FILE" ]]; then
echo "$FILE is dir"
elif [[ -f "$FILE" ]]; then
echo "$FILE is file"
else
echo "$FILE N/A" >&2
# exit 1
fi
else
echo "$FILE does NOT exist" >&2
# exit 1
fi
# Here doc
cat <<- _EOF_
1 line
2 line
_EOF_
# Here string
read file <<< $(ls)
echo $file
# While
count=0
while [[ $count -le 3 ]]; do
echo $count
count=$((count+1))
done
ls | while read file; do
echo $file
done
# Case
case $NAME in
q|Q) echo "Exiting";;
[[:alpha:]]+) echo "OK";;
*) echo "Invalid";;
esac
# Function
function sayHello() {
echo "Hello World"
}
# **** OR ****
sayHello () {
local foo=bar
echo "Hello World"
}
sayHello
# Validation
read -p "Input item > "
[[ -z $REPLY ]] && sayHello
# Positional arguments
echo $0 $1 $2
echo "Accepted $# args"
# Function with params
greet() {
echo "I am $1 and I am $2"
}
greet "Adam" "29"
# Shift
count=1
while [[ $# -gt 0 ]]; do
echo "Arg $count = $1"
count=$((count + 1))
shift
done
# For loop
for i in {A..D}; do
echo $i
done
# Manage empty variables
foo=
echo ${foo:-"bar"}
echo $foo
echo ${foo:="bar"}
echo $foo
echo ${foo:="fooooo"}
echo ${foo:?"is empty"}
foo=
echo ${foo:+"bar"}
foo=foooooo
echo ${foo:+"bar"}
# String operations
foo="This is something"
echo ${#foo}
echo ${foo:5:15}
foo=file.zip.txt
echo ${foo#*.}
echo ${foo##*.}
echo ${foo%.*}
echo ${foo%%.*}
foo=JPG.JGP
echo ${foo/JPG/jpg}
echo ${foo//JPG/jpg}
foo=aBcD
echo ${foo,,}
echo ${foo^^}
echo ${foo^}
# Number operations
echo $((5 - 6))
if (( $INT == 0 )); then
echo null
fi
echo $((51))
echo $((8#51))
echo $((16#51))
echo $((2#1111))
for ((i = 0; i <= 20; i++)); do
if (((( i % 5)) == 0 )); then
printf "<%d> " $i
else
printf "%d " $i
fi
done
# Arrays
a[1]=foo
echo ${a[1]}
days=(Mon Tue Wed Thu Fri)
echo $days
days+=(Sat)
echo ${#days[@]}