-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfd_manager.sh
More file actions
executable file
·106 lines (94 loc) · 2.3 KB
/
fd_manager.sh
File metadata and controls
executable file
·106 lines (94 loc) · 2.3 KB
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
#!/bin/bash
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No color
# Menu Function
function show_menu() {
echo -e "${GREEN}File Descriptor Manager${NC}"
echo "1) List Open File Descriptors"
echo "2) Redirect stdout to a file"
echo "3) Redirect stderr to a file"
echo "4) Create a custom FD (write mode)"
echo "5) Write to a custom FD"
echo "6) Read from a custom FD"
echo "7) Close a custom FD"
echo "8) Reset stdout & stderr"
echo "9) Exit"
echo -n "Choose an option: "
}
# List Open File Descriptors
function list_fds() {
echo -e "${GREEN}Listing Open File Descriptors:${NC}"
ls -l /proc/$$/fd
}
# Redirect stdout to a file
function redirect_stdout() {
echo -n "Enter file name to redirect stdout: "
read file
exec 1> "$file"
echo "stdout is now redirected to $file"
}
# Redirect stderr to a file
function redirect_stderr() {
echo -n "Enter file name to redirect stderr: "
read file
exec 2> "$file"
echo "stderr is now redirected to $file"
}
# Create a custom file descriptor (write mode)
function create_fd() {
echo -n "Enter FD number (>=3): "
read fd
echo -n "Enter filename: "
read file
exec {fd}> "$file"
echo "FD $fd is now pointing to $file (write mode)"
}
# Write to a custom FD
function write_fd() {
echo -n "Enter FD number to write to: "
read fd
echo -n "Enter message: "
read message
echo "$message" >&"$fd"
echo "Message written to FD $fd"
}
# Read from a custom FD
function read_fd() {
echo -n "Enter FD number to read from: "
read fd
cat <&"$fd"
}
# Close a custom FD
function close_fd() {
echo -n "Enter FD number to close: "
read fd
exec {fd}>&-
echo "FD $fd closed"
}
# Reset stdout & stderr
function reset_stdio() {
exec 1>&3 2>&4
echo "stdout and stderr reset"
}
# Save original stdout & stderr for reset functionality
exec 3>&1 4>&2
# Menu Loop
while true; do
show_menu
read option
case $option in
1) list_fds ;;
2) redirect_stdout ;;
3) redirect_stderr ;;
4) create_fd ;;
5) write_fd ;;
6) read_fd ;;
7) close_fd ;;
8) reset_stdio ;;
9) echo "Exiting..."; exit 0 ;;
*) echo -e "${RED}Invalid option!${NC}" ;;
esac
echo
done