Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在将脚本作为命令行参数运行的路径中遇到问题,测试它是否存在,然后更改为该路径以执行工作。这是我正在尝试的:
#!/bin/bash scriptpath=$1 if [ $# -lt 1 ] then echo "Usage: script.sh <directory_name>" fi if [ -d scriptpath ] then # work...... else echo "Directory does not exist" fi
改变这个:
if [ -d scriptpath ]
对此:
if [ -d $scriptpath ]
另外,我建议使用"",这样当参数包含奇怪的字符时,您的脚本仍然可以正常运行。(Unix 允许在文件名中使用空格、换行符、星号,甚至是控制字符。)所以:
""
scriptpath="$1" ... if [ -d "$scriptpath" ]