[英]Executing a unix shell script with ksh and ./
我試圖遍歷目錄中的一組文件並對其進行某些活動。 當我用ksh執行腳本時,它按預期工作。 但是當我用./執行它時,它失敗並顯示錯誤的錯誤,說了太多的參數。 請幫忙。
ScriptName:Script1
當執行帶有ksh Script1 argument1
exec時,它可以按預期工作。 但是,當使用./Script1 argument1
執行時, ./Script1 argument1
會拋出過多的參數錯誤。
#!/bin/bash
cd ../SrcFiles
if [ -e *$1* ]
then
for file in *$1*
do
echo $file
if [ $1 == "XXX" ]
then
echo $file >> ABC_"$1".txt
elif [ $1 == "YYY" ]
then
echo $file >> ABC_"$1".txt
else
echo $file >> ABC_"$1".txt
fi
done
else
#createadummyfile
fi
這里:
if [ -e *$1* ]
-e
需要一個參數。 全局擴展可能會找到多個文件。 如果要檢查是否有任何匹配的文件,請使用數組保存全局擴展:
files=(~(N)*"$1"*)
if (( ${#files[@]} == 0)); then
# no files found
else
# do stuff to matching files
for file in "${files[@]}"; do ...
fi
與ksh93一起使用。 ~(N)
部分指示ksh如果沒有文件匹配,則不要將模式保留為輸出(類似於bash的nullglob
選項)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.