Bourne Shell中没有找到脚本UNIX命令
问题描述:
#!/bin/sh
echo "Insert the directory you want to detail"
read DIR
#Get the files:
FILES=`ls "$DIR" | sort`
echo "Files in the list:"
echo "$FILES"
echo ""
echo "Separating directories from files..."
for FILE in $FILES
do
PATH=${DIR}"/$FILE"
OUTPUT="Path: $PATH"
if [ -f "$PATH" ]; then
NAME=`echo "$FILE" | cut -d'.' -f1`
OUTPUT=${OUTPUT}" (filename: $NAME"
EXTENSION=`echo "$FILE" | cut -s -d'.' -f2`
if [ ${#EXTENSION} -gt 0 ]; then
OUTPUT=${OUTPUT}" - type: $EXTENSION)"
else
OUTPUT=${OUTPUT}")"
fi
elif [ -d "$PATH" ]; then
OUTPUT=${OUTPUT}" (dir name: $FILE)"
fi
echo "$OUTPUT"
done
运行它时我得到这个输出(我跑使用相对路径和完整路径)Bourne Shell中没有找到脚本UNIX命令
$ ./problem.sh
Insert the directory you want to detail
.
Files in the list:
directoryExample
problem.sh
Separating directories from files...
Path: ./directoryExample (dir name: directoryExample)
./problem.sh: cut: not found
./problem.sh: cut: not found
Path: ./problem.sh (filename:)
$
$
$ ./problem.sh
Insert the directory you want to detail
/home/geppetto/problem
Files in the list:
directoryExample
problem.sh
Separating directories from files...
Path: /home/geppetto/problem/directoryExample (dir name: directoryExample)
./problem.sh: cut: not found
./problem.sh: cut: not found
Path: /home/geppetto/problem/problem.sh (filename:)
$
正如你可以看到我收到“cut: not found
”两排列文件类型的输出字符串的次数。为什么? (我正在使用Free BSD)
答
PATH
是shell用来存储目录列表的变量,其中可能会找到像cut
这样的命令。您重写了该变量的值,失去了最初的列表。简单的解决方法是在for
循环中不使用PATH
。更完整的答案是避免所有变量名仅由大写字母组成,因为这些变量名仅供shell使用。在所有自己的变量名称中包含至少一个小写字母或数字,以避免干扰shell使用的当前(或将来)变量。
+0
谢谢你完整的解释,它完美地解决了。同样谢谢你的建议,以避免声明完整的大写自变量 – FtheBuilder 2014-09-23 23:22:04
看起来你没有'cut'。 – jwodder 2014-09-23 23:03:17