当前位置:  技术问答>linux和unix

请问脚本中的.使用时需要注意什么?如何去定义使用?

    来源: 互联网  发布时间:2017-05-29

    本文导语:  举个例子…… #!/bin/sh #通过载入这个库你的脚本讲根据你输入的上一条指令的成功与否返回OK或FALSE的提示 . /etc/sysconfig/network RESPONSE = "Last Result: " if [ $? -ne 0]; then ^Iaction$"$RESPONSE" /bin/false else ^Iaction$"$RESPONSE...

举个例子……

#!/bin/sh
#通过载入这个库你的脚本讲根据你输入的上一条指令的成功与否返回OK或FALSE的提示


. /etc/sysconfig/network
RESPONSE = "Last Result: "


if [ $? -ne 0]; then
^Iaction$"$RESPONSE" /bin/false
else
^Iaction$"$RESPONSE" /bin/true
fi


如果一开始不定义". /etc/sysconfig/network"这一行,执行脚本时会提示bash: action: command not found


定义这行以后就可以显示结果“Last Result: [OK]”


于是我天真的认为这应该和C的#include是一个效果才对……
可是,我后来写了一个755权限的脚本test.sh,里面包含了类似于函数库的代码段
test(){
#函数内容
}
结果test的内容根本调用不到……所以想问问各位这个应该怎么写定义才能调用?


另外我也会经常看到类似这样的用法:
[ -e /etc/sysconfig/$PROGRAM_NAME ] && . /etc/sysconfig/$PROGRAM_NAME
是在要求检查$PROGRAM_NAME的存在性么?如果不存在就运行它,否则就执行/etc/sysconfig/network?这个我没什么自信啊……烦请提点一下这个是什么意思,有什么需要注意的地方?

|

作用跟 source 命令相同

 
.  filename [arguments]
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.

|
. /etc/sysconfig/network

意思是在这里脚本中执行上诉的脚本,相当于包含了那个脚本定义的函数,全局变量等。

    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。














站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3