Skip to content

SWE-agent takes a GitHub issue and tries to automatically fix it, using your LM of choice. It can also be employed for offensive cybersecurity or competitive coding challenges. [NeurIPS 2024]

Fix tools unbound variable errors

carlosejimenez7f02e50344d4
diff --git a/tools/defaults/bin/create b/tools/defaults/bin/create
index 3df63e90..92abc456 100644
--- a/tools/defaults/bin/create
+++ b/tools/defaults/bin/create
@@ -3,22 +3,23 @@ set -euo pipefail
source /root/tools/defaults/lib/utils.sh
main() {
- if [ -z "$1" ]; then
+ local filename="${1:-}"
+ if [ -z "${filename}" ]; then
echo "Usage: create <filename>"
exit 1
fi
# Check if the file already exists
- if [ -e "$1" ]; then
- echo "Error: File '$1' already exists."
- open "$1" || exit 1
+ if [ -e "${filename}" ]; then
+ echo "Error: File '${filename}' already exists."
+ open "${filename}" || exit 1
exit 1
fi
# Create the file an empty new line
- printf "\n" > "$1"
+ printf "\n" > "${filename}"
# Use the existing open command to open the created file
- open "$1" || exit 1
+ open "${filename}" || exit 1
}
main "$@"
diff --git a/tools/defaults/bin/open b/tools/defaults/bin/open
index a8b0d198..f8c4dbcb 100644
--- a/tools/defaults/bin/open
+++ b/tools/defaults/bin/open
@@ -5,8 +5,7 @@ source /root/tools/defaults/lib/utils.sh
main() {
local WINDOW=$(_read_env WINDOW)
- if [ -z "$1" ]
- then
+ if [ $# -eq 0 ]; then
echo "Usage: open \"<file>\""
exit 1
fi
@@ -19,7 +18,7 @@ main() {
fi
# Check if the second argument is provided
- if [ -n "$2" ]; then
+ if [ $# -ge 2 ]; then
# Check if the provided argument is a valid number
if ! [[ $2 =~ ^[0-9]+$ ]]; then
echo "Usage: open \"<file>\" [<line_number>]"
diff --git a/tools/edit_linting/bin/edit b/tools/edit_linting/bin/edit
index 6defeec3..778994d9 100644
--- a/tools/edit_linting/bin/edit
+++ b/tools/edit_linting/bin/edit
@@ -12,6 +12,13 @@ main() {
return
fi
+ if [ $# -eq 0 ]; then
+ echo "Usage: edit <start_line>:<end_line>"
+ echo "<replacement_text>"
+ echo "end_of_edit"
+ return
+ fi
+
local start_line="$(echo $1: | cut -d: -f1)"
local end_line="$(echo $1: | cut -d: -f2)"
diff --git a/tools/search/bin/search_file b/tools/search/bin/search_file
index 4c6828f2..4a942b1e 100644
--- a/tools/search/bin/search_file
+++ b/tools/search/bin/search_file
@@ -2,12 +2,13 @@ source /root/tools/defaults/lib/utils.sh
main() {
# Check if the first argument is provided
- if [ -z "$1" ]; then
+ local search_term="${1:-}"
+ if [ -z "${search_term}" ]; then
echo "Usage: search_file <search_term> [<file>]"
return
fi
# Check if the second argument is provided
- if [ -n "$2" ]; then
+ if [ $# -ge 2 ]; then
# Check if the provided argument is a valid file
if [ -f "$2" ]; then
local file="$2" # Set file if valid
@@ -18,7 +19,7 @@ main() {
fi
else
# Check if a file is open
- if [ -z "$CURRENT_FILE" ]; then
+ if [ -z "${CURRENT_FILE:-}" ]; then
echo "No file open. Use the open command first."
return # Exit if no file is open
fi
@@ -29,7 +30,7 @@ main() {
# Use grep to directly get the desired formatted output
local matches=$(grep -nH -- "$search_term" "$file")
# Check if no matches were found
- if [ -z "$matches" ]; then
+ if [ -z "${matches:-}" ]; then
echo "No matches found for \"$search_term\" in $file"
return
fi