一起来学linux:shell script(二)关于脚本
(一)首先来看shell脚本的执行方式,shell脚本的后缀名都是sh文件。
1 sh test.sh
2 source test.sh
这两种方式有什么区别呢。test.sh 里的脚本很简单, 从键盘输入名字后赋值个name变量
read -p “Please input your name:” name
执行如下
root@zhf-linux:/home/zhf/zhf/shell_prj# sh test1.sh
please input your name:zhf
root@zhf-linux:/home/zhf/zhf/shell_prj# echo $name
echo
$name是空值。原因前面介绍过。bash在执行脚本的时候,是另起一个子进程。当子进程完成后,子进程内的变量不会传入父进程中去
但是用source执行脚本就不一样了。
原因在于source对script的执行方式是在父进程中进行的。
root@zhf-linux:/home/zhf/zhf/shell_prj#
source test1.sh
please
input your name:zhf
root@zhf-linux:/home/zhf/zhf/shell_prj#
echo $name
zhf
在脚本中,避免不了要进行各种条件判断。条件判断有test
和[]两种方式
test方式:
read
-p "please input your name:" name
test
-z $name && echo "you must input your name"
-z
是判断字符串是否为0也就空字符串,如果是,则提示输入名字
root@zhf-linux:/home/zhf/zhf/shell_prj#
sh test1.sh
please
input your name:
you
must input your name
test有很多判断参数,具体可以用man
test的进行查看
[]方式:
read
-p "please input your name:" name
[
-z "$name" ] && echo "please input your name"
使用[]有几点需要注意:
1
在中括号内的每个组件都需要有空格键来分隔
2
在中括号内的变量都需要用双引号起来
关于第二点,如果不用双引号括起来,会引起如下问题:
root@zhf-linux:/home/zhf/zhf/shell_prj#
name="cq zhf"
root@zhf-linux:/home/zhf/zhf/shell_prj#
[ $name == "cq zhf" ]
bash:
[: too many arguments
原因在于$name=cq
zhf, 如果没有双括号,那么上面的不等式就等于
cq
zhf == “cq zhf”。系统会认为是2个变量,因此提示too
many arguments.
(二)script的变量。
在执行脚本的时候,会传入某些参数。那么在脚本里面如何监控这些参数呢
$#代表参数的个数
$@得到全部的参数
$1,$2…..代表的是各个参数。其中$0是指的脚本名称。
root@zhf-linux:/home/zhf/zhf/shell_prj#
sh test1.sh one two three
The
scritp name is test1.sh
The
total number of parameter is 3
The
first parameter is one
如果输入的变量太多,单从顺序上去记忆也比较麻烦,有一种参数偏移的方法可以省事一些。shift命令。这个命令的作用好比是一个FIFO的栈。
root@zhf-linux:/home/zhf/zhf/shell_prj#
sh test1.sh one two three
The
scritp name is test1.sh
The
total number of parameter is 3
The
first parameter is one
The
first parameter is two
其他流程控制语句,例如if,
when, for...do...done等都和其他语言用法类似。
一起来学linux:shell script(二)关于脚本的更多相关文章
- 一个改动配置文件的linux shell script
不久以前,以前搜到一篇博客是读取配置文件的,http://www.cnblogs.com/bo083/archive/2012/11/19/2777076.html,用到如今,感觉十分方便.感谢作者. ...
- Linux shell script All In One
Linux shell script All In One refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
- Linux shell编写端口扫描脚本
Linux shell编写端口扫描脚本 需求: 扫描特定主机 扫描特定主机的特定端口 扫描特定网段 扫描特定网段中哪些主机开放了特定的端口 源码如下: #/bin/bash #该脚本用于对特定目标主机 ...
- linux shell 写swoole重启脚本
linux shell 写swoole重启脚本 代码如下<pre>#!/bin/shkill `lsof -t -i:9501`sleep 2php /data/web/mircoweb/ ...
- Linux shell批量执行scp脚本工具
转载: linux shell + expect:批量scp脚本工具 2011-09-13 15:51:06 分类: Python/Ruby 最近在准备一个部署的任务,其中有一 ...
- Linux Shell Script目录
目录 Linux Shell基础 开始Shell编程 代码 示例代码查看:https://github.com/Furzoom/demo-C/tree/master/src/shell
- shell script 二 判断符号【】 shift 偏移量 if then fi
判断符号[]类似于test.但是[]有通配符及正则表达式,为了区分,利用[]来做判断时,前后都需要加空格来区分.又一个坑 [ -z "$HOME" ];echo $? 例: 1 r ...
- Linux shell Script初识
shell secript: 执行方式的差异: ./ sh执行都是在创建一个子程序来执行,只会继承环境变量, 其中的变量如果export声明,子程序的子程序会继承,不会升级为环境变量 source 的 ...
- Linux shell简单创建用户脚本
前面介绍简单的shell编写规则. 现在开始编写一个简单的shell脚本. Linux shell介绍 编写shell脚本 1.创建脚本文件 2.根据需求,编写脚本 3.测试执行脚本 ...
随机推荐
- 设置ListView的item不能点击
写了一个ListView结合volley的demo ListView只是用来展示数据,所以不需要点击效果. 网上搜索了下: 可以禁用ListView ListView.setEnabled(false ...
- 解决Ubuntu下gedit中文乱码的情况
windows下简体中文多用GBK编码 (或GB2312, GB18030), 而linux下多用UTF-8编码. 因此,一般来说在微软平台编辑的中文txt不能在ubuntu下直接打开查看,除非在保存 ...
- 2017.2.21 Java中正则表达式的学习及示例
学习网站:菜鸟教程 http://www.runoob.com/java/java-regular-expressions.html 1 正则表达式的基本使用 (1)类 正则表达式并不仅限于某一种语言 ...
- Docker 开源管理工具集锦
俗话说工欲善其事.必先利其器.Docker 是一种详细的虚拟化技术,Docker 尽管以RestAPI形式提供服务.但在实际生产环境中,管理大规模集群部署的Docker容器确实是一个巨大的挑战.尽管D ...
- GSL 1.15 and 1.16 building with Visual Studio 2010 --FROM 4fire
http://4fire.wordpress.com/2012/03/18/gsl-1-15-building-with-visual-studio-2010/ Update 05/02/2014: ...
- WeakReference &&reference quene &&GC
在了解WeakReference之前,先给出一段简单的代码: public class WeakReferenceTest {public static void main(String[] args ...
- js 原生方法获取所有兄弟节点
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- ubuntu 下开源安装
常用开源库安装: 0.安装g++: sudo apt-get install g++ 1.首先不可或缺的就是编译器与基本的函式库: sudo apt-get install build-essenti ...
- PX4学习之-uORB简单体验
一.前言 最近项目使用到 CPU2 与 CPU0 之间的通信, 使用定时器传递消息到 CPU0 后, CPU0 需要将消息分发到不同的应用程序里面. PX4 里面使用的是 uORB 多线程/进程通信机 ...
- cmake学习之- cmake_parse_arguments
最后更新: 2019-06-08 一.指令介绍 cmake_parse_arguments 为解析函数(function)或 宏(macros) 参数的命令: cmake_parse_argument ...