linux source shell腳本顯示找不到文件

例子: shell

[root@localhost osc]# sh a.sh 
a.sh: line 4: source: 1.sh: file not found
[root@localhost osc]# cat a.sh 
#!/bin/bash
cd /data/osc/bin/
if [ -f 1.sh ];then
source 1.sh
fi

剛開始,還覺得權限問題,而後給權限,但結果仍是同樣bash

man source ,找到緣由了。source時找filename的問題。ui

source 命令找filename是在當前的shell環境下找的,若是沒有反斜線,就在path中找,若是沒有就沒有。code

source filename [arguments]
              Read  and  execute commands from filename in the current shell environment and return the exit status of the last command exe-
              cuted from filename.  If filename does not contain a slash, file names in PATH are used to find the directory containing file-
              name.   The  file  searched  for  in  PATH  need  not be executable.  When bash is not in posix mode, the current directory is
              searched if no file is found in PATH.  If the sourcepath option to the shopt builtin command is turned off, the  PATH  is  not
              searched.   If  any  arguments  are  supplied, they become the positional parameters when filename is executed.  Otherwise the
              positional parameters are unchanged.  The return status is the status of the last command exited within the script  (0  if  no
              commands are executed), and false if filename is not found or cannot be read.

以下方式均可以:ip

一、將腳本所在的目錄加到PATH中it

二、加上相對路徑或絕對路徑io

三、就算是當前目錄也得加./table

相關文章
相關標籤/搜索