本篇主要写一些shell脚本函数的使用。


函数调用

#!/bin/bash
sum(){
s=`expr 2 + 3`
echo $s
}
sum
[root@localhost ~]# vim sum.sh
[root@localhost ~]# chmod +x sum.sh
[root@localhost ~]# ./sum.sh
5

传递参数

#!/bin/bash
sum(){
s=`expr $1 + $2`
echo $s
}
sum 2 3
[root@localhost ~]# vim sum.sh
[root@localhost ~]# ./sum.sh
5

return

#!/bin/bash
sum(){
return $(($1 + $2))
}
sum 2 3
echo $?
[root@localhost ~]# vim sum.sh
[root@localhost ~]# ./sum.sh
5

echo

#!/bin/bash
sum(){
echo $(($1 + $2))
}
res=$(sum 2 3)
echo $?,$res
[root@localhost ~]# vim sum.sh
[root@localhost ~]# ./sum.sh
0,5

自定义函数

#!/bin/bash
service_index(){
echo "Usage:servicectl <ServiceName> <start | stop | reload | restart | status>"
return 1
}
service_version(){
grep "release 7" /etc/centos-release &> /dev/null && echo "centos7"
grep "release 6" /etc/centos-release &> /dev/null && echo "centos6"
}
servicectl(){
[[ -z $1 || -z $2 ]] && service_index
[ $(service_version) = "centos7" ] && systemctl $2 ${1}.service || service $1 $2
}
[root@localhost ~]# vim servicectl.sh
[root@localhost ~]# source servicectl.sh
[root@localhost ~]# servicectl
Usage:servicectl <ServiceName> <start | stop | reload | restart | status>
Unknown operation '.service'.
Usage: service < option > | --status-all | [ service_name [ command | --full-restart ] ]
[root@localhost ~]# servicectl sshd status
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2019-10-08 03:07:15 CST; 11s ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 3169 (sshd)
CGroup: /system.slice/sshd.service
└─3169 /usr/sbin/sshd -D Oct 08 03:07:15 localhost systemd[1]: Starting OpenSSH server daemon...
Oct 08 03:07:15 localhost sshd[3169]: Server listening on 0.0.0.0 port 22.
Oct 08 03:07:15 localhost sshd[3169]: Server listening on :: port 22.
Oct 08 03:07:15 localhost systemd[1]: Started OpenSSH server daemon.

Shell 编程 函数的更多相关文章

  1. Linux之shell编程函数使用

    linux shell 可以用户定义函数,然后在shell脚本中可以随便调用.下面说说它的定义方法,以及调用需要注意那些事项. 原文和作者一起讨论:http://www.cnblogs.com/int ...

  2. shell编程-函数(九)

    每种语言都有自己的函数,shell也不例外.支持函数,它可以将脚本程序划分成一个个相对独立的代码块,使代码的模块化,结构更加清晰,并有效地减少程序的代码量,提高代码的复用率. 函数格式 functio ...

  3. 2.shell编程-函数的高级用法

    2.1.函数的定义和使用 函数基本使用 [root@VM_0_9_centos ~]# test() > {} -bash: syntax error near unexpected token ...

  4. shell编程中的 三种结构: 条件if/选择结构case/循环for/while/until等结构 和 函数的用法

    shell 函数的使用 (md中, 列表本身是有格式的, 他要产生缩进, 其次,列表项和列表项之间, 可以留有一个空行, 是合法的, 允许的) shell函数,就是 就相当于一个命令来看待和处理的, ...

  5. centos shell编程5 LANMP一键安装脚本 lamp sed lnmp 变量和字符串比较不能用-eq cat > /usr/local/apache2/htdocs/index.php <<EOF重定向 shell的变量和函数命名不能有横杠 平台可以用arch命令,获取是i686还是x86_64 curl 下载 第三十九节课

    centos shell编程5  LANMP一键安装脚本 lamp  sed  lnmp  变量和字符串比较不能用-eq  cat > /usr/local/apache2/htdocs/ind ...

  6. 【Shell 编程基础第二部分】Shell里的流程控制、Shell里的函数及脚本调试方法!

    http://blog.csdn.net/xiaominghimi/article/details/7603003 本站文章均为李华明Himi原创,转载务必在明显处注明:转载自[黑米GameDev街区 ...

  7. shell编程系列19--文本处理三剑客之awk中的字符串函数

    shell编程系列19--文本处理三剑客之awk中的字符串函数 字符串函数对照表(上) 函数名 解释 函数返回值 length(str) 计算字符串长度 整数长度值 index(str1,str2) ...

  8. shell编程系列6--shell中的函数

    shell编程系列6--shell中的函数 .函数介绍 linux shell中的函数和大多数编程语言中的函数一样 将相似的任务或者代码封装到函数中,供其他地方调用 语法格式 第一种格式 name() ...

  9. 【shell编程】之基础知识-函数

    linux shell 可以用户定义函数,然后在shell脚本中可以随便调用. shell中函数的定义格式如下: [ function ] funname [()] { action; [return ...

随机推荐

  1. LeetCode3-Longest_Substring_Without_Repeating_Characters

    参考思路 https://github.com/azl397985856/leetcode/blob/master/problems/3.longestSubstringWithoutRepeatin ...

  2. C# 集合根据属性去重筛选

    1.单个属性去重筛选 //去重筛选 var ChgDtlVoList = datas.Where((x, i) => datas.FindIndex(z => z.ChgId == x.C ...

  3. 调试经验分享-让自己的电脑充当WI-Fi模块,用来抓取连接Wi-Fi模块APP上的通信数据

    需求 手头有了厂家的APP和Wi-Fi模块 在已经知道APP是通过TCP连接Wi-Fi模块(8266), 同时也知道了连接的端口号的 情况下如何知道厂家的APP发送给Wi-Fi模块的数据 打开自己的笔 ...

  4. 前端性能优化&&网站性能优化

    加载优化:1.合并css.JavaScript 2.合并小图片,使用精灵图 3.缓存一切可缓存的资源 4.使用长cache 5.使用外联式引用css.JavaScript 6.压缩HTML.CSS.J ...

  5. JAVA还没死的原因

    尽管 TIOBE 指数显示,Java 是一门正在衰落的语言,但它仍然稳居榜首.从 2016 年到 2017 年间,这个数字可能会大幅下降,但最近下降速度有所放缓:在 2018 年 10 月到 2019 ...

  6. loj2305 NOI2017 游戏

    题目链接 思路 既然\(x\)的数量那么小,我们就可以先把每个\(x\)搜索一遍. 枚举x的时候不需要把\(a,b,c\)全枚举一遍,只要枚举其中的两个就可以枚举到当前位置选任何车的情况. 然后就变成 ...

  7. SpringCloud微服务实现生产者消费者以及ribbon负载均衡

    一.SpringCloud_eureka_server 1.导入依赖 <dependencies> <dependency> <groupId>junit</ ...

  8. .NetCore 使用k8s部署服务的过程中需要注意的地方以及遇到的问题

    这里开始我准备了3台测试服务器,这里我使用了JumpServer管理起来了,这里我们来看下: Master :192.168.0.236 Node1:192.168.0.237 Node2:192.1 ...

  9. AngularJS入门Demo

    1 :表达式 <html> <head> <title>入门小Demo-1</title> <script src="angular.m ...

  10. 转《深入理解 Java 内存模型》读书笔记

    转:https://mp.weixin.qq.com/s/2hA6u4hLEPWlTPdD-XB-bg 前提 <深入理解 Java 内存模型>程晓明著,该书在以前看过一遍,现在学的东西越多 ...