请帮我理解第二行。
我理解它增加了公共库,但我不确切地理解它的作用,特别是[ -f ..
命令
#!/bin/bash
[ -f ./scripts/commons.sh ] && source ./scripts/commons.sh ||
{ echo "Failed to source common.sh. Check that you are on l2ver dir." && exit 1; }
答案 0 :(得分:1)
-f
测试其参数是否存在并且是常规文件(例如,不是目录)。
在上面分享的代码段中,它检查./scripts/commons.sh
是否为文件,然后source
代码(即在相同的shell 中执行)。
答案 1 :(得分:0)
这意味着文件./scripts/commons.sh
存在
然后,如果它存在,文件将由source
命令处理
Read this
答案 2 :(得分:0)
-f
命令检查文件是否退出。 []
是bash中的测试语句。
&&
确保第二个命令source ./scripts/commons.sh
仅在第一个命令成功终止时才会生效。那是文件./scripts/commons.sh
存在
例如,如果文件存在
$ls test
test
$[ -f test ]
$echo $?
0
$[ -f test ] && echo "hello world"
hello world
如果不是
$[ -f test ]
$echo $?
1
$[ -f test ] && echo "hello world"
$
答案 3 :(得分:0)
基本上,由于逻辑表达式,它是唯一的一行。
如果文件存在,-f
是bash缩短的测试。 source
命令负责从./scripts/commons.sh
获取变量。
你有一个逻辑表达式,如:A和B或C.如果./scripts/commons.sh
不存在,那么echo和脚本会终止错误。
还有许多其他bash内置文件测试。请看下面的列表:
-b FILE
FILE exists and is block special
-c FILE
FILE exists and is character special
-d FILE
FILE exists and is a directory
-e FILE
FILE exists
-f FILE
FILE exists and is a regular file
-g FILE
FILE exists and is set-group-ID
-G FILE
FILE exists and is owned by the effective group ID
-h FILE
FILE exists and is a symbolic link (same as -L)
-k FILE
FILE exists and has its sticky bit set
-L FILE
FILE exists and is a symbolic link (same as -h)
-O FILE
FILE exists and is owned by the effective user ID
-p FILE
FILE exists and is a named pipe
-r FILE
FILE exists and read permission is granted
-s FILE
FILE exists and has a size greater than zero
-S FILE
FILE exists and is a socket
-t FD
file descriptor FD is opened on a terminal
-u FILE
FILE exists and its set-user-ID bit is set
-w FILE
FILE exists and write permission is granted
-x FILE
FILE exists and execute (or search) permission is granted