-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgptbash.sh
More file actions
executable file
·77 lines (66 loc) · 2.24 KB
/
gptbash.sh
File metadata and controls
executable file
·77 lines (66 loc) · 2.24 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
#!/bin/bash
# install:
# 1. save it to /usr/local/bin/gptbash
# 2. make it executable: chmod +x /usr/local/bin/gptb
# 3. add environment OPENAI_API_KEY=sk-...
#
# usage: POSTFIX="on ubuntu" gptb what you need for from bash
set -eu
# check is OPENAI_API_KEY set
if [ -z "${OPENAI_API_KEY:-}" ]; then
echo "OPENAI_API_KEY is not set"
echo ""
echo "Get it from https://platform.openai.com/api-keys"
echo "And add to your ~/.bashrc or ~/.zshrc"
echo "export OPENAI_API_KEY=sk-..."
echo ""
echo "Or run this script with OPENAI_API_KEY=sk-... prefix"
exit 1
fi
# if help command
if [ "$1" = "help" ]; then
echo "Usage: npx gptbash what you need for from bash"
exit 0
fi
PREFIX="${PREFIX:-how to}"
POSTFIX="${POSTFIX:-in bash, one-liner}"
out_file=$(mktemp -t gptb-XXXX)
trap 'rm -f "$out_file"' EXIT
p="$PREFIX $@ $POSTFIX"
p="You are bash script. Command you will return should be possible to execute in one confirmation click without changes and replacements. Use \\r\\n if need. Use current \".\" directory in examples if another is not provided. Users request: \"$p\" Bash one line command:"
echo "$p"
npx chatgpt "$p" | tee -a "$out_file"
cmd="$(cat $out_file | grep '```' -A1 | sed -n 2p)"
echo -e "\033[0;37m${cmd}\033[0m"
while true; do
read -p "Run this command? (Y/n) " yn
case $yn in
[Yy] | "" ) # Handles yes and empty (default to yes)
if [[ -n $cmd ]]; then
eval "cd $PWD && $cmd" # Run the command if $cmd is set
else
echo "Command (\$cmd) is not set. Exiting..."
fi
break
;;
[Nn]* ) # Handles no
echo "No, exiting..."
exit
;;
* ) # Handles everything else
echo "Please answer yes (Y/y) or no (N/n)."
;;
esac
done
# 1 of 100 random usages - print out greeting
if [ $((RANDOM % 100)) -eq 0 ]; then
echo "Made with love by Solving LLC: https://solving.llc/"
echo ""
echo "Follow us:"
echo "LinkedIn: https://www.linkedin.com/company/solving-llc/"
echo "Twitter: https://twitter.com/SolvingLLC"
echo ""
echo "Connect with author: http://ceo.solving.llc"
echo ""
echo "Welcome to contribute: https://github.com/Solving-LLC/gptbash"
fi