2017.01.23
1.shell 脚本必须以#!/bin/bash 开头,注意:最好使用“!/bin/bash”而不是“!/bin/sh”,如果使用tc shell改为tcsh,其他类似。
2.Shell编程中,使用变量无需事先声明,同时变量名的命名须遵循如下规则:
1>首个字符必须为字母(a-z,A-Z)
2>中间不能有空格,可以使用下划线(_)
3>不能使用标点符号
4>不能使用bash里的关键字(可用help命令查看保留关键字)
5>字符串输出要用"",如 "A is:"
# 对变量赋值:
a="hello world" #等号两边均不能有空格存在
# 打印变量a的值:
echo "A is:" $a
有时候变量名可能会和其它文字混淆,比如:
num=2
echo "this is the $numnd"
上述脚本并不会输出"this is the 2nd"而是"this is the ";这是由于shell会去搜索变量numnd的值,而实际上这个变量此时并没有值,返回空值。这时,我们可以用花括号来告诉shell要打印的是num变量:
num=2
echo "this is the ${num}nd"
其输出结果为:this is the 2nd0
原文:http://www.cnblogs.com/Davison/p/6343993.html