This repository has been archived by the owner on Aug 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx.sh
executable file
·203 lines (177 loc) · 7.45 KB
/
x.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/bin/bash
#shellcheck source=/home/tobi/.bash_functions/.bash_variables
source /home/"$USER"/.bash_functions/.bash_variables
# Default case
if [ $# -eq 0 ]
then
wsl.exe -v
echo "$(cat /etc/*release | grep '^NAME="' | sed -e 's/[^"]*"//' -e 's/"$//') version:" "$(cat /etc/*_version)" "$(cat /etc/*release | grep '^VERSION_CODENAME=' | sed -e 's/[^=]*=//')"
else
x=1
while [ -n "${!x}" ]
do
case "${!x}" in
h)
cat "$WSL_HELP_DIR"/x
break
;;
hx)
uname -a
printf '\n'
cat /etc/{*-release,*_version}
;;
x_rls0)
file_Path=$((x+1))
echo "\"${!file_Path}\"" | cmd.exe > /dev/null 2>&1 &
break
;;
*)
# Start statements (--> Windows Command Prompt)
if echo "${!x}" | grep -iq '^start'
then
echo "${!x}" | cmd.exe > /dev/null 2>&1
# Internet addresses
elif echo "${!x}" | grep -qE '^http|www.'
then
"${WSL_BROWSER_BIN}" "${!x}"
# Windows file paths / WSL / Linux distributions
else
# Function call with performance flag enabled
if echo "${!x}" | grep -q 'x_rls1'
then
tmp=$((x+1))
else
tmp=$x
fi
if echo ${!tmp} | grep -iqE '^[a-z]:|^%SYSTEMDRIVE%|^%USERPROFILE%|^%APPDATA%|^%LOCALAPPDATA%|^%OneDrive%'
then
file_Path=$(readlink -f "${!tmp}" | sed "s|$PWD||g" | cut -b 2-)
elif readlink -f ${!tmp} | grep -iq '/mnt/[a-z]/'
then
file_Path=$(wslpath -w "${!tmp}")
else
file_Path=$(readlink -f "${!tmp}")
fi
# Default function call with character encoding conversion
if echo "${!x}" | grep -qv x_rls1
then
# Insertion of placeholders for incompatible chars
file_Path=$(echo "$file_Path" | sed \
-e 's/Ä/{00/g; s/Á/{01/g; s/À/{02/g; s/Å/{03/g' \
-e 's/ä/{04/g; s/á/{05/g; s/à/{06/g; s/å/{07/g' \
-e 's/Æ/{08/g' \
-e 's/æ/{09/g' \
-e 's/Ç/{10/g' \
-e 's/ç/{11/g' \
-e 's/Ë/{12/g; s/É/{13/g; s/È/{14/g' \
-e 's/ë/{15/g; s/é/{16/g; s/è/{17/g' \
-e 's/Ğ/{18/g' \
-e 's/ğ/{19/g' \
-e 's/Ï/{20/g; s/Î/{21/g' \
-e 's/ï/{22/g; s/î/{23/g' \
-e 's/Ñ/{24/g' \
-e 's/ñ/{25/g' \
-e 's/Ö/{26/g; s/Ó/{27/g; s/Ò/{28/g; s/Ø/{29/g' \
-e 's/ö/{30/g; s/ó/{31/g; s/ò/{32/g; s/ø/{33/g' \
-e 's/ẞ/{34/g; s/Ş/{35/g' \
-e 's/ß/{36/g; s/ş/{37/g' \
-e 's/Ü/{38/g; s/Ú/{39/g; s/Ù/{40/g' \
-e 's/ü/{41/g; s/ú/{42/g; s/ù/{43/g' \
-e 's/Ÿ/{44/g' \
-e 's/ÿ/{45/g' \
-e 's/©/{46/g; s/℗/{47/g; s/®/{48/g; s/™/{49/g' )
fi
# Passed Windows file path / Execution within the WSL
if echo "$file_Path" | grep -iqE '^[a-z]:|^%SYSTEMDRIVE%|^%USERPROFILE%|^%APPDATA%|^%LOCALAPPDATA%|^%OneDrive%'
then
# .exe / .bat / .lnk extension
# shellcheck disable=SC1003
if echo "$file_Path" | rev | cut -d'\' -f1 | rev | grep -iqE '.exe$|.bat$|.lnk$'
then
# shellcheck disable=SC1117
echo "\"$WSL_PROG_DIR\scripts\wsl_start.bat\" exe \"$file_Path\"" | cmd.exe > /dev/null 2>&1 &
sleep 1
pkill -n cmd.exe
# Directories
elif [ -d "${!x}" ]
then
# shellcheck disable=SC1117
echo "\"$WSL_PROG_DIR\scripts\wsl_start.bat\" dir \"$file_Path\"" | cmd.exe > /dev/null 2>&1 &
# No file extension
elif echo "$file_Path" | rev | cut -d'\' -f1 | rev | grep -ivq '\.'
then
# shellcheck disable=SC1117
echo "\"$WSL_PROG_DIR\scripts\wsl_start.bat\" code \"$file_Path\"" | cmd.exe > /dev/null 2>&1 &
# Other file extensions
else
# shellcheck disable=SC1117
echo "\"$WSL_PROG_DIR\scripts\wsl_start.bat\" start \"$file_Path\"" | cmd.exe > /dev/null 2>&1 &
fi
# Execution within any Linux distribution
else
WSL_DISTRO_DIR=WSL_"$WSL_DISTRO_NAME"_DIR
# .pdf file extension
if echo "$file_Path" | rev | cut -d'/' -f1 | rev | grep -iq '.pdf'
then
# Get disk and current user if not available
if [ -z "$disk" ]
then
value=$(cmd.exe /c echo %SYSTEMDRIVE% 2> /dev/null | cut -b 1 | tr '[:upper:]' '[:lower:]')
export disk=$value
fi
if [ -z "$user" ]
then
# shellcheck disable=SC1012,SC2026
value=$(cmd.exe /c echo %USERNAME% 2> /dev/null | tr -d '$'\r'')
export user=$value
fi
# Copy the specified file outside the WSL and open it
file_Name=$(basename "$file_Path")
cp "${!x}" /mnt/"$disk"/users/"$user"/
# shellcheck disable=SC1117
echo "\"$WSL_PROG_DIR\scripts\wsl_start.bat\" start \"%USERPROFILE%\\$file_Name\"" | cmd.exe > /dev/null 2>&1 &
# Delete the file after it has been closed
# shellcheck disable=SC1003
value="/mnt/$disk/users/$user/${!x}":$(echo "$WSL_BROWSER_DIR" | rev | cut -d'\\' -f1 | rev)
export WSL_REMOVE_INFO=$value
# shellcheck disable=SC2016
fish -c 'wsl r $WSL_REMOVE_INFO' > /dev/null 2>&1 &
# Check if the current distribution is registered as a valid distribution in the WSL (--> $WSL_X_DIR)
elif [ -n "${!WSL_DISTRO_DIR}" ]
then
# .exe / .bat / .lnk file extension
if echo "$file_Path" | rev | cut -d"/" -f1 | rev | grep -iqE '.exe$|.bat$|.lnk$'
then
# shellcheck disable=SC1117
echo "\"$WSL_PROG_DIR\scripts\wsl_start.bat\" exe \"\\\\wsl\$\\$WSL_DISTRO_NAME\\$file_Path\"" | cmd.exe > /dev/null 2>&1 &
sleep 1
pkill -n cmd.exe
# Directories
elif test -d "$file_Path"
then
editor "$file_Path"
# No file extension
elif echo "$file_Path" | rev | cut -d"/" -f1 | rev | grep -ivq '\.' || echo "$file_Path" | rev | cut -d"/" -f1 | rev | grep -q '^\.'
then
"$WSL_CODE_BIN" "$file_Path"
# Other file extensions
else
# shellcheck disable=SC1117
echo "\"$WSL_PROG_DIR\scripts\wsl_start.bat\" cmd \"\\\\wsl\$\\$WSL_DISTRO_NAME\\$file_Path\"" | cmd.exe > /dev/null 2>&1 &
fi
# Distribution is not compatible / Distribution needs to be registered (--> $WSL_X_DIR)
else
echo "x ${!x}: operation not supported"
fi
fi
# Function call with performance flag enabled
if echo "${!x}" | grep -q x_rls1
then
break
fi
fi
;;
esac
x=$((x+1))
done
fi