目录
1.边写一个简单的shell脚本
1--创建一个shell脚本文件
[xijiu@localhost ~]$ touch hello.sh
[xijiu@localhost ~]$ cat hello.sh
[xijiu@localhost ~]$ ls
2.txt hello.sh shellScript.sh softdir test.txt
[xijiu@localhost ~]$ chmod 777 hello.sh
[xijiu@localhost ~]$ ls
2.txt hello.sh shellScript.sh softdir test.txt
[xijiu@localhost ~]$ vi hello.sh
2.vi内容
# 这个是注释
echo 'hello world'
3.查看已经边写的文件
cat hello.sh
[xijiu@localhost ~]$ cat hello.sh
# 这个是注释
echo 'hello world':
[xijiu@localhost ~]$
2.执行Shell脚本的方式
1-- /bin/sh 脚本文件名(或者脚本路径)
[xijiu@localhost ~]$ /bin/sh hello.sh
hello world:
2-- /bin/bash 脚本文件名(或者脚本路径)
[xijiu@localhost ~]$ /bin/bash hello.sh
hello world:
3-- sh 脚本文件名(或者脚本路径)
[xijiu@localhost ~]$ sh hello.sh
hello world:
4--bash 脚本文件名(或者脚本路径)
[xijiu@localhost ~]$ bash hello.sh
hello world:
3.Shell脚本变量名的命名规则
- 命名只能以英文字母和数字和下划线组成,并且首个字符不能是数字
- 变量名之间不能有空格
- 不能使用标点符号
- 不能是bash中的关键字(if , for ,else 等)
有效的命名如何下
myFirst
my_two
my_three
myThree
Go1
Go2
my_1_demo
无效命名
.
?gogog=123
[xijiu@localhost ~]$ ?gogog=123
bash: ?gogog=123: 未找到命令...
user/admin='gogogo'
[xijiu@localhost ~]$ user/admin='gogogo'
-bash: user/admin=gogogo: 没有那个文件或目录
my demo = easyEdit
[xijiu@localhost ~]$ my demo? = easyEdit
bash: my: 未找到命令...
4.使用Shell变量名
定义完变量 想要 使用变量 必须使用 $变量名 或 ${变量名} 引用
[xijiu@localhost ~]$ myFirst="go to park"
[xijiu@localhost ~]$ echo myFirst
myFirst
[xijiu@localhost ~]$ echo $myFirst
go to park[xijiu@localhost ~]$ echo ${myFirst}
go to park
5.只读变量
readonly 变量名
[xijiu@localhost ~]$ username="xi.ji"
[xijiu@localhost ~]$ readonly username
[xijiu@localhost ~]$ echo username
username
[xijiu@localhost ~]$ username="gogo"
-bash: username: 只读变量
[xijiu@localhost ~]$
6.删除变量
unset 变量名
注意:unset 不能删除只读变量
[xijiu@localhost ~]$ username="xi.ji"
[xijiu@localhost ~]$ readonly username
[xijiu@localhost ~]$ echo username
username
[xijiu@localhost ~]$ username="gogo"
-bash: username: 只读变量
[xijiu@localhost ~]$ adminname="zhangsan"
[xijiu@localhost ~]$ echo adminname
adminname
[xijiu@localhost ~]$ unset adminname
[xijiu@localhost ~]$ unset username
-bash: unset: username: 无法反设定: 只读 variable