美文网首页
Shell脚本常见使用

Shell脚本常见使用

作者: 黄宝玲_1003 | 来源:发表于2019-10-28 11:09 被阅读0次

持续更新中......

1、脚本开始通常有这么一句 Home=$(dirname $(dirname $(readlink -f $0)))

$0 : 表示当前运行的命令名,一般用于脚本中

示例:

$ vi test.sh

$ cat test.sh
#bin/bash
echo "$0"

$ ./test.sh
./test.sh

$ mkdir testFile

$ mv test.sh testFile/

$ ./testFile/test.sh
./testFile/test.sh

dirname : 输出已经去除了尾部的"/"字符部分的名称;如果名称中不包含"/",
则显示"."(表示当前目录)。

示例:

$ cat test.sh
#bin/bash
dirname /hello/summer/f
dirname kk.txt

$ ./test.sh
/hello/summer
.

readlink : 是linux系统中一个常用工具,主要用来找出符号链接所指向的位置。
-f : -f选项可以递归跟随给出文件名的所有符号链接以标准化,除最后一个外所有组件必须存在。
(简单地说,就是一直跟随符号链接,直到直到非符号链接的文件位置,限制是最后必须存在一个非符号链接的文件。)

示例:

$ cat test.sh
#bin/bash
s=$(readlink -f $0)
echo "${s}"

$ ./test.sh
/c/Windows/system32/hbl/test.sh

所以用readlink命令我们可以直接获取$0参数的全路径文件名,然后再用dirname获取其所在的绝对路径:

$ cat test.sh
#bin/bash
s=$(dirname $(readlink -f $0))
echo "${s}"

$ ./test.sh
/c/Windows/system32/hbl

$( ) : 命令替换
${ } : 变量替换

相关文章

网友评论

      本文标题:Shell脚本常见使用

      本文链接:https://www.haomeiwen.com/subject/dpbvvctx.html