确定Shell程序中的相对路径还是绝对路径
问题描述:
如标题所述,我需要确定路径是相对路径还是绝对路径,何时运行程序:
As stated in the title, I need to determine when a program is ran if the path is relative or absolute:
./program #relative
dir/dir2/program #relative
~User/dir/dir2/program #absolute
/home/User/dir/dir2/program #absolute
这是我的测试用例.我到底该如何在Shell程序中执行此操作?
This are my test cases. How exactly could I go about doing this in a shell program?
或更笼统地说,如何检查这种情况下的路径$0
是相对的还是绝对的?
Or more generally, how to check if a path, $0
in this case, is relative or absolute?
答
if [[ "$0" = /* ]]
then
: # Absolute path
else
: # Relative path
fi