-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.sh
executable file
·68 lines (48 loc) · 1.23 KB
/
client.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
#!/bin/bash
# script that takes a user as argument
# prompts user for request in format req args
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
function ctrl_c() {
# function to delete user.pipe when script is shut down
rm "$id".pipe
echo "shutting down client.."
exit 0
}
if [ ! "$#" -eq 1 ]; then
echo "Error: only takes one parameter"
exit 52
else
id="$1"
mkfifo "$id".pipe
while true; do
echo "enter your request: "
read -a request
if [ "${#request[@]}" -eq 1 ]; then
cmd="${request[0]}"
# exit current program
if [ "$cmd" = "exit" ]; then
ctrl_c
# shutdown server
elif [ "$cmd" = "shutdown" ]; then
echo "shutting down server.."
echo "$cmd" > server.pipe
fi
elif [ "${#request[@]}" -gt 1 ]; then
req="${request[0]}"
argsArray=("${request[@]:1}")
args="${argsArray[*]}"
echo "$id $req $args" > server.pipe
else
echo "Error: bad request"
fi
# loop to receive response from the server
while read response; do
if [[ "$response" = *"OK"* || "$response" = *"Error"* ]]; then
echo "command executed successfully"
elif [[ "$response" != *"start_result"* && "$response" != "end_result" ]]; then
echo "$response"
fi
done < "$id".pipe
done
fi