本篇主要写一些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. nwjs-打包

    1: 将项目内所有文件压缩成一个压缩包 app.zip 2: 将压缩包重命名为 app.nw 3: 将压缩包放置到 下载解压后的 nw.js 根目录下 4: shift+鼠标右键 选择在此处打开命令窗 ...

  2. <LinkedList> 160

    160. Intersection of Two Linked Lists 分别从AB循环两次.如果第一次没循环到,第二次就会在节点相遇. public class Solution { public ...

  3. raid,磁盘配额,DNS综合测试题

    DNS解析综合学习案例1.用户需把/dev/myvg/mylv逻辑卷以支持磁盘配额的方式挂载到网页目录下2.在网页目录下创建测试文件index.html,内容为用户名称,通过浏览器访问测试3.创建用户 ...

  4. 显示隐藏文件.reg

    显示隐藏文件.reg Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Microsoft\Windows\Curren ...

  5. [LeetCode] 153. Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  6. it's over | 2019 CSP-S 第一轮认证

    不知道自己有没有凉,毕竟我们省这么弱(据说有的省80都悬... 其实这几天对初赛基本没什么感觉,可能是没给自己多大压力吧,倒是班上的一群同学似乎比我们还着急,我们的数学课代表兼数竞大佬特意给我画了吉祥 ...

  7. Elasticsearch由浅入深(十一)内核原理

    倒排索引组成结构以及索引不可变原因 对于倒排索引是非常适合用来进行搜索的它的结构:(1)包含这个关键词的document list(2)包含这个关键词的所有document的数量:IDF(invers ...

  8. openldap 指定普通用户登录ldap后可查看某分组下的用户信息

    #ldap普通用户登录限制查看信息#在/openldap/slapd.conf文件最下面添加一下代码,可控制某个用户拥有查看用户信息的权限,而其他普通用户登录后无法查看用户信息,若有多个普通用户需要用 ...

  9. JAVA PTA 7-1 作品评分 (10 分)

    全国中小学生Scratch作品大赛拉开了序幕.每个参赛选手可以通过网络直接上传作品.本次比赛人人可做评委.每个网络评委可以通过网络对每一件作品进行打分.评分系统也是请程序高手设计的,能自动去掉一个最高 ...

  10. bizcharts 图表内容居中

    当图表内的数据只有一组时,会紧靠在y轴上,如下图: 想要图表的内容居中,解决方法分两种情况. 第一种:如果x轴是日期,则代码设置如下,图表的内容就居中了 const cols = { x: { ali ...