Linux Shell编程(一)

目录

1.边写一个简单的shell脚本

1--创建一个shell脚本文件

2.vi内容

3.查看已经边写的文件

2.执行Shell脚本的方式

1--  /bin/sh   脚本文件名(或者脚本路径)

2-- /bin/bash  脚本文件名(或者脚本路径)

3-- sh 脚本文件名(或者脚本路径)

4--bash 脚本文件名(或者脚本路径)

3.Shell脚本变量名的命名规则

4.使用Shell变量名

5.只读变量

6.删除变量


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值