编写SSH连接bash脚本遇“文件或目录未找到”错误的排查求助
Let’s break down the issues with your script and get it working properly:
Key Problems in Your Original Script
Broken Shebang Line:
Your script starts with#!usr/bin/bash—missing the leading slash (/) beforeusr, so the system can’t locate the bash interpreter correctly. Additionally, you’ve stuffed the entirextermcommand into the shebang line, which is invalid. The shebang should only specify the interpreter (and its optional flags), not the command you want to run.Smart Quotes Instead of Regular Quotes:
You’re using curly quotes (“Terminal1“,“.ssh/key_rsa“) instead of standard double quotes ("). Bash doesn’t recognize these smart quotes as string delimiters, so it treats them as part of the filename or command, leading to "file not found" errors.Leading Space in SSH Target:
There’s an extra space beforepi@192.168.0.107inside the quotes. This means SSH is trying to connect to a host namedpi@192.168.0.107(with a leading space), which isn’t a valid target.
Corrected Script
Here’s the fixed version of your script:
#!/bin/bash xterm -title "Terminal1" -hold -e ssh -i ".ssh/key_rsa" "pi@192.168.0.107"
Extra Tips
- Ensure Key Path is Reliable: If you run the script from a directory other than your home folder,
.ssh/key_rsamight not be found. Use an absolute path to avoid this:ssh -i "~/.ssh/key_rsa" "pi@192.168.0.107" - Make the Script Executable: After saving the corrected script, run this command to make it executable:
chmod +x your_script_name.sh
内容的提问来源于stack exchange,提问作者Alfred Wächter

