Skip to content

Instantly share code, notes, and snippets.

@mattak
Last active March 20, 2025 02:41
Show Gist options
  • Save mattak/431a7b3b128cfe6f4ff1574bf25210d6 to your computer and use it in GitHub Desktop.
Save mattak/431a7b3b128cfe6f4ff1574bf25210d6 to your computer and use it in GitHub Desktop.
Shell command generator by gemma3
#!/bin/bash
set -eu
if [ $# -lt 1 ]; then
cat << __USAGE__
Usage:
$0 <instruction>
Example:
$0 "show current working directory"
pwd
$0 "list up files with file size"
ls -l
__USAGE__
exit 1
fi
INSTRUCTION="$1"
# Create Environemnt info
SYSTEM_PROFILE="$(system_profiler SPHardwareDataType -json | jq -rMc '.SPHardwareDataType[0] | ["Model: \(.machine_model)","Chip: \(.chip_type)", "Memory: \(.physical_memory)"][]')"
OS_INFO="OS: $(sw_vers --productName) $(sw_vers --productVersion)"
ENVIRONMENT_INFO="${SYSTEM_PROFILE}\n${OS_INFO}"
# Create prompt
TMP_PROMPT=/tmp/gemma3command_prompt.txt
cat > $TMP_PROMPT << '__INSTRUCTION__'
```
You are shell command generator.
Please output appropreate command for the instruction.
Do not print extra descriptions. All you print is command text.
# Environment
{{ENVIRONMENT}}
# Examples
## Example1
現在のディレクトリのファイルリストを出力してください
ls
## Example2
awkで2行目のみを出力してください
awk '{print $2}'
# Instruction
{{INSTRUCTION}}
```
__INSTRUCTION__
perl -i'' -pe "s|{{ENVIRONMENT}}|${ENVIRONMENT_INFO}|g" $TMP_PROMPT
perl -i'' -pe "s|{{INSTRUCTION}}|${INSTRUCTION}|g" $TMP_PROMPT
# Execute gpt to generate command
RESULT=/tmp/gemma3command_result.txt
ollama run gemma3 "$(< $TMP_PROMPT)" |
perl -ne 'chomp; print if $_ !~ /^\s*$/' |
tee $RESULT
cat $RESULT | pbcopy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment