在这个声明中使用这个-f有什么用?

Apr*_*ors 1 unix linux shell

我没有-f在本程序中使用此声明.

if [ -f $file ]
Run Code Online (Sandbox Code Playgroud)

if知道为什么我们-f在这里使用?

这是整个计划:

case $ch in
  1)
    echo -e "\n This is File Create Operation"
    echo -e "\n Please Enter File Name:"
    read file

    if [ -f $file ]
    then
      echo -e "\n File already existing!!!"
    else

      touch $file
      echo -e "\n File Created!!!"
      echo -e "\n Created File can be Checked From Following Table"
      ls -t|head -n 15
    fi
  ;;
Run Code Online (Sandbox Code Playgroud)

Oli*_*lac 6

[通常既是别名,test也是内置的shell.[ something ]相当于test something

请参阅help [bash以了解bash builtin的版本,并man test了解非内置二进制文件/bin/test.

[ -f file ]如果文件存在且是常规文件,你会看到这是真的,否则为false(即只有file普通文件才返回0 )

  • downvotes*可能是*因为你的原始答案对于结束`]`是不正确的.虽然已经纠正,但大多数人在更新时都不会回过头来发帖(你没有得到关于此的通知),所以downvotes留了下来.我不认为这些是"巨魔",因为@ jm666说,但当时*有效*... (2认同)
  • @Carpetsmoker好的,是我的错,没有编辑历史记录 - 我看到答案的时候(几乎)是正确的.applogize给所有非巨魔;):) (2认同)