-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyshell.sh
189 lines (172 loc) · 5.08 KB
/
myshell.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
#!/bin/bash
# Global variables for command history and aliases
declare -a command_history
declare -A aliases
# Function to execute a command
execute_command() {
local command="$1"
command_history+=("$command") # Append to history
# Check for built-in commands
case "$command" in
"exit")
exit 0
;;
"history")
show_history
;;
"env")
show_environment_variables
;;
"help")
show_help
;;
*)
if [[ "$command" == *" && "* ]]; then
handle_chaining "$command"
elif [[ "$command" == *" | "* ]]; then
handle_piping "$command"
elif [[ "$command" == *">"* || "$command" == *">>"* ]]; then
handle_redirection "$command"
elif [[ "$command" == *"&" ]]; then
run_in_background "${command%&}"
elif [[ "$command" == "set "* ]]; then
set_environment_variable "$command"
elif [[ "$command" == alias* ]]; then
set_alias "$command"
elif [[ "$command" == "unalias "* ]]; then
remove_alias "$command"
elif [[ "$command" == cd* ]]; then
change_directory "$command"
elif [[ "$command" == open* ]]; then
open_website "$command"
else
# Check if command matches an alias
for alias in "${!aliases[@]}"; do
if [[ "$command" == "$alias"* ]]; then
command="${aliases[$alias]}"
fi
done
# Execute non-builtin command
eval "$command"
fi
;;
esac
}
# Show command history
show_history() {
echo "Command History:"
for i in "${!command_history[@]}"; do
echo "$((i + 1)): ${command_history[i]}"
done
}
# Show environment variables
show_environment_variables() {
echo "Environment Variables:"
printenv
}
# Handle command chaining with &&
handle_chaining() {
IFS="&&" read -ra commands <<< "$1"
for cmd in "${commands[@]}"; do
cmd=$(echo "$cmd" | xargs) # Trim whitespace
eval "$cmd" || break # Stop if a command fails
done
}
# Handle piping commands
handle_piping() {
local commands=("${@//|/ }")
eval "$1"
}
# Handle redirection (> and >>)
handle_redirection() {
local output_file
if [[ "$1" == *">>"* ]]; then
command="${1%>>*}"
output_file="${1#*>>}"
command=$(echo "$command" | xargs)
output_file=$(echo "$output_file" | xargs)
eval "$command" >> "$output_file"
else
command="${1%>*}"
output_file="${1#*>}"
command=$(echo "$command" | xargs)
output_file=$(echo "$output_file" | xargs)
eval "$command" > "$output_file"
fi
}
# Run command in background
run_in_background() {
eval "$1" &
}
# Set environment variable
set_environment_variable() {
local args=($@)
if [[ ${#args[@]} -eq 3 ]]; then
export "${args[1]}"="${args[2]}"
echo "Environment variable ${args[1]} set to ${args[2]}"
elif [[ ${#args[@]} -eq 2 ]]; then
echo "${args[1]}=${!args[1]}"
else
echo "Usage: set VAR VALUE or set VAR"
fi
}
# Set alias
set_alias() {
local args=($@)
if [[ ${#args[@]} -gt 2 ]]; then
local alias_name="${args[1]}"
local alias_command="${args[@]:2}"
aliases[$alias_name]="$alias_command"
echo "Alias set: $alias_name = $alias_command"
elif [[ ${#args[@]} -eq 2 && "${args[1]}" == "-l" ]]; then
echo "Aliases:"
for alias in "${!aliases[@]}"; do
echo "$alias = ${aliases[$alias]}"
done
else
echo "Usage: alias name command or alias -l"
fi
}
# Remove alias
remove_alias() {
local alias_name="${1#unalias }"
if [[ -n "${aliases[$alias_name]}" ]]; then
unset aliases[$alias_name]
echo "Alias '$alias_name' removed"
else
echo "No such alias: $alias_name"
fi
}
# Change directory
change_directory() {
local dir="${1#cd }"
if cd "$dir" 2>/dev/null; then
:
else
echo "Directory not found: $dir"
fi
}
# Open a website
open_website() {
local url="${1#open }"
xdg-open "$url" &>/dev/null || echo "Error opening URL"
}
# Show help
show_help() {
echo "Available Commands:"
echo " history - Show command history"
echo " env - Show environment variables"
echo " set VAR VALUE - Set environment variable"
echo " alias name command - Set command alias"
echo " alias -l - List all aliases"
echo " unalias name - Remove an alias"
echo " cd <directory> - Change directory"
echo " open <URL> - Open a website"
echo " help - Show this help message"
echo " exit - Exit the shell"
}
# Main loop
while true; do
read -p "myshell> " command
execute_command "$command"
done