-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.sh
74 lines (73 loc) · 930 Bytes
/
array.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
#!/bin/bash
declare -a arr
echo "Enter size"
read x
echo "enter elements"
for ((i = 0;i<$x;i++)) do
read arr[$i]
done
echo ${arr[@]}
while $1
do
echo "enter operation:"
read op
case $op in
1)
echo "enter element"
read a
arr=("$a" "${arr[@]}")
echo "inserted"
echo ${arr[@]}
;;
2)
echo "enter element"
read a
arr+=("$a")
echo "inserted"
echo ${arr[@]}
;;
3)
echo "enter element"
read a
echo "enter index"
read b
arr=( "${arr[@]:0:$b}" "$a" "${arr[@]:$b}" )
echo "inserted"
echo ${arr[@]}
;;
4)
echo "delete from beginning"
unset arr[0]
echo ${arr[@]}
;;
5)
echo "delete from end"
unset arr[-1]
echo ${arr[@]}
;;
6)
echo "delete from index"
echo "enter index"
read b
unset arr[$b]
echo ${arr[@]}
;;
7)
echo "search"
echo "enter element"
read a
for ((i = 0;i<$x;i++)) do
if [ "${arr[$i]}" = "$a" ];then
echo "found"
fi
done
;;
8)
echo "sort"
arr=($(printf '%s\n' "${arr[@]}" | sort))
echo ${arr[@]}
;;
*)
echo "invalid"
esac
done