Bash学习笔记(3)----变量与运算符
Bash学习笔记(3)变量和参数一、 变量定义
二、使用变量变量名前加美元符号,如: myname="shiyanlou"
echo $myname
echo ${myname}
echo ${myname}Good
echo $mynameGood
myname="miao"
echo ${myname}
三、只读变量使用 下面的例子尝试更改只读变量,结果报错: #!/bin/bash
myUrl="http://www.shiyanlou.com"
readonly myUrl
myUrl="http://www.shiyanlou.com"
运行脚本,结果如下: /bin/sh: NAME: This variable is read only.
四、特殊变量1.局部变量这种变量只有在代码块或者函数中才可见。后面的实验会详细讲解。 2.环境变量这种变量将影响用户接口和 shell 的行为。 在通常情况下,每个进程都有自己的“环境”,这个环境是由一组变量组成的,这些变量中存有进程可能需要引用的信息。在这种情况下,shell 与一个一般的进程没什么区别。 3.位置参数从命令行传递到脚本的参数:
4.位置参数实例这个十分重要,再我们运行一套脚本的时候,有时候是需要参数的,这里我们教大家如何获取参数 $ vim test.sh 输入代码(中文皆为注释,不用输入): #!/bin/bash
# 作为用例,调用这个脚本至少需要10个参数,比如:
# bash test.sh 1 2 3 4 5 6 7 8 9 10
MINPARAMS=10
echo
echo "The name of this script is "$0"."
# 添加./是表示当前目录
echo "The name of this script is "`basename $0`"."
# 去掉路径名,剩下文件名,(参见'basename')
echo
if [ -n "$1" ] # 测试变量被引用.
then
echo "Parameter #1 is $1" # 需要引用才能够转义"#"
fi
if [ -n "$2" ]
then
echo "Parameter #2 is $2"
fi
if [ -n "${10}" ] # 大于$9的参数必须用{}括起来.
then
echo "Parameter #10 is ${10}"
fi
echo "-----------------------------------"
echo "All the command-line parameters are: "$*""
if [ $# -lt "$MINPARAMS" ]
then
echo
echo "This script needs at least $MINPARAMS command-line arguments!"
fi
echo
exit 0
运行代码: $ bash test.sh 1 2 10
The name of this script is "test.sh".
The name of this script is "test.sh".
Parameter #1 is 1
Parameter #2 is 2
-----------------------------------
All the command-line parameters are: 1 2 10
This script needs at least 10 command-line arguments!
基本运算符
一、算数运算符$vim test.sh
#!/bin/bash
a=10
b=20
val=`expr $a + $b`
echo "a + b : $val"
val=`expr $a - $b`
echo "a - b : $val"
val=`expr $a * $b`
echo "a * b : $val"
val=`expr $b / $a`
echo "b / a : $val"
val=`expr $b % $a`
echo "b % a : $val"
if [ $a == $b ]
then
echo "a == b"
fi
if [ $a != $b ]
then
echo "a != b"
fi
运行 $bash test.sh
a + b : 30
a - b : -10
a * b : 200
b / a : 2
b % a : 0
a != b
二、关系运算符关系运算符只支持数字,不支持字符串,除非字符串的值是数字。 实例 vim test2.sh #!/bin/bash
a=10
b=20
if [ $a -eq $b ]
then
echo "$a -eq $b : a == b"
else
echo "$a -eq $b: a != b"
fi
运行 $bash test2.sh
10 -eq 20: a != b
三、逻辑运算符实例 #!/bin/bash
a=10
b=20
if [[ $a -lt 100 && $b -gt 100 ]]
then
echo "return true"
else
echo "return false"
fi
if [[ $a -lt 100 || $b -gt 100 ]]
then
echo "return true"
else
echo "return false"
fi
结果 return false
return true
四、字符串运算符#!/bin/bash
a="abc"
b="efg"
if [ $a = $b ]
then
echo "$a = $b : a == b"
else
echo "$a = $b: a != b"
fi
if [ -n $a ]
then
echo "-n $a : The string length is not 0"
else
echo "-n $a : The string length is 0"
fi
if [ $a ]
then
echo "$a : The string is not empty"
else
echo "$a : The string is empty"
fi
结果 abc = efg: a != b
-n abc : The string length is not 0
abc : The string is not empty
五、文件测试运算符实例 #!/bin/bash
file="/home/shiyanlou/test.sh"
if [ -r $file ]
then
echo "The file is readable"
else
echo "The file is not readable"
fi
if [ -e $file ]
then
echo "File exists"
else
echo "File not exists"
fi
结果 The file is readable File exists (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- angularjs – 增加ui.grid中列的宽度
- angularjs – Angular Translate的useSanitizeValueStrateg
- 深入理解 AngularJS 的 Scope
- angular-ng-bootstrap模式在交叉点击时不关闭
- 参数服务组件 Example 程序
- angular – 从新行开始文本
- angularjs – 为什么是ngModel $setViewValue(…)不工作
- scala – Spark:如何使用mapPartition并为每个分区创建/关
- angularjs – 为什么我在指令定义上得到错误TS2345?
- 关于#!/bin/sh 和 #!/bin/bash 的差别