shell脚本 空格
1.定义变量时, =号的两边不可以留空格.
eg:
gender=femal————right
gender =femal———–wrong
gender= femal———–wrong
2.条件测试语句 [ 符号的两边都要留空格.
eg:
if [ $gender = femal ]; then——-right.
echo “you are femal”;
fi
if[ $gender...-----------------------wrong
if [$gender...----------------------wrong.
3.条件测试的内容,如果是字符串比较的话, 比较符号两边要留空格!
eg:
if [ $gender = femal ]; then——-right.
if [ $gender= femal ]; then——–wrong.
if [ $gender=femal ]; then———wrong.
4.如果if 和 then写在同一行, 那么,注意, then的前面要跟上;号.如果 then 换行写, 那么也没问题.
eg:
if [ $gender = femal ];then——-right.
if [ $gender = femal ]
then——————————-right.
if [ $gender = femal ] then——-wrong. then前面少了 ; 号.
提示出错信息:
syntax error near unexpected token then
同理,还有很多出错信息 比如
syntax error near unexpected token fi 等都是这样引起的.
5.if 后面一定要跟上 then. 同理elif 后面一定要跟上 then.
不然提示出错信息:
syntax error near unexpected token else
1)if 语句后面需要跟着then,同时前面要有分号;
2) 空格非常重要,shell 会认为空格前的为一个命令,如果a=3 认为是赋值操作,如果写成a = 3,那么就会认为a为一个命令 this=ls -l |grep ‘^-' | wc -l
3) 操作符之间要用空格分开 ,如 test ! -d $1,其中的!和-d就要用空格分开
空格是命令解析中的重要分隔符
6. 命令和其后的参数或对象之间一定要有空格
if [ -x "/Workspace/shell/a.sh" ];then
只有 -x 后有空格才表示紧跟其后的字符串是否指向一个可执行的文件名,否则就成了测试 -x"/Workspace/shell/a.sh" 这个字符串是不是空。
7.取变量值的符号'$'和后边的变量或括号不能有空格
8. 管道两边空格可有可无
shell脚本 空格的更多相关文章
- shell脚本格式的几点注意:格式严格,空格不能随便出现(一写就记不住)
shell脚本中,不能随意添加空格,否则出错: 1,=等号两边必须无空格.否则出错.如i =$1和i= $1都是错的.但是在()内部不限制如for ((i= 1;i < 3;i= i+1))是正 ...
- shell脚本参数中有空格
shell脚本参数中有空格 在shell脚本中如果有空格的处理如下: sh test.sh "hello word" echo $1 得到的是hello,而不是hello word ...
- 解决shell脚本参数传递含有空格的问题
有这样一个py文件,需要传一个字典作为参数: import json import sys def parse_params(data): json_data = json.loads(data[1] ...
- Shell脚本传递带有空格的参数
在另一博文<Shell脚本实现DB2数据库表导出到文件>中实现了通过脚本实现将DB2数据库导出到文件,需要传入七个参数,最后一个是一个带有空格字符串,所以传入的时候有点问题,会自动识别空格 ...
- Shell脚本编程30分钟入门
Shell脚本编程30分钟入门 转载地址: Shell脚本编程30分钟入门 什么是Shell脚本 示例 看个例子吧: #!/bin/sh cd ~ mkdir shell_tut cd shell_t ...
- Linux shell脚本编程(一)
Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...
- shell脚本俄罗斯方块游戏
亲自测试了一个大牛写的shell脚本,感兴趣可以看看,效果如下:
- 学习 shell脚本之前的基础知识
转载自:http://www.92csz.com/study/linux/12.htm 学习 shell脚本之前的基础知识 日常的linux系统管理工作中必不可少的就是shell脚本,如果不会写sh ...
- shell脚本学习第一课
shell是一种程序设计语言,是访问操作系统内核的服务. Linux的shell种类常见的有: Bourne Shell(/usr/bin/sh或/bin/sh) Bourne Again Shell ...
随机推荐
- 前端性能优化----yahoo前端性能团队总结的35条黄金定律
转自 http://www.cnblogs.com/lei2007/archive/2013/08/16/3262897.html
- 安装时遇到:正在尝试其它镜像。 http://mirrors.btte.net/centos/7.2.1511/extras/x86_64/repodata/repomd.xml: [Errno 14] curl#6 - "Could not resolve host: mirrors.btte.net; 未知的错误"
我出现这种错误是因为网络链接问题,因为我设置虚拟机网络链接为VmNET8,设置了nat模式,使得我本地机可以访问虚拟机的linux服务器.但是打开虚拟机的浏览器却不能上网了.所以现在我用xshell装 ...
- 注入语句详解(get注入,cookie注入,搜索型注入等)
注意:对于普通的get注入,如果是字符型,前加' 后加 and ''=' 拆半法 ###################################### and exists (select ...
- JAVA6开发WebService (一)
转载自http://wuhongyu.iteye.com/blog/807470 WebService是SOA的一种较好的实现方式,它将应用程序的不同功能单元通过中立的契约(独立于硬件平台.操作系统和 ...
- tomcat启动后,在普通java类中获取spring管理的bean和ServletContext,(经过验证,可以取到)
//从spring容易中获取bean public static Object getBean(String beanName){ ApplicationContext context = Conte ...
- Java实现智能机器自动操作电脑
package com.tz.util; import java.awt.Robot; import java.awt.event.InputEvent; import java.awt.event. ...
- Mysql忽略文件名的安全编码
author:skatetime:2014/09/28 mysql如何删除以"#sql-"开头的临时表 现象:在重建索引后,发现Mysql服务器的磁盘空间快满了 在用如下命令重建索 ...
- Android课程---用进度条改变图片透明度
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- Python多行注释
由于Python的注释只有针对于单行的注释(用#),多行注释时很不方便(要想多行注释只能每行代码前面都加上#).在网上看到一个Python的多行注释方法,分享给大家,其实很简单的,就是使用一对三个双引 ...
- 省略号 对单行 多行的css
.twoline{ display: -webkit-box !important;; overflow:hidden; text-overflow: ellipsis; word-break: br ...