linux - 读取 shell 脚本时如何处理未找到的文件

标签 linux bash shell

我正在通过 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

关于linux - 读取 shell 脚本时如何处理未找到的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15881571/

相关文章:

json - 如果键匹配,则从 bash 中的 json 中提取特定键值

linux - 使用 BASH 脚本抓取并封装文本

linux - 从文件中的多行添加整数

c - 我想获取宿主语言文件的时间(ctime(..)?)

bash - VSCode Integrated Terminal 创建一个单独的窗口

bash - 如何从文件中包含 whiptail 的数据?

linux - shell bash 代码未运行且没有错误警告

linux - 从字符串 linux 中获取键值

linux - sed 用 NULL 替换空格

linux - 嗅探IOCTL和串口通信