POSIX Shell脚本意外退出问题排查求助
Hey there! Let's break down this tricky shell script issue you're dealing with— that "coordinates must be in pairs" error is a clear sign something's misfiring with how your mouse_click_coords() function gets its arguments, and it's yanking your script to a halt before it can finish its work. Here's how to debug and fix it:
mouse_click_coords() First, track down the definition of this function in your script. The error message tells us it expects coordinate pairs (like x1 y1 x2 y2), so it’s probably checking that the number of arguments is even, or that at least two values are passed. Look for logic like:
mouse_click_coords() { if [ $# -eq 0 ] || [ $(( $# % 2 )) -ne 0 ]; then echo "mouse_click_coords(): Coordinates must be in pairs! Exit code = 14" >&2 exit 14 fi # rest of the function... }
This check is failing somewhere, so we need to find where the function is being called with bad arguments.
Since the script dies right after the first loop, focus on the mouse_click_coords() call inside that loop. Ask yourself:
- How are the coordinates being generated? Are they pulled from a variable, command output, or command-line argument?
- Did you accidentally remove a parameter in a recent edit? For example, changing
mouse_click_coords "$x" "$y"tomouse_click_coords "$x"would trigger this error. - Is a variable holding a coordinate empty? If
$yis unset or blank, POSIX shell will drop it when passing arguments, turning a pair into a single value.
When you can’t remember what you changed, debug logs are your best friend. Add these lines to see exactly what’s being passed to the function:
- Right before calling
mouse_click_coords()in the loop:echo "DEBUG: Calling mouse_click_coords with args: $# total | $*" >&2 mouse_click_coords "$@" - Or at the very start of the
mouse_click_coords()function:mouse_click_coords() { echo "DEBUG: mouse_click_coords received: $# arguments | $*" >&2 # original validation logic here }
Run your script again, and the debug output will show you exactly which call has an odd number of arguments, or missing values.
-1 option handling Even though both launch modes fail the same way, confirm the -1 option isn’t messing with coordinate generation:
- Look for your script’s argument-parsing logic (likely using
while getopts). Does handling-1modify variables that the loop uses to generate coordinates? For example, overwriting a variable that stores Y-values, or changing how many iterations the loop runs? - It’s possible a recent edit broke the option handling in a way that affects both modes (e.g., a misplaced
shiftthat eats a coordinate argument).
If you use version control like git, compare your current script to the last known working version. Focus on:
- Changes to
mouse_click_coords()or its calls - Edits to the first loop’s logic
- Modifications to command-line argument parsing
A quick git diff will highlight exactly what you changed that might have caused the breakage.
Always wrap variables in double quotes when passing them as arguments (e.g., "$x" instead of $x). If a variable has spaces or special characters, skipping quotes will split it into multiple arguments, which can easily turn coordinate pairs into an odd number of values.
内容的提问来源于stack exchange,提问作者Vlastimil Burián

