-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.sh
81 lines (68 loc) · 1.48 KB
/
stack.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
#!/bin/sh
set -e
save_dir='/tmp/stackdata'
save_file="$save_dir/stack_$PPID"
usage() {
echo 'stack [ up | dw | sw | ls | rm | help ]'
}
help() {
echo 'stack [ up | dw | sw | ls | rm | help ] :'
echo ' | up : Push the current directory onto the stack'
echo ' | dw : Pop the top of the stack and return it'
echo ' | sw : Replace the top of the stack with the current directory and return it'
echo ' | ls : List the contents of the stack'
echo ' | rm : Remove the stack'
}
up() {
pwd >> "$save_file"
}
dw() {
touch "$save_file"
last_line=$(tail -n 1 "$save_file")
num_lines=$(wc -l < "$save_file")
new_num_lines=$((num_lines - 1))
head -n "$new_num_lines" "$save_file" > "$save_file"
echo $last_line
}
sw() {
dw
up
}
list() {
touch "$save_file"
cat "$save_file"
}
remove() {
rm "$save_file"
touch "$save_file"
}
if [ "$#" -lt 1 ]; then
echo 'Error: Insufficient number of arguments!'
usage
exit 1
fi
mkdir -p "$save_dir"
case "$1" in
'up')
up
;;
'dw')
dw
;;
'sw')
sw
;;
'ls')
list
;;
'rm')
remove
;;
'help')
help
;;
*)
usage
exit 1
esac
exit 0