1.
#shell脚本的赋值方式
#注意:shell脚本的赋值方式与C语言的赋值方式不同,通常我们在C中给一个变量赋值需要指明该变量的变量类型,shell脚本则不必指明类型。其赋值方式如下:
#例如:要取出c中的内容可以这样操作:
[root@localhost ~]# a=10
[root@localhost ~]# b="$a"
[root@localhost ~]# c="$b"
[root@localhost ~]# echo $c
10
5.shell文件权限的修改:
命令:chmod
比如:
#新建一个shell脚本文件,查看其属性为“-rw-r--r--”,第一个符号为“-”,此时该文件没有可执行权限,需修改属性。
[root@localhost ~]# vim a.sh
[root@localhost ~]# ls -l a.sh
-rw-r--r-- 1 root root 30 08-01 22:59 a.sh
#修改后
[root@localhost ~]# chmod 777 a.sh
[root@localhost ~]# ls -l a.sh
-rwxrwxrwx 1 root root 30 08-01 22:59 a.sh
#补充:文件权限修改的几种方式
[root@localhost ~]# chmod 777 a.sh
[root@localhost ~]# chmod -rwxrwxrwx a.sh
[root@localhost ~]# chmod u+x,g+wx,o+wx a.sh
6. expr 算术算法命令(只能进行简单的整型数据加(+),减(-),乘(\*),除(/),不能进行浮点型数据运算)
加法:
[root@localhost ~]# expr 20 + 30
50
减法:
[root@localhost ~]# expr 20 - 30
-10
乘法:
[root@localhost ~]# expr 20 \* 30
600
除法:
[root@localhost ~]# expr 60 / 20
3
注意:数字与操作符之间必须要空格,否则会有语法错误。
7.tput 语句
tput setf [1,2,3,4,5,6,7]以高亮方式显示文件(常用在播放器的歌词高亮方式显示)
#在终端上显示有实际不同颜色的效果,但是这里不能显示颜色,故不作演示。
tput bold 以粗字体显示
tput blink 闪烁显示
#配合tput setf 使用还不错
tput sgr 0 取消所有效果显示
其它如:
tput bel 终端响铃
tput smul 启动下划线模式
tput rmul 关闭下划线模式
tput smso 启动突出显示模式
tput rmso 关闭突出显示模式
8. LINUX中的break与continue(与C中相同)
break->跳出整个循环
continue->跳出当前循环,进入下一个循环。
9. for 循环语句格式:
for 变量名 in 列表
do
命令
done
例:
#!/bin/bash
for a in 1 2 3 4 5
do
echo -n "$a"
done
echo
运行shell脚本后输出:
[root@localhost ~]# 12345
10. echo $? 查看上一条指令或程序执行结果:
[root@localhost ~]# ls sh3.sh
ls: sh3.sh: 没有那个文件或目录
[root@localhost ~]# echo $?
2-》非0表示执行ls sh3.sh失败,当前目录中没有该文件或目录,返回一个非0的值。
[root@localhost ~]# ls sh2.sh
sh2.sh
[root@localhost ~]# echo $?
0
#0-》0表示执行成功,返回0。
#1~255表示执行失败
who-》查看当前登录到系统的用户
[root@localhost ~]# who
11. shell函数
a=10-》全局变量
local a=10-》局部变量
在linux中变量前有关键字local,则表示局部变量。
a=10不论在函数内部还是外部都是可用。
local a=10不论在函数内部还是外部都为局部变量。