Linux shell script function All In One
shell 脚本函数
shell script function
linux shell 可以用户定义函数,然后在shell脚本中可以随便调用。
# shell script 函数的定义格式
[ function ] funname [()]
{
action;
[return int;]
}
1、可以带function fun() 定义,也可以直接fun() 定义,不带任何参数。
2、参数返回,可以显示加:return 返回,如果不加,将以最后一条命令运行结果,作为返回值。 return后跟数值n(0-255

https://www.runoob.com/linux/linux-shell-func.html
# shell script function
is_same_file () {
test "$(stat -c "%d %i" "$1")" = "$(stat -c "%d %i" "$2")";
}
# 等价于
# function 关键字
function is_same_file () {
# 使用 $0 ~ $N 接收函数的参数列表 ✅
# 返回值,是最后一行命令的执行结果的退出状态码 ✅
return test "$(stat -c "%d %i" "$1")" = "$(stat -c "%d %i" "$2")";
}
$ if is_same_file student.sh hard; then echo yes; else echo no; fi
# yes
$ if is_same_file student.sh soft; then echo yes; else echo no; fi
# no
demos
bug
# ❌ inode is different
$ [ ./student.sh -ef ./soft ] && echo yes || echo no
yes
$ [ ./student.sh -ef ./hard ] && echo yes || echo no
yes
#!/usr/bin/env bash
echo "./student.sh -ef ./soft"
# if [ ./student.sh -ef ./soft ]; then
if [ ./test.sh -ef ./test-soft-link.sh ]; then
echo "yes"
else
echo "no"
fi
echo "./student.sh -ef ./hard"
# if [ ./student.sh -ef ./hard ]; then
if [ ./test.sh -ef ./test-hard-link.sh ]; then
echo "yes"
else
echo "no"
fi
<<EOF
eric@rpi4b:~ $ ls -ail | grep test
24553 -rw-r--r-- 1 eric eric 411 9月 21 11:37 nvm_echo_test.sh
30221 -rwxr-xr-x 1 eric eric 172 9月 21 18:03 shell-env-variable-test.sh
24592 -rwxr-xr-x 2 eric eric 156 9月 21 13:15 test-hard-link.sh
24592 -rwxr-xr-x 2 eric eric 156 9月 21 13:15 test.sh
23662 lrwxrwxrwx 1 eric eric 7 10月 8 16:44 test-soft-link.sh -> test.sh
EOF

(? 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
refs
https://www.cnblogs.com/xgqfrms/p/17728141.html#5221859
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 ?️,侵权必究⚠️!