簡體   English   中英

Bourne Shell在腳本上找不到Unix命令

[英]Bourne Shell doesn't find unix commands on script

#!/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

運行時得到此輸出(我使用相對路徑和完整路徑運行)

$ ./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是外殼程序用來存儲目錄列表的變量,在其中可以找到諸如cut命令。 您重寫了該變量的值,從而丟失了初始列表。 簡單的解決方法是在for循環中不要使用PATH 更為完整的答案是避免所有僅由大寫字母組成的變量名,因為它們保留供外殼使用。 在您自己的所有變量名中至少包含一個小寫字母或數字,以避免干擾Shell使用的當前(或將來)變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM