为何gcloud DNS记录更新命令在终端直接执行正常,在Bash脚本中运行却报错?
Looks like the issue comes from hidden extra characters in your script that are messing up the --zone parameter. Let's break this down:
What's Going Wrong?
Your error message shows the managed zone is being interpreted as pixelcraft (notice the trailing space at the end), but your actual zone name is pixelcraft without the space. This usually happens when:
- Your script was edited on a Windows machine, which uses CRLF (
\r\n) line endings instead of Linux's LF (\n). The hidden\rcharacter gets appended to the end of your--zone=pixelcraftparameter, making gcloud look for a zone namedpixelcraft\r(which shows up aspixelcraftin error messages). - There's an accidental trailing space after
pixelcraftin your script's--zoneargument (though looking at your script code, this seems less likely).
How to Fix It
Here are a few straightforward solutions:
Convert line endings to Linux format:
Run this command on your script to convert CRLF to LF:dos2unix your_script_name.shIf
dos2unixisn't installed, you can install it first with:sudo apt install dos2unixManually retype the gcloud commands in your script:
Sometimes copy-pasting code from Windows editors or other sources brings in hidden characters. Open your script in a Linux text editor (likenanoorvim) and retype the threegcloud dns record-sets updatelines from scratch, making sure there are no trailing spaces afterpixelcraft.Add quotes around variables (good practice anyway):
While not the root cause here, wrapping your$IP_ADDRvariable in double quotes prevents issues if the IP ever has unexpected whitespace (unlikely here, but a safe habit):gcloud dns record-sets update other.pixelcraftserver.net --rrdatas="$IP_ADDR" --type=A --zone=pixelcraft
Verify the Fix
After making these changes, run your script again. If you want to test first, execute the full command directly in your terminal to confirm it works (which you said it does), then ensure the script version matches exactly.
内容的提问来源于stack exchange,提问作者Pixonepro

