![](/img/trans.png)
[英]How to run a shell script from another shell script reading first shell script name from a CSV file
[英]how to handle file not found, when reading a shell script
我正在通過Shell腳本從文件讀取字符串。 它是這樣的:
count = 0
while read LINE
do
count++
if [ "$LINE" == "NONE" ]
then
echo "state is NONE"
else
if [ "$LINE" == "PLAYING" ]
then
echo "state is PLAYING"
fi
fi
done<$FILENAME
這是我從文件中讀取的內容,以及如何處理它,現在,如果找不到該文件,我想做其他事情,無論如何,還有做嗎? 例如:
if[ file not found]
then
do something
fi
if [ -f path_to_file ]
then
echo "file was found"
else
echo "file was not found"
fi
您最好使用以下條件開始編寫腳本:
if [ ! -f /your/file ]; then
echo "file not found"
else
...
proceed with your `while`, etc.
...
fi
由於您正在嘗試讀取文件,因此也許應該測試一下文件是否存在以及是否具有讀取權限:
if [[ ! -f $FILENAME ]] || [[ ! -r $FILENAME ]]
then
# do stuff
fi
-f
測試以查看$FILENAME
是否為常規文件, -r
測試以查看您(當前用戶)是否具有讀取權限。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.