Shell 编程 函数
本篇主要写一些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 编程 函数的更多相关文章
- Linux之shell编程函数使用
linux shell 可以用户定义函数,然后在shell脚本中可以随便调用.下面说说它的定义方法,以及调用需要注意那些事项. 原文和作者一起讨论:http://www.cnblogs.com/int ...
- shell编程-函数(九)
每种语言都有自己的函数,shell也不例外.支持函数,它可以将脚本程序划分成一个个相对独立的代码块,使代码的模块化,结构更加清晰,并有效地减少程序的代码量,提高代码的复用率. 函数格式 functio ...
- 2.shell编程-函数的高级用法
2.1.函数的定义和使用 函数基本使用 [root@VM_0_9_centos ~]# test() > {} -bash: syntax error near unexpected token ...
- shell编程中的 三种结构: 条件if/选择结构case/循环for/while/until等结构 和 函数的用法
shell 函数的使用 (md中, 列表本身是有格式的, 他要产生缩进, 其次,列表项和列表项之间, 可以留有一个空行, 是合法的, 允许的) shell函数,就是 就相当于一个命令来看待和处理的, ...
- 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 ...
- 【Shell 编程基础第二部分】Shell里的流程控制、Shell里的函数及脚本调试方法!
http://blog.csdn.net/xiaominghimi/article/details/7603003 本站文章均为李华明Himi原创,转载务必在明显处注明:转载自[黑米GameDev街区 ...
- shell编程系列19--文本处理三剑客之awk中的字符串函数
shell编程系列19--文本处理三剑客之awk中的字符串函数 字符串函数对照表(上) 函数名 解释 函数返回值 length(str) 计算字符串长度 整数长度值 index(str1,str2) ...
- shell编程系列6--shell中的函数
shell编程系列6--shell中的函数 .函数介绍 linux shell中的函数和大多数编程语言中的函数一样 将相似的任务或者代码封装到函数中,供其他地方调用 语法格式 第一种格式 name() ...
- 【shell编程】之基础知识-函数
linux shell 可以用户定义函数,然后在shell脚本中可以随便调用. shell中函数的定义格式如下: [ function ] funname [()] { action; [return ...
随机推荐
- Codeforces Round #507 (Div. 2, based on Olympiad of Metropolises) D mt19937
https://codeforces.com/contest/1040/problem/D 用法 mt19937 g(种子); //种子:time(0) mt19937_64 g(); //long ...
- Windows搭建FTP/Http文件共享(利用IIS)
控制面板——程序——添加功能 勾选Ftp服务器.万维网服务.IIS管理控制台 然后,计算机——右键管理——服务和应用程序,添加网站和添加Ftp IP设置为 未分配 或 本机获取到的静态IP即可. 然后 ...
- 第22课 weak_ptr弱引用智能指针
一. weak_ptr的概况 (一)weak_ptr的创建 1. 直接初始化:weak_ptr<T> wp(sp); //其中sp为shared_ptr类型 2. 赋值: wp1 = sp ...
- snowflake ID生成器
背景 Snowflake 是 Twitter 内部的一个 ID 生算法, 可以通过一些简单的规则保证在大规模分布式情况下生成唯一的 ID 号码. 其组成为: 第一个 bit 为未使用的符号位. 第二部 ...
- asp.net core 3.0 身份认证 替换为自已的提供程序 AuthenticationStateProvider replace to SelfAuthenticationStateProvider
public void ConfigureServices(IServiceCollection services) { // 添加身份验证服务 services.AddAuthorizationCo ...
- 使用JDBC连接MySQL数据库操作增删改查
目录 1.首先这个Myeclipse的包名以及一些实现的类(这样子写是我的习惯) 2.接下来我们创建数据库(MySQL) 3.在数据库里面添加数据 4.首先是BaseDao,这个是重中之重,注意那个数 ...
- Ubuntu 16 PPA源管理(查询、添加、修改、删除)
查询 在Ubuntu中,每个PPA源是单独存放在/etc/apt/sources.list.d/文件夹中的,进入到该文件夹,使用ls命令查询即可列出当前系统添加的PPA源. 添加 sudo add-a ...
- EasyNetQ笔记
Each call to Subscribe creates a new queue consumer. If you call Subscribe two times with the same m ...
- eclipse打开本地文件所在目录位置的快捷键
在开发的过程中总是会遇到需要在本地文件夹找到该本地文件的情况,比如说要发送给同事什么的. 在使用Eclipse的过程中,大多数人都是先在Eclipse目录中定位到文件,然后通过在文件的右键属性中找到文 ...
- 数据库IN查询参数化改造的方法
// 批量查询的 2019-05-14 if (!string.IsNullOrWhiteSpace(Request["userCodes"])) { string userCod ...